Monday, March 19, 2012

How to create a SharePoint menu from xml

We had a requirement wherein we wanted to create a SharePoint menu with 4 level of menus. We used an xml source for the same for the same.

Step1: Create a document library and add a new xml document from SharePoint designer.

Step 2: Open your master page and add a new datasource. To add a new datasource, click on the above ribbon and select DataSource - select your xml file. It will include the below code in your master page

'SharePoint:SPXmlDataSource runat="server" id="SPXmlDataSource1"' 'DataFileParameters' 'asp:Parameter Name="FileName" DefaultValue="main_menu.xml"/' 'asp:Parameter Name="FilePath" DefaultValue="Menu"/' '/DataFileParameters' '/SharePoint:SPXmlDataSource'

Step 3: Make the below changes in your AspMenu control:

'SharePoint:AspMenu
ID="SPSiteMapProvider1"
Runat="server"
EnableViewState="false"
DataSourceID="SPXmlDataSource1"
UseSimpleRendering="true"
UseSeparateCss="false"
Orientation="Horizontal"
StaticDisplayLevels="2"
MaximumDynamicDisplayLevels="4"
SkipLinkText=""
CssClass="Custom-tn"'
'DataBindings'
'asp:menuitembinding NavigateUrlField="url"
TextField="text" /' '/DataBindings'
'/SharePoint:AspMenu'


Notice two things above - DataSourceID is the same as the XML datasource.
Also, we have included something as DataBindings to get the exact value from the xml. This is going to change depending on the text field in the xml you are using.

In case you need the same setting for your sub sites as well, you will need to create another master page. Simply copy everything in your master page, create a new one and make the below change.

In the section - SPXMLDataSource change the 'DefaultValue' to below:

http://210.212.92.197:1000/Menu. This is the exact path of the document library of your home site.

5 comments:

  1. Can you give me an idea of what the xml file structure would look like.

    ReplyDelete
  2. Darren,

    XML structure will depend on your requirement. In our case we needed a four level structure. Starting from one node going to three level of sub nodes. We had something like below:
    Menu
    Folder
    Sub Folder
    Node
    Item

    The structure will completely depend on your requirement.

    ReplyDelete
  3. Hi Swati,


    If I give relative like this, it works fine only at the root site. For the sub-sites, it throws me error.
    It applies to the sub-sites, only if I give the absolute URL here.
    Cuz I want to apply this master page to all my sites.

    Can you please give me an idea on this..

    ReplyDelete
  4. Hi Swati,

    asp:Parameter Name="FilePath" DefaultValue="Menu"
    If I give relative like this, it works fine only at the root site. For the sub-sites, it throws me error.
    It applies to the sub-sites, only if I give the absolute URL here.
    Cuz I want to apply this master page to all my sites.

    Can you please give me an idea on this..

    ReplyDelete
  5. Create a copy of your masterpage to use on the sub sites. Change the path of the xml files in the master page for the sub sites. The master page for internal sites will be same except the url for the xml file. Place the xml file in the root site.

    ReplyDelete