How to Target Web Parts to an Audience
Hi i am new in sharepoint. How to progammatically Target Web Par开发者_开发问答ts to an Audience suggest any answer.
As per this link:
To provide permission to a sharepoint web part for specific SharePoint Group, we have to use AudienceManager as shown below
SPWeb currentSite = SPContext.Current.Site.RootWeb;
SPSite spSite = currentSite.Site;
SPServiceContext sc = SPServiceContext.GetContext(spSite);
AudienceManager am = new AudienceManager(sc);
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;
mgr = currentSite.GetLimitedWebPartManager("SitePages" + "/" + page,
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in mgr.WebParts)
{
if (webPart.Title == "WebPartTitle")
{
//Get the Existing Audience for the Web Part
string audWebPart = webPart.AuthorizationFilter;
//If there is no Audience for the Web Part just add it
if (audWebPart == "")
{
webPart.AuthorizationFilter = ";;;;" + NewAudience;
}
//If already Audiences are exist for the Web Part then append new Audience
else
{
webPart.AuthorizationFilter = audWebPart + "," + NewAudience;
}
mgr.SaveChanges(webPart);
lblMessage.Text = "Succeeded...";
}
}
If we just set the value for AuthorizationFilter property, then the existing Audiences will be overwritten. To avoid it first we have to check whether already audiences exists for that web part. If so we have to append it.
The SharePoint Server 2010 framework requires that three kinds of values be separated by a pair of semicolons (";;"). Multiple values for global audiences and SharePoint groups are delimited by commas, and multiple values for distribution lists are delimited by newline characters ("\n"). Therefore in the above example we used ";;;;" to provide valid string to the AuthorizationFilter property.
精彩评论