SharePoint Delegate Control

Hi,

I am working on Public Facing site in sharepoint 2010 and i had requirement to put Image after Top Navigation end on all subsites but not at Parent of Subsite and top level site of Site collection.

I could have done with this creating a Page layout and providing webpart zone so authors can add image in there but what if they want to change image then they have to go all pages change image manually so then i found this link  http://www.sharepointnutsandbolts.com/2007/06/using-delegate-control.html and thought of doing this with Powerful delegate Control.

I have Created Picture Library with Name of file and Site Title so i can fetch image based on site title from list.

First thing i have put Delegate Control Placeholder in MasterPage like below

 <SharePoint:DelegateControl ID="DelegateControl2" runat="server"  ControlId="PictureHolder" ></SharePoint:DelegateControl> 

In Visual Studio 2010 Create Sharepoint Empty Project and add Mapped folder to Control Temple in Layout and add Web User Control(.ascx).

<div id="pic">
<asp:Image ID="PicHolder" runat="server"  Visible="false"/>
</div>

Code Behind file .ascx.cs (Page_Load Event)

try  {

SPWeb web = SPContext.Current.Site.RootWeb;
SPWeb _currrentWeb = SPContext.Current.Web;
string Title = _currrentWeb.Title;
string _getImageByTitle = @"<Where>
<Eq>
<FieldRef Name='SiteLookUp' />
<Value Type='Lookup'>{0}</Value>
</Eq></Where>";

StringBuilder _query = new StringBuilder();
_query.AppendFormat(_getImageByTitle, Title);
SPQuery Query = new SPQuery();
Query.Query = _query.ToString();
SPList _PictureHolder = web.Lists["Site theme Images"];
SPListItemCollection items = _PictureHolder.GetItems(Query);
foreach (SPListItem item in items) {
//Check for Item Status as we are after only item with Approved Status
if((item.ModerationInformation.Status == SPModerationStatusType.Approved) && item.HasPublishedVersion) {
PicHolder.Visible = true;
PicHolder.ImageUrl = item.File.ServerRelativeUrl;
break;
}
}
}
catch(Exception ex) {
Trap Exception
}

Now Its time to replace delegate control in master page with this .ascx Control for that add new feature in Visual Studio 2010 and elements.xml  file of feature should look like this

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Control Id="PictureHolder" ControlSrc="~/_ControlTemplates/Name of Folder /Name of COntrol.ascx" Sequence="80" />
</Elements>

as i said requirement was to put image on SubSite so scope feature at Web Now deploy it and activate feature.

Take look at here for working sample : http://wwwtest.music.udel.edu/about-us/Pages/default.aspx

Now Content Authors want to change image they change in Picture Library and it will change in all pages within that site….

Simple yet Powerfull…

Comments and suggestions are always welcome

Thanks

Ronak

7 thoughts on “SharePoint Delegate Control

  1. I am trying this and I cannot get this to work. my src is always blank.

    My code is below
    try
    {
    SPWeb web = SPContext.Current.Site.RootWeb;
    SPWeb _currentWeb = SPContext.Current.Web;
    string Title = _currentWeb.Title;
    string _getImageByTitle = “” +
    “{0}”;

    StringBuilder _query = new StringBuilder();
    _query.AppendFormat(_getImageByTitle, Title);
    SPQuery Query = new SPQuery();
    Query.Query = _query.ToString();
    SPList _PictureHolder = web.Lists[“Site Theme Images”];
    SPListItemCollection items = _PictureHolder.GetItems(Query);

    foreach (SPListItem item in items)
    {
    //check for item status
    TMPicHolder.Visible = true;

    //PicHolder.ImageUrl = item[SPBuiltInFieldId.EncodedAbsUrl].ToString();
    TMPicHolder.ImageUrl = item.File.ServerRelativeUrl;
    break;
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(“{0} Exception caught.”, ex);
    }

    • Hi Ronak,

      I was able to make it work but using a hardcoded value for the site title because I used a simple text and thought you are using a site title. Then I realize what you did is you created a lookup column in the picture library. How did you use a lookup column to point to a site url? IN your code you use _currentweb.Title which in your case matches your value in that fied you added to the picture library. Can you share me the data type you used for that column in the picture library?

      I’m new to Sharepoint and i’ve tried to debug but I cannot do a step by step line of code debugging. it just goes straight to the website and did not go to each line of code. How do you debug?

      • Hi Michelle,
        Sure.I have sites and subsites and i would like to display banner pics for all of sites and subsites so first i have created a custom list called ListOfSites(on root level site) with only one column Title and enter all Titles of sites make sure it match with name of Sites and subsites titles.
        Then i have created a Asset Library(on root level site) which in sharepoint 2010 if your are using moss u can create Picture Library with columns you need and in this library i have column called NameofSite which type if lookup and its lookup to ListofSites(Title Column).

        Now if you look at code i have two object web(Top level) and _currentweb(current site or subsite).Web object is to get ref of Asset Libray and currentweb to get title of current site.

        Well in case of debug if you are using VS2010 then its plain Vanila and if you have other version then u can wspbuilder and other tools.
        hope this will help
        Thanks
        Ronak

Leave a reply to Jamil Haddadin Cancel reply