We wanted to create a notification banner on top of every page in a SharePoint 2010 site collection like this:
You could modify the master page for each site but that very cumbersome.
So we will use the “Delegate Control” to achieve this, without touching the mater pages.
Delegate Control creates a region in the page that will allow to replace the original content with our custom content. This can be handled using SharePoint feature.
In this example I am going to create user control that is going to replace the ‘GlobalNavigation’ in master page using delegate control.
The following the the v4.master page and the DelegateControl with ControlId as “GlobalNavigation” is the one we want to replace the content.
Steps to create Delegate Control:
1. Create empty SharePoint project in Visual Studio and deploy as a “Farm Solution“:
2. Right click your project name and then “Add >> SharePoint Mapped Folder…”
3. Choose “CONTROLTEMPLATES” and click OK:
4. Right click CONTROLTEMPLATES you just added and then click “Add >> New Item…”
5. Add a new “User Control”:
6. Right click your project name and add a new item:
7. Add a new “Empty Element”:
8. Now your solution explorer looks like this:
9. Double click “Feature1” and then change the scope from “Web” to “Site”:
10. Open the “Elements.xml” file and modify it so it looks like this:
<?xml version=”1.0″ encoding=”utf-8″?>
<Control Id=”GlobalNavigation” Sequence=”1000″ ControlSrc=”~/_controltemplates/UserControl1.ascx” />
11. Open the UserControl1.ascx designer view and add the following lines:
<SharePoint:CssRegistration ID=”CssRegistration3″ runat=”server” Name=”/Style Library/Custom CSS/custom.css” />
<asp:Panel ID=”pnlSiteNotifications” runat=”server” CssClass=”SiteNotifications”>
Obviously you will need to have a “custom.css” file in this location: “/Style Library/Custom CSS/custom.css”
This is what I have in the custom.css file:
12. Create a custom list somewhere in SharePoint called “Site Notifications” and the fields are:
13. Open the Code View of the UserControl1.ascs.cs and insert the following line inside the “Page_Load” function:
using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
using (SPWeb parentWeb = currentSite.OpenWeb(“Departments”))
SPWebCollection webs1 = parentWeb.Webs;
SPWeb currentWeb1 = webs1[“IT”];
SPWebCollection webs = currentWeb1.Webs;
SPWeb currentWeb = webs[“MelbIT”];
SPQuery query = new SPQuery();
query.Query = String.Format(@”
<FieldRef Name=’Active’ />
var list = currentWeb.Lists[“Site Notifications”];
SPListItemCollection listItems = list.GetItems(query);
if (listItems.Count > 0)
foreach (SPListItem item in listItems)
Literal1.Text = Convert.ToString(item[“Notification”]) + “
pnlSiteNotifications.Visible = false;
14. Deploy your project:
15. Go to the “Site Notifications” list and add a new notification:
The notification should be on top of every page of the site collection. If you want to remove the notification, you can either delete the above list item or uncheck the “Active” checkbox.
Hope this tutorial helps someone.