How to parse Audiences from list item
I have a SPList
which is audience aware and I need to p开发者_运维百科arse it's audiences from a SPListItem
.
string Audiences;
public Class1(SPListItem item)
{
this.Audiences = item[FieldId.AudienceTargeting] as string;
}
I've come to this so far. I've been searching how to parse each audience from the item but I couldn't find much help.
Does someone know to do that?
I think the proper way to retrieve Audience Targeting contents from the List is to do the following.
string audString = item[fieldId.AudienceTargeting] as string;
string[] audienceIds;
string[] distinguishedNames;
string[] spGroups;
AudienceManager manager = new AudienceManager();
AudienceManager.GetAudienceIDsFromText(audienceAsText, out audienceIds, out distinguishedNames, out spGroups);
var namesList = manager.GetAudienceNames(new ArrayList(globalAudienceID.Select(id => new Guid(id)).ToList()));
This way if the format of the string changes, the change in api in GetAudiencEIDsFromText will evolve accordingly.
from this artical it should be like this
public Class1(SPListItem item)
{
string ID = item[FieldId.AudienceTargeting] as string;
string NewID = ID.Remove(36);
Guid guid = new Guid(NewID);
Audience siteAudience = audManager.GetAudience(guid);
}
And then probably siteAudience.GetMembership();
That is useful when you want the first Audience. With those lines I'm inspired to do something like this, which should get me an array with all the audiences:
public Class1(SPListItem item)
{
string ID = item[FieldId.AudienceTargeting] as string;
string audiences = audienceTargeting.Split(';')[0];
String[] audiencesArray = audiences.split(',');
}
And then iterate to get the audiences by the guid.
Keeping in mind the sintax of the audience target as it is described here: http://www.sharepoint-tips.com/2011/01/setting-target-audiences-with-code.html
I'll confirm later after testing.
精彩评论