none
Creating rules for audiences RRS feed

  • Question

  • 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.

    Tuesday, July 31, 2012 12:34 PM

Answers

  • 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);

                    }

                }

            }

    • Marked as answer by Igor Demjanov Thursday, August 23, 2012 8:25 AM
    Thursday, August 23, 2012 8:24 AM

All replies

  • Maybe someone have seen something similar somewhere? I would appreciate your help.

    Regards,

    Igor.

    Monday, August 13, 2012 6:32 PM
  • 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);

                    }

                }

            }

    • Marked as answer by Igor Demjanov Thursday, August 23, 2012 8:25 AM
    Thursday, August 23, 2012 8:24 AM