SharePoint Navigation - Setting the Audience Property of an SPNavigationNode

In this tip/trick i will demonstrate a way of setting by code the target audience of an SP navigation node.
You might say setting this property in SharePoint UI is quite simple, but if you want to set it by code you will not find any straight forward property or method out of the box in SharePoint Object Model that does the job.

The SPNavigationNode class has a property called “Properties”, which allows us to add custom properties to the node. One of the Property is “Audience”, which is used for setting the target audience on the navigation node.

using (SPSite site = new SPSite("URL of your SharePoint Site"))
{        
        using (SPWeb web = site.OpenWeb())
        { 
            SPNavigationNode navNode = web.Navigation.GlobalNodes[0]; 
 
            if (navNode.Properties.Contains("Audience"))
            {
                navNode.Properties["Audience"] = "Users or Groups";
            } 
            else 
            {
                navNode.Properties.Add("Audience", "Users or Groups");
            }
            navNode.Update();
        }
}

1 Comment

Add a Comment

As it will appear on the website

Not displayed

Your website