[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Adding C# code beind on Sharepoint 2010 Master Pages

Posted on 2012-04-10
26
Medium Priority
?
971 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:Carla Romere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 7
26 Comments
 
LVL 18

Expert Comment

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

Author Comment

by:Carla Romere
ID: 37828028
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:Carla Romere
ID: 37828079
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Tehzar
ID: 37828093
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:Carla Romere
ID: 37828104
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 2000 total points
ID: 37828133
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:Carla Romere
ID: 37828139
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
ID: 37828151
Sure, what issues were experiencing?
0
 

Author Comment

by:Carla Romere
ID: 37828158
Let me go through it again, step by step, and I'll let you know what happens.
0
 
LVL 9

Expert Comment

by:Tehzar
ID: 37828167
Sounds good, good luck
0
 

Author Comment

by:Carla Romere
ID: 37828348
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 2000 total points
ID: 37828405
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:Carla Romere
ID: 37828419
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
 

Author Comment

by:Carla Romere
ID: 37828601
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:Carla Romere
ID: 37828669
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
ID: 37828752
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:Carla Romere
ID: 37828817
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:Carla Romere
ID: 37828821
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
ID: 37828894
When you deployed the project, did you add a reference and object(s) for the code-behind file?
0
 

Author Comment

by:Carla Romere
ID: 37829535
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:Carla Romere
ID: 37829570
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:Carla Romere
ID: 37829653
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:Carla Romere
ID: 37830202
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:Carla Romere
ID: 37834855
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:
Carla Romere earned 0 total points
ID: 37856484
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:Carla Romere
ID: 37877332
Thanks for the help and for pointing me in the correct direction!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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