Link to home
Start Free TrialLog in
Avatar of Carla Romere
Carla RomereFlag for United States of America

asked on

Adding C# code beind on Sharepoint 2010 Master Pages

Is it possible to add any code behind to my default sharepoint master page? I want to add a label to display code in the ribbon to the left of the users welcome drop down. I have added an external link there already and it works beautifully. I just need to get this label functioning.

        string ip = Context.Request.UserHostAddress.ToString();
        myip.Text = "My IP Address is: " + ip;

<asp:Label style="position:relative; top: .15em; color :white; font-family:Verdana; font-size:small;" ID="myip" runat="server" />

Open in new window


We use this frequently at the help desk on our existing intranet and I have to have this functionality in the SharePoint Intranet site before the old one can come down.
Avatar of Member_6283346
Member_6283346

Hi!
You can add code directly in page markup:
<div>My IP Address is: <%= Context.Request.UserHostAddress.ToString(); %> </div>
Avatar of Carla Romere

ASKER

When I do that, I get this error:

An error occurred during the processing of /_catalogs/masterpage/v4default.master. Code blocks are not allowed in this file.

Troubleshoot issues with Microsoft SharePoint Foundation.

Correlation ID: 6a96536c-0ca0-4798-af33-07ed0a4763b5
I just found this link and think I may give it a shot. Why I didn't find this last week, I have no idea.

http://www.codeproject.com/Articles/120190/How-to-enable-code-blocks-in-Sharepoint-2010-Works
You have to add an explicit authorization for the file(s) where you want to do so. The question is: do you really want to do it? You should take the best approach which is to add a code behind file (you can find how to do it here:http://msdn.microsoft.com/en-us/library/bb986729.aspx)  

If you still want to do it, just add a tag like this to your masterpage:

<PageParserPaths>:
<PageParserPath VirtualPath=”/_catalogs/masterpage/MyMaster.Master” CompilationMode=”Always” AllowServerSideScript=”true” />

Open in new window


hth,

ZaR
I tried several different methods of adding a code behind file last week and couldn't get it to work. I'm realtively new to SharePoint customizations. Before, I start working through this tutorial, this link is for SharePoint 2007, will it also work for SharePoint 2010 Foundation?
SOLUTION
Avatar of Tehzar
Tehzar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's exactly one of the ones I tried to follow last week and couldn't get it to work. I will gladly give it another shot if you folks can help me get it to work. Truthfully, I would much rather have a code behind page as opposed to the link I found earlier today.
Sure, what issues were experiencing?
Let me go through it again, step by step, and I'll let you know what happens.
Sounds good, good luck
Okay, I followed part 1 of this link (http://rburgundy.wordpress.com/2010/03/10/sharepoint-2010-custom-masterpage-with-code-behind-file-%E2%80%93-part-1/) and the master page loads correctly. But, I'm seriously showing my ignorance here, where is the code behind page? Part 2 of that tutorial is creating a page layout.

After I followed Part 1, I clicked on Build and it built successfully. Then I clicked on Deploy and it says it deployed successfully. Now, when I go to the SharePoint site, I get this error:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /sites/sandbox/SitePages/Home.aspx
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, I can't get to site settings now because it tells me my site is not found. I didn't change the location of any part of the site. I restarted the Sharepoint User Code Host service, but my site still doesn't load. This site is located on my laptop and I'm testing everything here before I eventually promote it to the Intranet site. I can still open my site from SharePoint Designer though and it looks like all the pages are there.
Ok I found Part 2 of the article above (the link at the bottom took me to Part 2 of another tutorial). However, the first thing it tells me to do is add a reference to Microsoft.SharePoint.Publishing.dll by browsing to directory: C:\Program Files\Microsoft Office Servers\14.0\Bin. I don't have a Microsoft Office Servers folder at all. This seems to be pointing to Office 2010, which I don't have. I have some Office 2010 components that I guess were installed by SharePoint 2010, but I only have Office 2007.

I can always try to modify my web.config file and see if that works, but right now the most important thing is to get my SharePoint site to load once again.
Okay - I got my site to reload by replacing the master page file with the site definition master page. Now back to square one - lol.
Sorry, was distracted by teammates.

Okay, if you're back to square one, try ivan's original post with my code

<PageParserPaths>:
<PageParserPath VirtualPath=”/_catalogs/masterpage/MyMaster.Master” CompilationMode=”Always” AllowServerSideScript=”true” />
I also managed to break the Welcome drop down box now, nothing happens when I click it. On the real site, I get the My Settings, Request Access, etc., not sure what I did.
I'm pretty sure it happened when I depoyed the project that I thought was creating the custom master page. I have no clue how to reverse that though.
When you deployed the project, did you add a reference and object(s) for the code-behind file?
Ok, Ivan and Tehzar -

I modified my web.config file as follows:

<SharePoint>
    <SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
      <PageParserPaths>
        <PageParserPath VirtualPath="/_catalogs/masterpage/v4default.Master" CompilationMode="Always" AllowServerSideScript="true" />
      </PageParserPaths>
    </SafeMode>

I am still getting the no code blocks error:

<div style="position:relative; top: -.4em; color:white; font-family:Verdana; font-size:small;">
My IP Address: <%= Context.Request.UserHostAddress.ToString(); %><br />
Ok - I had an error in my path in the web.config file. I corrected the path, and now I'm getting this error:

Compiler Error Message: CS1026: ) expected
Source Error:
Line 245:       <a href="#" tabindex="-1" style="display:none"></a><a href="#" tabindex="-1" style="display:none"></a>
Line 246:       <div class="s4-trc-container-menu">
on this line==>>Line 247:             <div><div style="position:relative; top: -.4em; color:white; font-family:Verdana; font-size:small;">My IP Address: <%= Context.Request.UserHostAddress.ToString(); %><br />
I tried adding this code in the head section:
	<script type="text/c#">
	public string IpAddress()
	{
		strIp = Context.Request.UserHostAddress.ToString();
  		return strIp;
 		myip.Text = strIp;
	}
	</script>

Open in new window


then I have my label defined in the ribbon:
My IP Address: <asp:Label ID="myip" runat="server" />

But it doesn't get populated. Doesn't give me an error, the text is just blank.
I still haven't gotten anywhere with this project. I've scoured everything I've done and can't figure out why it isn't working.
User generated image
I'm attaching a screenshot of where I want the IP address to appear. Still haven't gotten code-behind to work. I did manage to get a button configured to open a link in a new window.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help and for pointing me in the correct direction!