Question

I am developing a web part in C# which is reading the contents of a SharePoint list. I can retrieve the values in all the fields I need, except the Target Audience field (which uses AD security groups). I have tried various ways to access this e.g.

string myItem = Convert.ToString(ListItem.properties["Audience"])

but all I get is null returned. I can see that a target group has been stored in the fueield for thwe item when I edit the item in SharePoint.

How can I retrieve the contents of this field using code?

Was it helpful?

Solution

Try not using the Properties of the ListItem, but the fields themselves.

In the "Target Audience" field you have some GUIDs stored as strings, these you need to retrieve like so:

//use the FieldId enumeration for system fields
string audienceID = item[FieldId.AudienceTargeting] as string;
string newID = audienceID.Remove(36); //retrieve just the first guid
Guid audienceGuid = new Guid(newID);

AudienceManager audienceManager= new AudienceManager(SPContext.Current.Site);
Audience audience = audienceManager.GetAudience(guid);

afterwards you might want to look at audience.GetMembership().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top