Tags

, , , ,

For one of my customers I needed a top navigation which was the the same acccross their different
sitecollections, and offcourse security trimmed.
The top navigation will look like this:

Afbeelding

So in this post I’m explaining the following :

  • create a custom security trimmed sitemap provider
  • deploying a masterpage accross all sitecollections
  • modifying the web.config
  • activating the masterpage acccross the whole site collection

The solution will look like this:

Afbeelding

To create a custom sitemapprovider a created a sitemap file.
Just add an xml file and rename it:

<siteMap>
<siteMapNodetitle=Containerurl=“”>
<siteMapNodetitle=Global Portaldescription=“”url=//>
<siteMapNodetitle=Test 002description=“”url=~/Test 002>
<siteMapNodetitle=test 001url=~/sites/test 002/001description=“”/>
<siteMapNodetitle=test 002url=~/sites/test 002/002description=“”/>
<siteMapNodetitle=test 003url=~/sites/test 002/003description=“”/>
</siteMapNode>
<siteMapNodetitle=Teamdescription=“”url=~/team/>
<siteMapNodetitle=Information Technologydescription=“”url=~/it/>
<siteMapNodetitle=Legaldescription=“”url=~/legal/>
<siteMapNodetitle=Regionsdescription=“”url=“”>
<siteMapNodetitle=North Americaurl=~/regions/NAdescription=“”/>
<siteMapNodetitle=Middle Easturl=~/regions/medescription=“”/>
</siteMapNode>
</siteMapNode>
</siteMap>

The next step is to tell SharePoint that this provider exists, therefore we need to change the web.config file.
We could do this manually but if you have a large farm mmmm….
I created a feature with a receiver that does the work form:

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;namespace MUAC.SiteMapProvider.Features.MUAC.SiteMapProvider
{
[Guid(“0b9032da-7c51-48da-8c4b-9635d307a31a”)]
public class MUACEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
webConfigMod(true, webApp);
}
catch (Exception exception)
{

}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
UnInstallMasterPage(webApp);
webConfigMod(false, webApp);
}

private void webConfigMod(bool setting, SPWebApplication webApp)
{
try
{

#region Muac Custom Navigator

SPWebConfigModification setting_CustomNavigator = new SPWebConfigModification();
setting_CustomNavigator.Path = “configuration/system.web/siteMap/providers”;
setting_CustomNavigator.Name = “add[@name=’MUAC_CustomNavigator’]”;
setting_CustomNavigator.Sequence = 0;
setting_CustomNavigator.Owner = “MUAC_CustomNavigator”;
setting_CustomNavigator.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
setting_CustomNavigator.Value = @”<add name=””MUAC_CustomNavigator”” siteMapFile=””_layouts/MUAC.sitemap”” type=””MUAC.SiteMapProvider.CustomNavigator, MUAC.SiteMapProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02fc9494e4423234″” securityTrimmingEnabled=””true”” />”;

#endregion

if (setting)
webApp.WebConfigModifications.Add(setting_CustomNavigator);
else
RemoveProperty(webApp, setting_CustomNavigator, “MUAC_CustomNavigator”);

webApp.WebService.ApplyWebConfigModifications();
webApp.Update();
}
catch (Exception exception)
{

throw;
}
}

In the code I created a method to add or delete a setting from the web.config.
When you activate your feature your web.config will look like this:

<siteMapdefaultProvider=CurrentNavigationenabled=true>
<providers>

<addname=MUAC_CustomNavigatorsiteMapFile=_layouts/MUAC.sitemaptype=MUAC.SiteMapProvider.CustomNavigator, MUAC.SiteMapProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02fc9494e4423234securityTrimmingEnabled=true />
</providers>
</siteMap>

Dont mind the type property yet, we will get back on this later.
The last thing to do is to modify your masterpage.
I downloaded a version of the v4.master, renamed it and then did the changes on the copy, which is in my case ‘MUAC.master’

You need to search in the masterpage for the ‘SharePoint:AspMenu’.
Right before this control you need to place the following line:

<asp:SiteMapDataSource ShowStartingNode=”False” SiteMapProvider=”MUACNavigationProvider” id=”MUACNavigationProvider” runat=”server”/>

and then you need to modify the ‘SharePoint:AspMenu’ control itself so it looks like this:

<asp:SiteMapDataSource ShowStartingNode=”False” SiteMapProvider=”MUACNavigationProvider” id=”MUACNavigationProvider” runat=”server”/>
<SharePoint:AspMenu
ID=”TopNavigationMenuV4″
Runat=”server”
EnableViewState=”false”
DataSourceID=”MUACNavigationProvider”
AccessKey=”<%$Resources:wss,navigation_accesskey%>
UseSimpleRendering=”true”
UseSeparateCss=”false”
Orientation=”Horizontal”
StaticDisplayLevels=”1″
MaximumDynamicDisplayLevels=”3″
SkipLinkText=””
CssClass=”s4-tn”/>

You need to change the DataSourceId to the one you just added to your masterpage.
Also change the ‘MaximumDynamicDisplayLevels’ else you will not have submenu’s.
I you now save , publish… this master page in to your SharePoint and deploy this feature you will have your menu.

But you need to manually change your masterpage on all sites … , activate the publishing features on all sites…, deploy your masterpage…
This should be done automaticilly!
So we add a new feature site scoped, this feature will only deploy the masterpage.
To do so we need to add a new module to our solution I called it ‘MasterPage’.

Afbeelding

Remove the sample file and add your masterpage (MUAC.master) to your module.
(Right Click add existing file and then search for your altered masterpage)
Open the the ‘Element.xml’ file and modify it so it looks like this:

