In line code for MOSS

In line Code for MOSS

Adding in line code is tricky. Microsoft support policies recommended for inline code are as under: 

Inline code in Site Definition based ASPX pages:

Inline code is not recommended within a site definition due to the issues involved with the code no longer rendering as soon as the page is customized. 

Inline code in _layouts-based ASPX pages:

Inline code is supportable if included on a page in the _layouts directory. 

Inline code enabled through PageParserPath exclusion:

Inline code should not be allowed through the PageParserPath exclusion, except in extremely rare circumstances, because it can be modified without going through any review process. As a result, it represents a danger to the environment’s performance and security. 

So think about the pros and cons and if you are still ready to take the ride, continue reading. The first step is to open the site in Microsoft Office SharePoint Designer.  

Go from File -> New -> Page and select ASP.Net under “Page” and then select “Create from Master Page..”. You can add any page depending upon your requirements. 

This will open a blank page which will inherit master page look & feel. Now add you script under first head tag. If you don’t find it, add the head tag as well. The code written by me is as under: 

<head>

<META name=”WebPartPageExpansion” content=”full”>

<script runat=”server”>        

 SPSite sp;       

SPWeb WebSite;       

SPList DocLib;       

public void Page_Load(object o, EventArgs e){                

sp = new SPSite(“Site URL”);       

WebSite = sp.OpenWeb();       

DocLib = WebSite.Lists[“Doc Library Name”];       

} 

public void Hello_Click(object o, EventArgs e){       

context.Response.Write(“HELLO WORLD”);

} 

</script>  

</head>             

Add the method on the OnClick event of the button. Lastly, make changes in the web.config file. Usually present at this path C:\Inetpub\wwwroot\wss\VirtualDirectories\80.

But before making changes, save the file at some other place or you might be in big trouble. Add the following line in the PageParserPaths. Be careful.  

<PageParserPaths>       

<PageParserPath VirtualPath=”/*” CompilationMode=”Always” AllowServerSideScript=”true” IncludeSubFolders=”true” />     

</PageParserPaths>

</PageParserPaths> 

Save the changes and access the page in the browser. 

Exception: 

An error occurred during the compilation of the requested file, or one of its dependencies. The type or namespace name ‘SPSite’ could not be found (are you missing a using directive or an assembly reference?) 

Reason: 

Forget to import SharePoint namespace. 

Resolution: 

Add <%@ Import Namespace=”Microsoft.SharePoint” %> after the page directive. 

Exception: 

An error occurred during the compilation of the requested file, or one of its dependencies. Invalid token ‘=’ in class, struct, or interface member declaration 

Reason: 

Didn’t initialize variables in Page_Load method. 

Resolution: 

Initialize variables in Page_Load method.

Advertisements

1 Comment »

  1. Ambuncnen said

    Good aftenoon !
    Good stuff, very nicely done.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: