AND、OR、(、) の各演算子を使用して対象ユーザーに複雑なルールを追加する

最終更新日: 2010年1月27日

適用対象: SharePoint Server 2010

ユーザー インターフェイスで、対象ユーザー用のルールを定義するためには、すべてのルールを満たすユーザーを含めるか、ルールのいずれかを満たすユーザーを含めるかの 2 つのオプションしかありません。一定の状況ではこれで十分ですが、複雑なルールを満たすユーザーを含める必要がある場合も多いでしょう。複雑なルールでは AND と OR 演算子を使用します。また、ルールをグループ化してそれぞれ異なる意味を与えるために、かっこも使用します。

複雑なルールを作成する必要がある場合、Audience オブジェクト モデルを使用できます。オブジェクト モデルでは、最大 3 レベルのかっこのネストをサポートしています。

以下のコード例では、"John and Joe Connection" という対象ユーザー用の複雑なルールを追加します。この例では、AND、OR、(、) の各演算子を使用して、複数のルールを組み合わせ、ルールをグループ化します。

注意

複雑なルールを持つ対象ユーザーを作成した場合、ユーザー インターフェイスを使用してプロパティを表示または編集したり、対象ユーザーを削除したりすることはできません。ただし、ユーザー インターフェイスを使用してメンバシップを表示できます。

コード例を実行する前に、servername およびその他の文字列を実際の値に置き換えてください。また、Microsoft Visual Studio プロジェクトで以下の参照を追加してください。

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • 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"))
                {
                    SPServiceContext context = SPServiceContext.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();
            }

        }

    }
}

関連項目

その他の技術情報

対象ユーザーを使用してコンテンツを設定する