Creating Audience Rules via the Object Model
SharePoint Server (MOSS) was designed as a platform for building applications upon. Using the SharePoint Server Object Model (OM) exposes many different and often more complicated operations than what can be done with the UI only.
An example of this is with Audience creation. Audiences are an organizational type concept that is associated with the Shared Service Provider (SSP). Audiences can be built up using rules that are exposed within the Admin UI for such properties as Account Name or Title. In this way you may be able to create audiences that comprise of user profiles that all have a property in common and can then use this compiled audience for such things as targeting content.
The Audience OM allows the creation of audiences that can be more complicated in their evaluation. For example, the Audience OM allows the groupings of audience rules which will result in compiled audiences based on a combination of AND/OR clauses.
// Create thet test audience.
this.testAudience = this.audienceManager.Audiences.Create(
TestStringGenerator.GetString(MaxAudienceNameLength),
TestStringGenerator.GetString(
MaxAudienceDescriptionLength));
this.testAudience.GroupOperation =
AudienceGroupOperation.AUDIENCE_OR_OPERATION;
// Add the desired rule to the test audience.
ArrayList audienceRules = new ArrayList();
AudienceRuleComponent r1 = new AudienceRuleComponent("AccountName", "Contains", this.DLTestAccountName1);
audienceRules.Add(r1);
AudienceRuleComponent r2 = new AudienceRuleComponent(null, "OR", null);
audienceRules.Add(r2);
AudienceRuleComponent r3 = new AudienceRuleComponent("AccountName", "Contains", this.DLTestAccountName2);
audienceRules.Add(r3);
this.testAudience.AudienceRules = audienceRules;
this.testAudience.Commit();
From the code above you can see that:
this.testAudience.GroupOperation =
AudienceGroupOperation.AUDIENCE_OR_OPERATION;
Sets the group operation for the audience similar to the Audience Admin UI where you can select
But with via OM you can specify groupings by using parentheses () with this:
new AudienceRuleComponent(null, "(", null);
<some audience rules>
new AudienceRuleComponent(null, ")", null);
so that now you can represent an expression like this:
(AccountName contains DLTestAccountName1 OR AccountName contains DLTestAccountName1)
AND
(Title contains TestTitle1 OR Title contains TestTitle2)