SharePoint Navigation - Setting the Audience Property of an SPNavigationNode
Posted
by jhallal
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by jhallal
Published on Wed, 22 Jun 2011 07:51:00 GMT
Indexed on
2011/06/22
8:23 UTC
Read the original article
Hit count: 349
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();
}
}
© ASP.net Weblogs or respective owner