views:

374

answers:

2

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?

A: 

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

naivists
I did that, and the value of the script is in this format: GUID,GUID,GUID;;;; (one GUID ID for each audience comma separated, followed by 4 semi colons.) When I used that format to set the field, and then viewed the field in edit mode, it contained the exact text entered (the guids, followed by 4 semicolons.)
Kyle Trauberman
In other words: Sharepoint isn't doing any translation on the value set to the field.
Kyle Trauberman
I think, you should read this post, too: http://dotnetmafia.sys-con.com/node/1181567/mobile
naivists
That link is for using audiences on a webpart. I'm trying to audience a list item. I have tried exactly what that post says, but since there is no `AuthorizationFilter` property on a list item, I set the formatted string as the value of the `"Target Audiences"` field.In any case, I have come up with a work around for my issue, and I'll be posting an answer shortly. I would still like to know if it can be done using OOB SharePoint Audience Targeting, though.
Kyle Trauberman
A: 

Kyle, could you post what your final solution was? I am facing the same problem as you.

Andreas
Sure, I'll look a my code tomorrow when I go into work, and post an answer here.
Kyle Trauberman