среда, 28 декабря 2011 г.

Creating custom PortalSiteMapProvider for SharePoint Portal 2010

This provider ables to remove navigation from Cities list and add items from Members list.
CustomNavigationDataProvide.cs

namespace SiteGroup.WebControls
{
    public class CustomNavigationDataProvider : PortalSiteMapProvider
    {
        public override System.Web.SiteMapNodeCollection GetChildNodes(System.Web.SiteMapNode node)
        {

            PortalSiteMapNode portalNode = (PortalSiteMapNode)node;

            SPQuery query = new SPQuery() { Query = "" };
           
            // Remove nodes wich include in "Cities" list

            SiteMapNodeCollection nodeCities = this.GetCachedListItemsByQuery(
                    portalNode.WebNode, "Cities", query, SPContext.Current.Web);
            foreach (SiteMapNode nodeCity in nodeCities)
            {
                for (int nNodeIndex = coll.Count - 1; nNodeIndex >= 0; nNodeIndex--)
                {
                    if (coll[nNodeIndex].Title == nodeCity.Title)
                    {
                        coll.RemoveAt(nNodeIndex);
                    }
                }
            }
            // Add nodes to Members node
            if (node.Title == "Members")
            {
               // PortalSiteMapNode portalNode = (PortalSiteMapNode)node;

                //SPQuery query = new SPQuery() { Query = "" };
                SiteMapNodeCollection listItemNodes = this.GetCachedListItemsByQuery(
                        portalNode.WebNode, "Members", query, SPContext.Current.Web);


                coll = new SiteMapNodeCollection(listItemNodes.Count);
               
                SPWeb web = SPContext.Current.Site.RootWeb;
                Guid guidBank = web.Lists["Members"].ID;
                foreach (PortalListItemSiteMapNode nodeList in listItemNodes)
                {
                    PortalSiteMapNode nodeNew = new PortalSiteMapNode(
                        portalNode.WebNode,
                        string.Format("{0}_add", nodeList.Key),
                        Microsoft.SharePoint.Publishing.NodeTypes.ListItem,
                        nodeList.Url,
                        nodeList.Title,
                        nodeList.Description);
                    coll.Add(nodeNew);
                }

                return coll;
            }

            return coll;
        }
    }
}

Creating Navigation module

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control
    Id="TopNavigationDataSource"
    Sequence="40"
    ControlClass="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapDataSourceSwitch"
    ControlAssembly="Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
    <Property Name="ID">topSiteMap</Property>
    <Property Name="SiteMapProvider">
CustomNavigationDataProvider</Property>
    <Property Name="EnableViewState">false</Property>
    <Property Name="StartFromCurrentNode">true</Property>
    <Property Name="ShowStartingNode">true</Property>
    <Property Name="TreatStartingNodeAsCurrent">true</Property>
    <Property Name="TrimNonCurrentTypes">Heading</Property>
  </Control>
</Elements>

web.config registration

<actions>
  <add path="configuration/system.web/siteMap/providers">
    <add name="LifeGlobalNavSiteMapProvider" description="" type="SiteGroup.WebControls.
CustomNavigationDataProvider, SiteGroup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ce31f39e24cadf59" NavigationType="Combined" EncodeOutput="true" />
  </add>
</actions>


You need to activate feature with Navigation module.

Комментариев нет:

Отправить комментарий