Creating rules for audiences
-
2012年7月31日 12:34
Hello everyone,
I am able to create audiences in Visual Studio, but I am stuck while creating rules for those audiences.
The problem is that we are using several custom fields in user profiles, and I am not able to choose those fields (I can perfectly create rules with standard fields by using "PropertyConstants", and there I get a long list with all the standard fields):
So my question is, how to get the custom fields?
Any help appreciated!
Regards,
Igor.
すべての返信
-
2012年8月13日 18:32
Maybe someone have seen something similar somewhere? I would appreciate your help.
Regards,
Igor.
-
2012年8月23日 8:24
My colleague has helped me with that issue. The thing is that you need to reference term by GUID... Here is a code snippet:
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://site"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
var userProfileManager = new UserProfileManager(context);
AudienceManager AudMgr = new AudienceManager(context);
AudienceCollection ac = AudMgr.Audiences;
bool ruleListNotEmpty = false;
Audience a = null;
string sAudName = "some name";
string sDescription = "some description";
TaxonomySession session = new TaxonomySession(site);
var termStore = session.TermStores["Managed Metadata Service Internal Proxy"];
var group = from g in termStore.Groups where g.Name == "User Profile Terms" select g;
var termSet = group.FirstOrDefault().TermSets["name of a term set"];
string termID = string.Empty;
foreach (Term term in termSet.Terms)
{
if (term.Name == "term name")
{
termID = term.Id.ToString();
break;
}
}
try
{
a = AudMgr.Audiences["some name"];
}
catch (AudienceArgumentException ex)
{
//your exception handling code here
try
{
a = ac.Create(sAudName, sDescription);
}
catch (AudienceDuplicateNameException e)
{
//Your exception handling code here
}
catch (AudienceException e1)
{
//Your exception handling code here
}
}
ArrayList aRules = a.AudienceRules;
if (aRules == null)
{
aRules = new ArrayList();
}
else
{
ruleListNotEmpty = true;
}
try
{
if (ruleListNotEmpty)
{
aRules.Add(new AudienceRuleComponent(null, "AND", null));
}
AudienceRuleComponent r1 = new AudienceRuleComponent("term name", "Contains", termID);
aRules.Add(r1);
Console.WriteLine("PlaceOfWork contains "+ termID);
AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r2);
Console.WriteLine("Rule2");
AudienceRuleComponent r3 = new AudienceRuleComponent("AccountName","Contains", "testuser");
aRules.Add(r3);
a.AudienceRules = aRules;
a.Commit();
}
catch (AudienceException e)
{
Console.WriteLine("Error" + e.Message);
}
}
}
- 回答としてマーク Igor Demjanov 2012年8月23日 8:25

