AudienceOperator AudienceOperator AudienceOperator Class

Definition

Represents the definition of an audience operator.

public ref class AudienceOperator
public class AudienceOperator
Public Class AudienceOperator
Inheritance
AudienceOperatorAudienceOperatorAudienceOperator

Examples

<p>The following code example adds complex rules for an audience called "John and Joe Connection". This example uses AND, OR, and ( and ) operators to combine multiple rules and to group rules. </p>
<p>
  <strong>Note</strong>: If you create an audience with complex rules, you cannot view or edit its properties or delete it by using the user interface. However, you can use the user interface to view its membership.</p>
<p>Replace servername and other strings with actual values before running the code example. Also add the following references in your Microsoft Visual Studio project:</p>
<ul><li>Microsoft.Office.Server</li><li>Microsoft.SharePoint</li><li>System.Web</li></ul>
<pre><code>using System;

using System.Collections.Generic; using System.Text; using Microsoft.SharePoint.Administration; using Microsoft.Office.Server.Audience; using Microsoft.SharePoint; using Microsoft.Office.Server; using System.Web; using System.Collections;

namespace AudienceConsoleApp { class Program { static void Main(string[] args) { try { using (SPSite site = new SPSite("http://<span class="placeholder">servername</span>")) { 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", "&lt;span class="placeholder"&gt;example.com&lt;/span&gt;");
                    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", "&lt;span class="placeholder"&gt;someexample.com&lt;/span&gt;");
                    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
                }
            }
        }

        catch (Exception exception)
        {
            Console.WriteLine(exception.ToString());
            Console.Read();
        }

    }

}

}

Remarks

The AudienceOperator class represents an individual operator that is allowed as part of a rule definition. The list of allowed operators includes "=," ">," ">=," "<," "<=," "Contains," "Reports Under," "<>," "Not contains," "AND," "OR," "(," ")," and "Member of." New operators require the following information: the display name (if operator is used from the user interface), the operator name used by the audience objects, and whether the operator can be applied to a group or whether it is a "NOT" operator. Valid group operations are determined by the AudienceGroupOperation enumeration, and include "AND," "OR," "(," and ")."

This object can be instantiated only by the AudienceManager object. Use the AudienceOperatorList property to retrieve the correct AudienceOperator object.

Properties

bGroupOperator bGroupOperator bGroupOperator

Gets a flag that indicates whether the operator portion of the audience rule is a group operator or a "NOT" operator.

OperatorDisplayName OperatorDisplayName OperatorDisplayName

Gets the display name for the audience operator that appears in the user interface (UI).

OperatorName OperatorName OperatorName

Gets the name of the operator portion of the audience rule.

Extension Methods

ToJson(Object) ToJson(Object) ToJson(Object)

Applies to