Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Programmatically add audience for web parts sharepoint 2010

Programmatically add audience for web parts sharepoint 2010

In this Post we will set the audience for a WebPart which Web Parts page, you can use the SPLimitedWebPartManager object to get the Web Part whose property you want to edit.

using (SPWeb site = (SPWeb)properties.Feature.Parent)
{
// Get SPLimitedWebPartManager for “default.aspx” page.
using (SPLimitedWebPartManager webPartManager = site.GetLimitedWebPartManager(“default.aspx”, PersonalizationScope.Shared))
{
AudienceManager audienceManager = new AudienceManager(ServerContext.Current);

// Assign the GUID for “Sample Audience” to the AuthorizationFilter property of the first Web Part on the page.
webPartManager.WebParts[0].AuthorizationFilter = string.Format(“{0}, {1};;;;”,audienceManager.GetAudience(“Sample Audience”).AudienceID);
// Save the changes to the Web Part.
webPartManager.SaveChanges(webPartManager.WebParts[0]);
}}

No comments:

Post a Comment