<Elementsxmlns=http://schemas.microsoft.com/sharepoint/>
<ModuleName=MasterPagesPath=MasterPageRootWebOnly=TRUEUrl=_catalogs/masterpage>
<FileUrl=MUAC.masterType=GhostableInLibraryIgnoreIfAlreadyExists=TRUE >
</File>
</Module>
</Elements>

This module is just deploying the masterpage.
To do so we add a new feature because this cannot be combined with our first feature because that one is not site scoped but webapplication scoped.
The feature looks like this:
Afbeelding

We have now 2 features:

  • To deploy our custom sitemap
  • To deploy the masterpage

Next we want that the masterpage is automaticilly set so we do not have to change every sitecollection manually.
In ourr first feature I added 2 extra methods:

private void InstallMasterPage(SPWebApplication webApp)
{
foreach (SPSite site in webApp.Sites)
{
SPWeb topLevelSite = site.RootWeb;           // Calculate relative path to site from Web Application root.
string webAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!webAppRelativePath.EndsWith(“/”))
webAppRelativePath += “/”;

// Activate publishing infrastructure
site.Features.Add(new Guid(“f6924d36-2fa8-4f0b-b16d-06b7250180fa”), true);

try
{
site.Features.Add(new Guid(“9e9d8436-3a78-4abd-a9d1-d49707ea2116”), true);
}
catch (Exception ex)
{
throw;
}

// Enumerate through each site and apply branding.
foreach (SPWeb web in site.AllWebs)
{
// Activate the publishing feature for all webs.
web.Features.Add(new Guid(“94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb”), true);
web.MasterUrl = webAppRelativePath + “_catalogs/masterpage/MUAC.master”;
web.CustomMasterUrl = webAppRelativePath + “_catalogs/masterpage/MUAC.master”;

web.Update();
}
}
}

private void UnInstallMasterPage(SPWebApplication webApp)
{
try
{
foreach (SPSite site in webApp.Sites)
{
SPWeb topLevelSite = site.RootWeb;

// Calculate relative path to site from Web Application root.
string webAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!webAppRelativePath.EndsWith(“/”))
webAppRelativePath += “/”;

site.Features.Add(new Guid(“9e9d8436-3a78-4abd-a9d1-d49707ea2116”), true);

// Enumerate through each site and apply branding.
foreach (SPWeb subSite in site.AllWebs)
{
subSite.MasterUrl = webAppRelativePath + “_catalogs/masterpage/v4.master”;
subSite.CustomMasterUrl = webAppRelativePath + “_catalogs/masterpage/v4.master”;
subSite.SiteLogoUrl = string.Empty;
subSite.Update();
}
}
}
catch (Exception ex)
{

throw;
}
}

Add ‘InstallMasterPage ‘ in the feature activated and the ‘UnInstallMasterPage’ in the deactivation method.
So this will activate the feature that contains the masterpage if it is their, also activate the publishing features on every site and subsite.
And next it will set the masterpage, when you deactivate it will reverse the settings again.
It just loops through all sitecollections and changes the propery of the masterpage.

try
{
site.Features.Add(new Guid(“9e9d8436-3a78-4abd-a9d1-d49707ea2116”), true);
}
catch (Exception ex)
{
throw;
}

This piece will acctivate our other feature that holds the masterpage, your guid will properbly be different.
You can find it in the properties of the feature, the one that contains the masterpage.
So everything is almost finished, but to make sure our topnavigation bar is security trimmed we need to create our custom Navigator class,
which has a propery called ‘IsAccessibleToUser’ which we can override:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Navigation;
using System.Web;
using System.Xml;namespace MUAC.SiteMapProvider
{
public class CustomNavigator :XmlSiteMapProvider// SPXmlContentMapProvider
{
public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node)
{
try
{
if (node == null)
{
throw new ArgumentNullException(“node”);
}
if (context == null)
{
throw new ArgumentNullException(“context”);
}
if (!base.SecurityTrimmingEnabled)
{
return true;
}
if (string.IsNullOrEmpty(node.Url))
{
return this.IsGranted(context, node.ChildNodes);
}
return this.IsGranted(context, node);
}
catch
{
return false;
}
}

private bool IsGranted(HttpContext context, SiteMapNode node)
{
bool isGranted = false;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb web = new SPSite(SPContext.Current.Site.MakeFullUrl(node.Url)).OpenWeb())
{
SPUser user = web.AllUsers[context.User.Identity.Name];
if (user != null)
{
try
{
if ((node.Roles != null) && (node.Roles.Count > 0))
{
foreach (string str in node.Roles)
{
isGranted = (str == “*”) || (user.Groups[str] != null);
if (isGranted)
{
break;
}
}
}

isGranted = web.DoesUserHavePermissions(user.LoginName, SPBasePermissions.EmptyMask | SPBasePermissions.ViewPages);
}
catch
{
}
}
}
});

return isGranted;
}

private bool IsGranted(HttpContext context, SiteMapNodeCollection childNodes)
{
bool flag = false;
foreach (SiteMapNode node in childNodes)
{
flag = this.IsGranted(context, node);
if (flag)
{
return flag;
}
this.IsGranted(context, node.ChildNodes);
}
return false;
}
}
}

The code is not hard to understand so I’m not going to explain eveything.
But let’s look add the code of our first feature again where we added a provider into the web.config.
This provider needs to referr to this class and asssembly because we created our own Navigator.
The type property needs to referr to our class !!!

  <add name="MUAC_CustomNavigator" siteMapFile="_layouts/MUAC.sitemap"
 type="MUAC.SiteMapProvider.CustomNavigator, MUAC.SiteMapProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02fc9494e4423234" securityTrimmingEnabled="true" />

When you now debug it should your class and checking if the user has access or not.

So that actually it, if you have questions or remarks let me know…