How to: Target Web Parts to an Audience

Applies to: SharePoint Server 2010

Web Parts in Microsoft SharePoint Server 2010 can be targeted to appear only to people who are members of a particular group or audience. When you do this by editing the Web Part in the browser, SharePoint Server 2010 assigns the GUID that identifies the audience to the AuthorizationFilter property of the Web Part. You can programmatically target a Web Part to an audience by performing the same operation in your code. When you do this, the correct audience name will appear and can be edited when users choose to edit a Web Part in the browser.

Target a Web Part to a Specific Audience in the Browser

Use the following procedure to target a Web Part to a specific audience in the browser.

Target a Web Part to a specific audience in the browser

  1. On the Web page that contains the Web Part, on the Site Actions menu, click Edit Page.

  2. On the Web Part, click the Web Part drop-down menu, and then click Edit Web Part.

  3. Under Advanced, type one or more audience names in the Target Audiences box.

Target a Web Part to a Specific Audience Programmatically

You can assign three kinds of audiences to the AuthorizationFilter property. To target a Web Part to a global audience programmatically, you can retrieve the GUID for the audience you want by using a AudienceManager object and then assigning that GUID to the AuthorizationFilter property of the Web Part. You can also assign the Lightweight Directory Access Protocol (LDAP) distinguished name of a distribution list or the name value of a SharePoint group to this property.

The SharePoint Server 2010 framework requires that these 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"). The following example builds a string that would be valid for assignment to the AuthorizationFilter property.

string[] audienceIDs = new string[] {"GUID", "GUID"};
string[] distributionLists = new string[] 
{"LDAP Distinguished Distribution List Name", "LDAP Distinguished Distribution List Name"};
string[] sharePointGroups = new string[] {"SharePoint Group Name", "SharePoint Group Name"};

string result = string.Format("{0};;{1};;{2}",
string.Join(",", audienceIDs),
string.Join("\n", distributionLists),
string.Join(",", sharePointGroups));
Dim audienceIDs() As String = {"GUID", "GUID"}
Dim distributionLists() As String = {"LDAP Distinguished Distribution List Name", "LDAP Distinguished Distribution List Name"}
Dim sharePointGroups() As String = {"SharePoint Group Name", "SharePoint Group Name"}

Dim result As String = String.Format("{0};;{1};;{2}", String.Join(",", audienceIDs), String.Join(vbLf, distributionLists), String.Join(",", sharePointGroups))

If you are working with a Web Parts page, you can use the SPLimitedWebPartManager object to get the Web Part whose property you want to edit. The following example shows how to retrieve the GUID for an Audience and assign it to the AuthorizationFilter property of the first Web Part on a page. The example assumes that you have references to and using statements for Microsoft.SharePoint, Microsoft.SharePoint.WebPartPages, and Microsoft.Office.Server.Audience. Notice that the example adds four semicolons (";;;;") after the GUIDs, to indicate that it is adding no distribution lists or SharePoint groups.

When you target the Web Part to a specific audience in this way, the names of the specified audiences appear in the Target Audiences box when users edit the Web Part by using the browser.

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" and "Sample Audience 2" 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, audienceManager.GetAudience("Sample Audience2").AudienceID);
// Save the changes to the Web Part.
            webPartManager.SaveChanges(webPartManager.WebParts[0]);
         }
   }
 Using site As SPWeb = CType(properties.Feature.Parent, SPWeb)
' Get SPLimitedWebPartManager for "default.aspx" page.
     Using webPartManager As SPLimitedWebPartManager = site.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared)
           Dim audienceManager As New AudienceManager(ServerContext.Current)
' Assign the GUID for "Sample Audience" and "Sample Audience 2" 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, audienceManager.GetAudience("Sample Audience2").AudienceID)
' Save the changes to the Web Part.
          webPartManager.SaveChanges(webPartManager.WebParts(0))
     End Using
End Using

See Also

Tasks

How to: Target List Items to an Audience

Reference

AudienceManager