All about the Strategy, Design, Customisation, Deployment and Development of SharePoint and its related Technologies

  Administration   All Me!! Baby!!   BDC   Book Review   Business   CKS   Conferences   CQWP   Development   Duffer Moments   Email   Errors   Family   Fixes   General   Groove   How To   How To Code   InfoPath   iPhone   IRM   Longhorn   Lotus Notes   Migration   Mobility   Office System 2007   Personal Projects   Powershell   Records Management   Search Server   Security   SharePoint   SharePoint 2010   Silverlight   SQL   Tech Ed 2008   Testing   Vista   VSTO   WSS   XSL

[21/01/2008] SharePoint – User Control in Master Page (Page Footer)
 
Categories: Development, Office System 2007, SharePoint
 

Historically in web programming when you wanted to add let's say a list of static web links to a site you would use the following code:

<!--#include file="mymenus.inc"-->

Using this syntax would allow you to structure a website and use external files as references within the site. Moving on in the technology we now have master pages that pretty much do the same thing. Within SharePoint we use Master Pages for branding but they can also have static content that you wish to appear on each page. One of the requirements I recently faced, was each page needing a footer that could be changed without having to modify the master page. Modifying Master Pages can have some major impact on the site if the person doing the changes is not 100% confident in making these modifications. So I decided to look at using User Controls to do this. To begin with I created a new asp.net website project within Visual Studio.

I then added a new item to the project and selected the "Web User Control" option.

The project should now render the components as shown below:

So now for this demonstration we simply want to add some basic HTML tags that render a table with columns and some text values. Open up the "SiteFooter.ascx" and add the following text:

This should be nice and simple and should render as shown below:

So now we have our control, simple it maybe, but created. We now need to compile this and move it to our SharePoint server. It is often best practice to strong name the project but for this demonstration we will just compile as is. Once compiled we end up with the following items:

UPDATE: You may not actually have a .CS file for your user control. If you do not no worries, you can simply just copy the "ASCX" control as the code is compiled in the DLL. I had specifically asks the code to be in a seperate file.

Thanks for the reminder Tobias!! :-)

User Control DLL

Site Footer User Control

 

 

 

Now we need to copy these files to the following locations:

File

Location

UserControls.dll

C:\Inetpub\wwwroot_INTRANET\bin

SiteFooter.ascx

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES

SiteFooter.ascx.cs

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES

 

Now we have them copied we need to make a change to the master page we are using. So open up your master page in good old trusty notepad or SharePoint Designer and make the following change:

Now we have our control registered we can simply add the control using the following syntax anywhere we want in our master page. For this example I have created a new content place holder and added it inside the tags.

So now we have our control built, registered and added to the master page it should render on our page as shown below:

In my demonstration I need to add a quick CSS style to make the text show up in white instead as my text sites on a darker background. This is a simple as modifying the ASCX file, changing the HTML as shown below:

Added CSS Style

Added CSS Style to HTML Table Tags

So now we have our CSS style added and with the existing styles I already have on the master page my new footer now looks like this:

As you can see this is very flexible approach to adding static or what I class as semi-dynamic content that is easy for users to update without breaking SharePoint or having to learn the SharePoint Designer product. In a sample I was working on the other day I changed the approach above to call a SharePoint list for the menu items, this gave me the best of both worlds, ease of updating using a SharePoint list and also making sure that no changes were made to actual files as well. J

 
7 Comments
 

Comments

Monday, 21 Jan 2008 06:25 by Tobias Zimmergren
Hey Liam! Nice post. A tip: You don't need to actually include the .cs file if you compile the .dll and just refers to the assembly from your .ascx file instead. That way you can protect your code-behind in your .dll instead of simply copying the .cs file to the server aswell :) Keep up the good work, see you soon. Tobias Zimmergren

Monday, 21 Jan 2008 07:58 by Liam Cleary
Hey Tobias, Glad you like the post, I had copied the CS file as well due to the fact of when I added the user control I chose the code to be a seperate file. Don't ask why but hey was one of those days. Anyway must remember to change the post to refelect this!! Cheers. Chat soon. :-) Liam

Thursday, 31 Jan 2008 01:32 by Aaron Saikovski
Hi Liam, I found in order to get this method to work. I needed to modify my web.config to have the trust level set at full trust. Also i was thinking of wrapping up my user controls and deploying it as a .wsp solution.

Thursday, 31 Jan 2008 12:40 by Liam Cleary
Hi Aaron, Thanks for this. Will have to check about the "Full" trust. Have been doing some work as well with pushing user controls as a WSP. Works well. Liam

Tuesday, 17 Jun 2008 07:30 by Bilal
Hello, Thanks for this nice article. The problem I am facing here is that control is working fine for Admin, Owners and Members. But it dives access denied for Visitors (Read only) group. I have tried changing to "Full"Trust and all my code behind is using SPSecurity.Runwithelevatedpriviliges but still it is not working for Visitors. Any help on this?

Tuesday, 31 Mar 2009 11:12 by Marius C.
Hi Bilal, Try to deploy using solution proposed by Tobias Z., meaning that in your .ascx file add FullAssembly reference, but make sure you sign your assembly before deployment. In terms of deployment you 2 options, put it in GAC with full trust or use a CAS policy to deploy it. Either way both could be achieved via WSP. Hope it helps, let me know if you need more support! Cheers, Marius C.

Friday, 7 Aug 2009 12:08 by MJ
H, I followed exactly the same steps with my code, but I get following error: An error occurred during the processing of /_controltemplates/CustomMySiteControl.ascx. Could not load type 'CustomControl.CustomMySiteControl' I just made following changes: CustomControl = UserControl CustomMySiteControl = SiteFooter

Name:

URL:

Email:

Comments: