Saturday, September 20, 2014

SharePoint Master Page Dynamic footer using Delegate controls

Hi there,

I had worked on the requirement to display the custom information like site owner, contact number, email and last date modified in the master page footer information.

So all the data related to the site owner, contact number, email are dynamic and when the new person takes the site owner role, our code should be smart enough to update this information rather than changing the master page file.

Here we used the delegate controls to get the information from the custom list where the items contains the site owner name, contact, email so that the delegate control is able to render this information in the master page footer control.

For adding the delegate controls in the master page, first we have to create the user control and then reference it in the master page with attribute ControlID.
<SharePoint:DelegateControl runat="server" ControlId="Footer" />
view raw gistfile1.xml hosted with ❤ by GitHub


Create the user control with mapping the ControlTemplates folder in the visual studio and then deploy this control with specifying the path using module and elements.xml.

Here is the elements.xml from the module file.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control Id="Footer" Sequence="36" ControlSrc="~/_controltemplates/FooterControl.ascx" />
</Elements>
view raw gistfile1.xml hosted with ❤ by GitHub
See the attributes Id mapped to the delegate control in the master page.

Now the FooterControl.ascx file has its own custom logic to fetch the information from the  custom list based on the current web context and able to render the same on the master page.

Here is the sample code used for Footer user control page load method. Here the custom list is located in the root web and it is named as SiteOwner with fields Web title, owner name, email, contact.

Final output:





SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb web1 = SPContext.Current.Web;
SPWeb rootweb=web1.Site.RootWeb;
Literal1.Text = web1.Title + " " + web1.LastItemModifiedDate.ToString();
SPList list = rootweb.Lists.TryGetList("SiteOwner");
SPQuery q1 = new SPQuery();
string query = string.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", web1.Title);
q1.Query = query;
SPListItemCollection items = list.GetItems(q1);
if (items.Count > 0)
{
Literal1.Text = items[0]["Owner"].ToString() + " " + items[0]["Email"].ToString() + " " + items[0]["Contact"].ToString();
}
else
{
Literal1.Text = rootweb.Site.Owner.LoginName;
}
}
);
view raw gistfile1.cs hosted with ❤ by GitHub

No comments: