Sunday, October 14, 2012

SharePoint: Manipulate Audience Programmatically

Audience is a nice feature in SharePoint but only available in MOSS/SharePoint Server. Generally speaking, Audience is group of users that can be used to target contents. The class ‘Microsoft.Office.Server.Audience.AudienceManager’ is the main entry point for accessing Audience feature in SharePoint. Audience values are of three different types which are separated by ‘;;’:

  • Global audience represented by GUIDs. Multiple global audience values are separated by commas.
  • Distinguished names represented by Fully Qualified Domain Name. Multiple distinguished names are separated by ‘\r\n’.
  • SharePoint Group IDs. Multiple sharepoint groups are separated by commas.

An example of audience field value is given below:

A88B9DCB-5B82-41E4-8A19-17672F307B95, B88B9DCB-5B82-41E4-8A19-17672F307B95 ;; cn=all developers,ou=distribution lists,dc=redmond,dc=corp,dc=microsoft,dc=com \r\n cn=all testers,ou=distribution lists,dc=redmond,dc=corp,dc=microsoft,dc=com \r\n ;; 1,12,21,37

I’ve found many times that people are manipulating these audience field values manually by parsing the text. However there’s SharePoint server object model support to manipulate Audience field programmatically.

Find Audience Field in List/Library

Audience field is of type ‘Microsoft.Office.Server.WebControls.FieldTypes.SPFieldTargetTo’. You can find out the field types from list fields’ as shown below (using any of the two described):

var audienceFields = list.Fields.OfType<Microsoft.Office.Server.WebControls.FieldTypes.SPFieldTargetTo>();

//Alternatively you can use the following code snippet also:
foreach (SPField field in list.Fields)
if (field is SPFieldTargetTo)
//found audience field

Code 1: Get Audience fields


Read/Parse Audience Values

You can manipulate the audience values as shown below. To read value from a list/library audience field, pass the value in the AudienceManager.GetAudienceIDsFromText method. The method will parse the audience field value and populate three out variables: Global Audience, Distinguished Name and SharePoint Group.

string[] globalAudienceIds;
string[] distinguisedNames;
string[] sharepointGroupNames;

var audienceFieldValue = listItem["AudienceField"] == null ? string.Empty : listItem["AudienceField"].ToString();
AudienceManager.GetAudienceIDsFromText(audienceFieldValue, out globalAudienceIds, out distinguisedNames, out sharepointGroupNames);

Code 2: Read Audience Field values


Once you have the global audience values you can get three different types of audience-details as shown below:

var audienceManager = new AudienceManager(ServerContext.GetContext(SPContext.Current.Site));
//get global audience
Audience globalaudience = audienceManager.GetAudience(globalAudienceGuid);
//get distinguished name audience
Audience distinguisedNameAudience = audienceManager.GetAudience(distinguisedName);
//get sharepoint group audience
SPGroup sharepointGroupAudience = web.SiteGroups.GetByID(sharepointGroupId);

Code 3: Get audience based on audience id or name

If you have global audience id (which is valid guid), please convert the guid string to GUID type variable and then pass in the GetAudience method.


Update Audience Field Value

To update audience field value programmatically you need to use following code snippet. You need pass the global audience IDs, distinguished names and sharepoint group names to Audience Manager’s GetAudienceIDsAsText method which will combine the values as text to save in audience field.

listItem["AudienceField"] = AudienceManager.GetAudienceIDsAsText(globalAudienceIds, distinguisedNames, sharepointGroupNames);

Useful links

The following links might be useful for you to read.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.