How Rules in Audiences work#

For a big customer we had to create about 80 different audiences, which should be deployed on multiple server (test, acceptance, production, etc.). Each audience have rules to describe if a user is part of the audience or not. Creating audiences is very easy, just provide a name and description. Rules however need some more attention.

Time to write a script for that! So, first of al, I searched the SDK for some more information about the way rules are used within the audiences. Basically you can add rules programatically like this:

using (SPSite site = new SPSite("http://servername"))
    {
          ServerContext context = ServerContext.GetContext(site);
          AudienceManager AudMgr = new AudienceManager(context);

          AudienceCollection ac = AudMgr.Audiences;
          Audience a = null;
          bool ruleListNotEmpty = false;

          try
          {
               a = AudMgr.Audiences["John and Joe Connection"];
          }
          catch (AudienceArgumentException ex)
           {
               //your exception handling code here
           }

          ArrayList aRules = a.AudienceRules;
          if (aRules == null)
          {
              aRules = new ArrayList();
          }
          else
          {
              ruleListNotEmpty = true;
          }

          try
          {
              //if the rule is not emply, start with a group operator 'AND' to append
             if (ruleListNotEmpty)
             {
                  aRules.Add(new AudienceRuleComponent(null, "AND", null));
             }

             AudienceRuleComponent r0 = new AudienceRuleComponent(null, "(", null);
             aRules.Add(r0);

             AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "John");
             aRules.Add(r1);

             AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
             aRules.Add(r2);

             AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "example.com");
             aRules.Add(r3);

             AudienceRuleComponent r4 = new AudienceRuleComponent(null, ")", null);
             aRules.Add(r4);

             AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);
             aRules.Add(r5);

             AudienceRuleComponent r6 = new AudienceRuleComponent(null, "(", null);
             aRules.Add(r6);

             AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "Joe");
             aRules.Add(r7);

             AudienceRuleComponent r8 = new AudienceRuleComponent(null, "AND", null);
             aRules.Add(r8);

             AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "someexample.com");
             aRules.Add(r9);

             AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
             aRules.Add(r10);
             a.AudienceRules = aRules;
             a.Commit();
           }
           catch (AudienceException e)
           {
               //Your exception handling code here
           }
     }
}

Quite a flexible way to deal with rules. However, there are three parts that you have to deal with:

  • LeftContent (AudienceRuleComponent.LeftContent Property), this is the part where the property is defined. In the example above, this is 'firstname' 'workemail'. etc. Not documented, for a 'member of' operator you need "DL".
  • RuleOperator (AudienceRuleComponent.Operator Property), this is the operator, that could be 'contains' or '='. Again, not documented, for 'member of', this is 'Member of'.
  • RightContent (AudienceRuleComponent.RightContent Property), this is the value, that can be the email address for example. Not documented, for a user it is the full AD path. for example: cn=SecurityGroup,ou=Groups,ou=AnotherGroup,dc=domain,dc=com

This way you can add change and construct audiences easily.

Thursday, August 09, 2007 1:23:29 PM UTC #     | 

 

All content © 2012, Mart Muller
On this page
This site
Calendar
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts