How to Create a Customize Delegate Control RRS feed

  • Question

  • Anyone,

       I tried creating a Customize Delegate Control for the top menu to substitute the SiteMapDataSource on the  v4.master.
    <SharePoint:AspMenu ID="TopNavigationMenuV4"   Runat="server"   EnableViewState="false"   DataSourceID="topSiteMap"
        AccessKey="1"  UseSimpleRendering="true"   UseSeparateCss="false"  Orientation="Horizontal"   StaticDisplayLevels="2"
        MaximumDynamicDisplayLevels="1"   SkipLinkText=""   CssClass="s4-tn"/>
    <SharePoint:DelegateControl  runat="server"  ControlId="TopNavigationDataSource"   Id="topNavigationDelegate">
               <asp:SiteMapDataSource   ShowStartingNode="False"
                id="topSiteMap" runat="server"

    1. Created an empty project .
    2. Set  Deploy as a farm solution
    Add New Item Control.

        public class DelegateControl : SiteMapDataSource
            protected override void OnDataBinding(EventArgs e)
                //some code here


            protected override void OnLoad(EventArgs e)
                //some code here

    4. Create a Feature to deploy the control.
        In the Element.xml file.
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Control Id="TopNavigationDataSource" Sequence="50"
       ControlAssembly="DelegateControl, Version=, Culture=neutral, PublicKeyToken=a92509c0c043981">
        <Property Name="SiteMapProvider">SPNavigationProvider</Property>
        <Property Name="ID">topSiteMap</Property>

    6. Added a SafeControl entry.
    <?xml version="1.0" encoding="utf-8"?>
    <ProjectItem Type="Microsoft.VisualStudio.SharePoint.GenericElement" DefaultFile="Elements.xml" SupportedTrustLevels="All" SupportedDeploymentScopes="Web, Site, WebApplication, Farm, Package" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
        <ProjectItemFile Source="Elements.xml" Target="DelegateControlFeature\" Type="ElementManifest" />
        <SafeControl Name="SafeControlEntry1" Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="DelegateControl.DelegateControlFeature" TypeName="*" IsSafe="true" IsSafeAgainstScript="false" />

    5. Press Debug and page loads but it doesnt stop at the break point.

    Please help

    M Sariman RKO Business Solutions Inc.

    Wednesday, June 20, 2012 11:50 PM

All replies

  • just wanted to understand what is the reason behind creating such delegate control?

    Can you please explain more?

    Akhilesh Nirapure

    Friday, June 22, 2012 7:03 PM
  • My project is to create dynamic menus for more that 100 site pages that each of them has it's own set of properties for its menu.
    The current method is overriding the SPSiteMapProvider where it's working ok for me but I still need to modify the properties of the masterpage. I want to avoid that and programmatically change MaximumDynamicDisplayLevels via C# code

    Main aim is to access the ASPMenu object without touching the master pages.


    M Sariman RKO Business Solutions Inc.

    Monday, June 25, 2012 5:42 PM
  • Did you ever figure this out?

    I'm trying to do the same things.

    I can successfully override the TopNavigationDataSource to through a feature and by extending PortalSiteMapProvider. (As suggested in this excellent blog post: http://www.helpmeonsharepoint.com/2012/03/custom-sharepoint-top-menu-and.html)

    Like you, I prefer an implementation that does not require modifying the master page.

    BUT, I need to be able to access the TopNavigationMenuV4 ASPmenu control to modify MaximumDynamicDisplayLevels.

    I have not figured out how to do this programmatically.

    I don't think it can be done. But I want to be wrong.

    Monday, August 6, 2012 3:47 PM
  • Hi Mohamad and "the_real_ewillyb",

    @the_real_ewillyb: Thanks for referencing my blog post as a possible solution to override the TopNavigationDataSource. This will answer half of Mohamad's question.

    The other half, how to override the MaximumDynamicDisplayLevels programatically. You cannot do that from the SharePoint API as-is, but you could put a few pieces together and try this approach.

    1- Create a feature with event receiver.

    2- In that event receiver get the current MasterPage in use.

    3- Check out the MasterPage, load it in a FileStream and look for the line with MaximumDynamicDisplayLevels and replace it with whatever number you wish. Before that you could store the previous line as it was on the RootWeb Property Bag. You could reference that from the deactivating step 5

    4- Save the filestream, check-it in and publish it.

    5- Create another event receiver when the feature is de-activated, using the same approach described on 1st to 4th step, retrieving the value from the RootWeb Property Bag instead.

    This is slightly cumbersome but it should be fully functional and a coded approach. No need to use SharePoint Designer or anything like this and it will be compatible with all SharePoint Masterpages in use. I can see some gaps with this solution, for instance, if they change the site masterpage to another one after the feature has been activated. Then our changes to the MaximumDynamicDisplayLevels will not be applied to the new MasterPage, unless we re-activate our feature, or it could overwrite that second MasterPage with the value stored from the first MasterPage when we deactivate the feature again... but well... I think you could work it out in some way mixing up different options. Technically, this would be a possible approach for it.

    I hope it helps!


    Sunday, August 19, 2012 4:43 PM
  • You could (dynamically from within your delegate control code) visit the ASP page control hierarchy and set the property this way:

    var aspMenu = FindControlRecursive(this.Page, "TopNavigationMenuV4") as AspMenu;
    aspMenu.MaximumDynamicDisplayLevels = 4;

    The FindControlRecursive helper is as defined at http://msdn.microsoft.com/en-us/library/y81z8326.aspx.

    I can't vouch for whether breaking out of the SharePoint model like this is a good idea, but I have used this approach for similar needs. I'm dead against editing master pages because we end up with several stakeholders each having a reason to make their master page "the one".

    Wednesday, March 13, 2013 10:29 PM