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