Solved

Adding C# code beind on Sharepoint 2010 Master Pages

Posted on 2012-04-10
26
876 Views
Last Modified: 2012-04-22
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.
0
Comment
Question by:Hers2keep
  • 18
  • 7
26 Comments
 
LVL 18

Expert Comment

by:ivan_vagunin
Comment Utility
Hi!
You can add code directly in page markup:
<div>My IP Address is: <%= Context.Request.UserHostAddress.ToString(); %> </div>
0
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Expert Comment

by:Tehzar
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Assisted Solution

by:Tehzar
Tehzar earned 500 total points
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Expert Comment

by:Tehzar
Comment Utility
Sure, what issues were experiencing?
0
 

Author Comment

by:Hers2keep
Comment Utility
Let me go through it again, step by step, and I'll let you know what happens.
0
 
LVL 9

Expert Comment

by:Tehzar
Comment Utility
Sounds good, good luck
0
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Assisted Solution

by:Tehzar
Tehzar earned 500 total points
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Expert Comment

by:Tehzar
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 
LVL 9

Expert Comment

by:Tehzar
Comment Utility
When you deployed the project, did you add a reference and object(s) for the code-behind file?
0
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Author Comment

by:Hers2keep
Comment Utility
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
 

Accepted Solution

by:
Hers2keep earned 0 total points
Comment Utility
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
 

Author Closing Comment

by:Hers2keep
Comment Utility
Thanks for the help and for pointing me in the correct direction!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Last week I faced a strange issue recently, i have deployed SharePoint 2003 servers for one project and one of the requirements was to open SharePoint site from same server. when i was trying to open site from the same server i was getting authentic…
There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now