Okay, so you’ve created your audience and used my gl-addaudiencerule command to add some complex rules to the audience. Three months later you’ve forgotten what those rules are and need to see them again – unfortunately you can’t do that via the browser, so what do you do? Simple, you run gl-enumaudiencerules, another command I’ve created to help manage audiences via STSADM.

This command is really simple so I’ll be brief – I simply loop through all the rules associated with an audience and display back an XML structure representing those rules. The nice thing about this is that you can use it to help build your rules to assign using the gl-addaudiencerule command – you’d create all the rules you need via the browser, with no groupings, and then use this command to get the XML and then simply add your grouping elements into the XML – this way you’re sure the field names are correct.

Here’s a snippet of the code:

 1/// <summary>
 2/// Returns an XML structure containing all the rules associated with the audience.
 3/// </summary>
 4/// <param name="sspName">Name of the SSP.</param>
 5/// <param name="audienceName">Name of the audience.</param>
 6/// <param name="includeAllAttributes">if set to <c>true</c> [include all attributes].</param>
 7/// <returns></returns>
 8private string EnumRules(string sspName, string audienceName, bool includeAllAttributes)
 9{
10    ServerContext context;
11    if (string.IsNullOrEmpty(sspName))
12        context = ServerContext.Default;
13    else
14        context = ServerContext.GetContext(sspName);
15 
16    AudienceManager manager = new AudienceManager(context);
17 
18    if (!manager.Audiences.AudienceExist(audienceName))
19    {
20        throw new SPException("Audience name does not exist");
21    }
22 
23    Audience audience = manager.Audiences[audienceName];
24 
25    ArrayList audienceRules = audience.AudienceRules;
26 
27    if (audienceRules == null || audienceRules.Count == 0)
28        return "The audience contains no rules.";
29 
30    string rulesXml = "<rules>\r\n";
31    foreach (AudienceRuleComponent rule in audienceRules)
32    {
33        if (includeAllAttributes)
34        {
35            rulesXml += string.Format("\t<rule field=\"{1}\" op=\"{0}\" value=\"{2}\" />\r\n", rule.Operator, rule.LeftContent, rule.RightContent);
36        }
37        else
38        {
39            switch (rule.Operator.ToLowerInvariant())
40            {
41                case "=":
42                case ">":
43                case ">=":
44                case "<":
45                case "<=":
46                case "contains":
47                case "<>":
48                case "not contains":
49                    rulesXml += string.Format("\t<rule field=\"{1}\" op=\"{0}\" value=\"{2}\" />\r\n", rule.Operator, rule.LeftContent, rule.RightContent);
50                    break;
51                case "reports under":
52                case "member of":
53                    rulesXml += string.Format("\t<rule op=\"{0}\" value=\"{1}\" />\r\n", rule.Operator, rule.RightContent);
54                    break;
55                case "and":
56                case "or":
57                case "(":
58                case ")":
59                    rulesXml += string.Format("\t<rule op=\"{0}\" />\r\n", rule.Operator);
60                    break;
61            }
62        }
63    }
64    rulesXml += "</rules>";
65    return rulesXml;
66}

The help for the command is shown below:

C:\>stsadm -help gl-enumaudiencerules

stsadm -o gl-enumaudiencerules


Outputs the rules for an audience.

Parameters:
        -name <audience name>
        [-ssp <SSP name>]
        [-explicit (shows field and value attributes for every rule)]

The following table summarizes the command and its various parameters:

Command NameAvailabilityBuild Date
gl-enumaudiencerulesMOSS 2007Released: 8/6/2008
Parameter NameShort FormRequiredDescriptionExample Usage
namenYesThe name of the audience-name "IT Department", -n "IT Department"
sspNoThe name of the SSP that the audience is associated with. If not specified then the default SSP is used.-ssp SSP1
explicitexNoIf specified then all attributes will be returned in the XML – this is only really useful if you wish to see the field names of the special operators, “member of” and “reports under”.-explicit, -ex

The following is an example of how to display the rules associated with an audience:

stsadm -o gl-enumaudiencerules -name "IT Department"

The following is an example of the XML that would be displayed:

1<rules>
2    <rule op="(" />
3    <rule field="Department" op="=" value="IT" />
4    <rule op="or" />
5    <rule op="reports under" value="domain\glapointe" />
6    <rule op=")" />
7    <rule op="and" />
8    <rule field="IsContractor" op="=" value="false" />
9</rules>