2008年12月23日 星期二

How to write sever-side code in sharepoint master page

Yes,we know we can add server-side code in asp.net master page directly.

However,when I want to add sever-side code in shaerpoint's master page,

there's unknow error occur.You can't add any server-side code in every
sharepoint page.





If you want to add code in sharepoint master page by code-inline, you can add
code by these steps as folloes:



1.add section in web.config:

<PageParserPaths>

<PageParserPath VirtualPath="/_catalogs/masterpage/*" CompilationMode="Always"
AllowServerSideScript="true" IncludeSubFolders="true" />

</PageParserPaths>

2.And you can add sever-side code in your master page,like this:

<script runat="server">

string myVar = "hello world!";

void Page_Load(object sender, System.EventArgs e)

{

Response.Write(myVar);

SPSite siteCollection = new SPSite("http://usb-server1");

SPWeb site = siteCollection.OpenWeb("/SiteDirectory/MainCalendar/");



SPList list = site.Lists["Announcements"];

SPListItemCollection items = list.Items;



foreach(SPListItem item in items)

{

Response.Write(item["Title"].ToString());

Response.Write("<br/>");

}



}

</script>



code behide:

Create a class and compile it. import your class namespace in your sharepoint
master page.



reference link:

MSDN

沒有留言: