AudienceRuleComponent AudienceRuleComponent AudienceRuleComponent Class

Definition

Represents one rule in an audience.

public ref class AudienceRuleComponent
public class AudienceRuleComponent
Public Class AudienceRuleComponent
Inheritance
AudienceRuleComponentAudienceRuleComponentAudienceRuleComponent

Examples

<p>The following code example adds complex rules for an audience named "John and Joe Connection". This example uses <strong>AND</strong>, <strong>OR</strong>, and <strong>( and )</strong> 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 (UI). However, you can use the UI 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 empty, 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();
        }

    }

}

}

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

Namespace AudienceConsoleApp
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            Try
                Using site As New SPSite("http://servername")
                    Dim context As ServerContext = ServerContext.GetContext(site)
                    Dim AudMgr As New AudienceManager(context)

                    Dim ac As AudienceCollection = AudMgr.Audiences
                    Dim a As Audience = Nothing
                    Dim ruleListNotEmpty As Boolean = False

                    Try
                        a = AudMgr.Audiences("John and Joe Connection")
                    Catch ex As AudienceArgumentException
                        ' Your exception handling code here.
                    End Try

                    Dim aRules As ArrayList = a.AudienceRules
                    If aRules Is Nothing Then
                        aRules = New ArrayList()
                    Else
                        ruleListNotEmpty = True
                    End If


                    Try
                        ' If the rule is not empty, start with a group operator 'AND' to append.
                        If ruleListNotEmpty Then
                            aRules.Add(New AudienceRuleComponent(Nothing, "AND", Nothing))
                        End If

                        Dim r0 As New AudienceRuleComponent(Nothing, "(", Nothing)
                        aRules.Add(r0)

                        Dim r1 As New AudienceRuleComponent("FirstName", "Contains", "John")
                        aRules.Add(r1)

                        Dim r2 As New AudienceRuleComponent(Nothing, "AND", Nothing)
                        aRules.Add(r2)

                        Dim r3 As New AudienceRuleComponent("WorkEmail", "Contains", "example.com")
                        aRules.Add(r3)

                        Dim r4 As New AudienceRuleComponent(Nothing, ")", Nothing)
                        aRules.Add(r4)

                        Dim r5 As New AudienceRuleComponent(Nothing, "OR", Nothing)
                        aRules.Add(r5)

                        Dim r6 As New AudienceRuleComponent(Nothing, "(", Nothing)
                        aRules.Add(r6)

                        Dim r7 As New AudienceRuleComponent("FirstName", "Contains", "Joe")
                        aRules.Add(r7)

                        Dim r8 As New AudienceRuleComponent(Nothing, "AND", Nothing)
                        aRules.Add(r8)

                        Dim r9 As New AudienceRuleComponent("WorkEmail", "Contains", "someexample.com")
                        aRules.Add(r9)

                        Dim r10 As New AudienceRuleComponent(Nothing, ")", Nothing)
                        aRules.Add(r10)
                        a.AudienceRules = aRules
                        a.Commit()
                    Catch e As AudienceException
                        ' Your exception handling code here.
                    End Try
                End Using

            Catch exception As Exception
                Console.WriteLine(exception.ToString())
                Console.Read()
            End Try

        End Sub

    End Class
End Namespace

Remarks

An audience can be composed of one or more rule components. Each rule is connected by an operator, such as AND or OR. 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 to specify rule values.

Constructors

AudienceRuleComponent() AudienceRuleComponent() AudienceRuleComponent()

Initializes a new instance of the AudienceRuleComponent class.

AudienceRuleComponent(String, String, String) AudienceRuleComponent(String, String, String) AudienceRuleComponent(String, String, String)

Initializes a new instance of the AudienceRuleComponent class with the operand, operator, and value for an audience rule.

Properties

LeftContent LeftContent LeftContent
Operator Operator Operator
RightContent RightContent RightContent

Extension Methods

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

Applies to