Add sever-side code to SharePoint Master Page

To make the SharePoint allows sever-side code in master page, we need to put an extra line into web.config file first to allow the inline C# code to execute.

<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
      <PageParserPath VirtualPath="/_catalogs/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />

After trying to add many inline code in my master page, I just found an interesting issue.
The page was broke since I added code block between <td></td> tag, and gave me the error message below.
The Controls collection cannot be modified because the control contains code blocks

The problem solved when I added a ContentPlaceHolder to wrap up the code block.

    <asp:ContentPlaceHolder ID="ContentPlaceHolderEasySearch" runat="server">
       <% if (SPContext.Current.Web.ServerRelativeUrl.StartsWith("/myprofile/test/", StringComparison.OrdinalIgnoreCase)) { %>
           <div class="easySearch">
           <a href="#" onclick="MM_openBrWindow('','easySearch','width=900,height=593')">
               <img src="/_layouts/images/btn_easy_search.gif" alt="Ask a question (EasySearch)" style="border:none;" />
<% } %>

Below is the javascript to open a new window:
<script language="javascript" type="text/javascript">
function MM_openBrWindow(theURL,winName,features) {,winName,features);


2 thoughts on “Add sever-side code to SharePoint Master Page

  1. I hardly leave a response, hkwever after reading through a few of the remarks on
    Add sever-side code to SharePoint Master Page | My Share Points.
    I actually do have a couple of questions for you if it’s okay.
    Is it simply me or do some of the remarks
    appear like they are left by brain dead individuals?
    😛 And, if you are posting at additional online sites,
    I would like to keep uup with you. Could you list of every one
    of all your social pages like your Facebook page,
    twitter feed, or linkedin profile?

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s