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.
Carla RomereDirector of Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ivan_vaguninCommented:
Hi!
You can add code directly in page markup:
<div>My IP Address is: <%= Context.Request.UserHostAddress.ToString(); %> </div>
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

TehzarCommented:
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
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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?
0
TehzarCommented:
Glad I missed the obvious, sigh.  It is different, follow this tutorial instead:

http://rburgundy.wordpress.com/2010/03/10/sharepoint-2010-custom-masterpage-with-code-behind-file-%E2%80%93-part-1/
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
TehzarCommented:
Sure, what issues were experiencing?
0
Carla RomereDirector of Information TechnologyAuthor Commented:
Let me go through it again, step by step, and I'll let you know what happens.
0
TehzarCommented:
Sounds good, good luck
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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
0
TehzarCommented:
Part 2 consists of getting the code-behind file working on the custom master-page.  

Run through part 2 before giving up, it's really teh nuts and bolts of the article.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
TehzarCommented:
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” />
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
TehzarCommented:
When you deployed the project, did you add a reference and object(s) for the code-behind file?
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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 />
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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 />
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
Screenshot of where I want the user's IP address to appear.
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.
0
Carla RomereDirector of Information TechnologyAuthor Commented:
http://www.simple-talk.com/dotnet/.net-tools/using-features-and-solutions-to-deploy-your-sharepoint-customizations/

I finally found a tutorial that after working through a couple of times that successfully displays the user's ip address in the ribbon bar.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carla RomereDirector of Information TechnologyAuthor Commented:
Thanks for the help and for pointing me in the correct direction!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.