Tag Archives: SharePoint 2013

Programmatically set navigation settings in SharePoint 2013

When working on a project where I needed to set the navigation for newly created sites programmatically, I had a hard time finding the correct properties and methods in the SharePoint object model to change all the navigation settings. After some researching I found that you need no less than 3 different objects representing the navigation of a website to be able to modify all the settings. These examples apply to SharePoint sites with publishing features enabled (navigation options differ on non-publishing sites), and just using the PublishingWeb.Navigation and WebNavigationSettings objects were sufficient for me. Other properties can be found in the SPWeb.Navigation object however.

These are the objects containing navigation properties:

  • web.Navigation (Namespace: Microsoft.SharePoint)
  • publishingWeb.Navigation (Namespace: Microsoft.SharePoint.Publishing)
  • WebNavigationSettings (Namespace: Microsoft.SharePoint.Publishing.Navigation)

Here are examples of how you instantiate these objects:

// Web Navigation
using (var site = new SPSite("http://somesite"))
using (var web= site.OpenWeb())
{
 var navigation = web.Navigation;
}

// PublishingWeb Navigation
var pubWeb = PublishingWeb.GetPublishingWeb(web);

// WebNavigationSettings
var webNavigationSettings = new WebNavigationSettings(web);

Once instantiated, you will be able to modify the navigation using these objects. I will show some examples of how to set different options on the Navigation settings page on a publishing site through code.

Select type of navigation

GlobalAndCurrent

There are two navigations. The Global navigation (also called Top navigation) and the Current navigation (also called left navigation or quicklaunch). Both of these can be set to different to use different sources. They can inherit from their parent (if a subsite only), use managed navigation, or structural navigation. All you need to do is select the webNavigationSettings object, choose which navigation to set, and select a source for that navigation.

webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.InheritFromParentWeb;
webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.PortalProvider;
webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.Unknown;
webNavigationSettings.CurrentNavigation.Source = StandardNavigationSource.Unknown;

TaxonomyProvider is Managed Metadata Navigation, PortalProvider is structural navigation, InheritFromParentWeb is self-explanatory, and Unknown leaves no radiobutton selected.

Show subsites and pages

To change these options you need to use the PublishingWeb object.

ShowSubsitesAndPages2

pubWeb.Navigation.CurrentIncludeSubSites = false;
pubWeb.Navigation.CurrentIncludePages = false;
pubWeb.Navigation.CurrentDynamicChildLimit = 20;

Managed navigation: Page settings

To change the values of these two checkboxes, use the WebNavigationSettings object again.

DefaultPageSettings

webNavigationSettings.AddNewPagesToNavigation = false;
webNavigationSettings.CreateFriendlyUrlsForNewPages = false;

Managed navigation: Term set

To connect to a term set in code you can do something like this.

MetadataNavigation

// Below code has to be used from within a method of course.
var session = new TaxonomySession(web.Site);
if (session.TermStores.Count != 0)
{
    var termStore = session.TermStores["MyTermStoreName"]; // Standard name is "Managed Metadata Service"
    var group = this.GetTermGroupByName(termStore.Groups, "MyTermGroupName");
    var termSet = this.GetTermSetByName(group.TermSets, "MyTermSetName");
    webNavigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
    webNavigationSettings.GlobalNavigation.TermSetId = termSet.Id;
}

// Support methods
private Group GetTermGroupByName(GroupCollection groupCollection, string name)
{
    foreach (var group in groupCollection)
    {
        if (group.Name == name)
        {
            return group;
        }
    }
    return null;
}

private TermSet GetTermSetByName(TermSetCollection setCollection, string name)
{
    foreach (var set in setCollection
    {
        if (set.Name == name)
        {
            return set;
        }
    }
    return null;
}

Structural navigation: Sorting

Use the PublishingWeb.Navigation to set the ordering of the Structural navigation.

Sorting

pubWeb.Navigation.OrderingMethod = OrderingMethod.Automatic;
pubWeb.Navigation.AutomaticSortingMethod = AutomaticSortingMethod.Title;

Update
Finally, don’t forget to update your objects when you are done with them.

webNavigationSettings.Update();
pubWeb.Update();
web.Update();

These examples are not a complete overview of what you can do with the navigation in SP2013, but the objects I have shown contain most of the properties needed to set the navigation. You just have to look at the object model in Visual Studio to find other options, and most are pretty self-explanatory.

Advertisements

Remove SharePoint feature witch name starts with… using powershell

This is how to search for features which display name starts with the given parameter (in this case it would list features which started with MyFeature).

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") }

EDIT: You can also find features using GUID instead with the following script:

Get-SPFeature | where { $_.Id.ToString().StartsWith("MyGUID") }

This will remove all those features which are found using the previous script.

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") } | foreach { Uninstall-SPFeature $_.Id -confirm:0 -force }

Sometimes (especially in SharePoint 2013), retracting wsps aren’t done properly (by SharePoint that is), and you might get errors when you are trying to reinstall a retracted wsp. After you have retracted a wsp, if the features in that wsp can still be found by Get-SPFeature in PowerShell, and/or you get an error message installing the wsp that “A feature with ID <guid> has already been installed in this farm.”, you might have to manually remove some of the resources yourself. These steps often work for me.

  1. Retract and remove wsp from server.
  2. Run powershell script above to check if the features of the wsp are still installed.
  3. If there are still features installet, delete them from the SPFarm manuall with the following command:
    $feats = (Get-SPFarm).FeatureDefinitions
    $f = $feats | where {$_.DisplayName.Contains("Feature name")} 
    $f.Delete()
    

    This only works with single results, so make sure you get only one feature in variable $f Before deleting.

  4. Remove feature folders from the hive (for example C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES”
  5. Remove any references in the assembly. In SP2010 (and earlier perhaps), the assembly is located at C:\Windows\assembly, and for SP2013, you can find it at C:\Windows\Microsoft.NET\assembly\GAC_MSIL.
  6. Restart the SharePoint Timer Service (Found in Services)
  7. Restart IIS (cmd “iisreset /noforce”)