Share via


AudienceRuleComponent Class

Represents one rule in an audience.

Inheritance Hierarchy

System.Object
  Microsoft.Office.Server.Audience.AudienceRuleComponent

Namespace:  Microsoft.Office.Server.Audience
Assembly:  Microsoft.Office.Server (in Microsoft.Office.Server.dll)

Syntax

'Declaration
<SerializableAttribute> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class AudienceRuleComponent
'Usage
Dim instance As AudienceRuleComponent
[SerializableAttribute]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class AudienceRuleComponent

Remarks

An audience can be composed of one or more rule components. Each rule is connected by an operator, such as AND, OR, and so on. Each rule component includes three parts: operand, operator, and value.

The AudienceRuleComponent class has public string fields to set the left content, operator and right content values when creating a new audience rule for an audience. For all audience rules, a rule that is created against a property of type DateTime should have the value specified in an invariant culture format. For all other property types, such as int, long, double, and float, the site format is used for specifying rule values.

Examples

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.

Note

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.

Replace servername and other strings with actual values before running the code example. Also add the following references in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.SharePoint

  • System.Web

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("https://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
                    }
                }
            }

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

        }

    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

AudienceRuleComponent Members

Microsoft.Office.Server.Audience Namespace