AudienceRuleComponent 类

代表在访问群体中的一条规则。

继承层次结构

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

命名空间:  Microsoft.Office.Server.Audience
程序集:  Microsoft.Office.Server.UserProfiles(位于 Microsoft.Office.Server.UserProfiles.dll 中)

语法

声明
<SerializableAttribute> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class AudienceRuleComponent
用法
Dim instance As AudienceRuleComponent
[SerializableAttribute]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class AudienceRuleComponent

备注

观众可由一个或多个规则组件。每个规则由一个运算符,例如AND或OR连接。每个规则组件包括三个部分: 操作数、 运算符和值。

AudienceRuleComponent类有公用字符串字段,若要为用户创建新的访问群体规则时设置左侧的内容、 运算符和权利内容的值。所有访问群体规则,针对类型DateTime的属性创建的规则应有指定固定区域性的格式的值。对于所有其他属性类型,如int、 long、 double和float,网站格式用于指定规则的值。

示例

下面的代码示例将名为"约翰和乔连接"访问群体添加复杂的规则。此示例使用AND, OR和( and )运算符来组合多个规则和规则组。

备注

如果使用复杂的规则创建访问群体时,不能查看或编辑其属性或将其删除通过使用用户界面 (UI)。但是,您可以使用用户界面以查看其成员身份。

请先将 servername 和其他字符串替换为实际值,然后再运行代码示例。还要在 Microsoft Visual Studio 项目中添加以下引用:

  • 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 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", "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();
            }

        }

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

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

AudienceRuleComponent 成员

Microsoft.Office.Server.Audience 命名空间