Create a notification banner on top of every page in a SharePoint 2010 site collection using delegate control

We wanted to create a notification banner on top of every page in a SharePoint 2010 site collection like this:

1

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.

2

Steps to create Delegate Control:

1. Create empty SharePoint project in Visual Studio and deploy as a “Farm Solution“:

3

2. Right click your project name and then “Add >> SharePoint Mapped Folder…”

4

3. Choose “CONTROLTEMPLATES” and click OK:

5

4. Right click CONTROLTEMPLATES you just added and then click “Add >> New Item…”

6

5. Add a new “User Control”:

7

6. Right click your project name and add a new item:

8

7. Add a new “Empty Element”:

9

8. Now your solution explorer looks like this:

10

9. Double click “Feature1” and then change the scope from “Web” to “Site”:

12

10. Open the “Elements.xml” file and modify it so it looks like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<Control Id=”GlobalNavigation” Sequence=”1000″ ControlSrc=”~/_controltemplates/UserControl1.ascx” />
</Elements>

13

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”>

</asp:Panel>

14

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:

15

12. Create a custom list somewhere in SharePoint called “Site Notifications” and the fields are:

16

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(@”
<Where>
<Eq>
<FieldRef Name=’Active’ />
<Value Type=’Boolean’>1</Value>
</Eq>
</Where>”, “”);

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”]) + “

Please contact ” + Convert.ToString(item[“Contact”]) + “ if you have any concern. Thank you!

“;
}
}
else
{
pnlSiteNotifications.Visible = false;
}
}
}

17

14. Deploy your project:

18

15. Go to the “Site Notifications” list and add a new notification:

19

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.

Happy coding!

 

 

Advertisements
This entry was posted in SharePoint 2010, Visual Studio 2010. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s