Set “Target Audiences” field with multiple values on a List Item
-
19-09-2019 - |
Question
I am attempting to set the "Target Audiences" field on a list item programmatically. I have been able to set the value programmatically for one audience, but when I attempt to use multiple audiences, SharePoint tries to interpret the value I am setting as a single audience, rather than multiple. I am setting the value using the code below.
listItem[listItem.Fields["Target Audiences"].InternalName] = "Audience One";
I use this code to specify multiple audiences like so:
listItem[listItem.Fields["Target Audiences"].InternalName] = "Audience One; Audience Two";
When I do this, SharePoint tries to interpret the entire string as a single audience, and I get a message when I edit the list item that says "No exact match was found."
Am I using the correct format for specifying multiple audiences for this field, or is there a class that I should be using similar to SPFieldLookupValue
?
Solution 2
Just realized I never came back and answered this.
I ended up storing the names of the audiences in a column attached to each list item, then querying the object model to see if the current user is a member of those audiences. This worked for me because I was pulling the data from the list item in a custom web part, and the user never saw the actual list item.
OTHER TIPS
I don't know how to save multiple audiences in a SPListItem, but if I had this problem, I'd try to print out the value of this field from a PowerShell script. Something like:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$site = Microsoft.SharePoint.SPSite("http://yourserver");
$web = $site.openweb();
$list = $web.lists["YourList"];
$item = $list.getitembyid(itemid);
write-output $item["Target Audiences"];
EDIT: found some information about what the Audience field value actually is: http://dotnetmafia.sys-con.com/node/1181567/mobile