AND および OR 演算子を使用して対象ユーザーへの簡易ルールを追加する

最終更新日: 2010年2月2日

適用対象: SharePoint Server 2010

対象ユーザーを作成する際、ルールを定義するためにユーザー インターフェイスで用意されているオプションには、「すべてのルールを満たすユーザーを含める」または「いずれかのルールを満たすユーザーを含める」の 2 つしかありません。また、このユーザー インターフェイスを使用しているときは、ルールは 6 つまでに制限されています。ほとんどの場合はこれで十分ですが、7 つ以上のルールが必要になることもあります。

このような場合には、対象ユーザー オブジェクト モデルを使用することができます。対象ユーザー オブジェクト モデルを使用すれば、対象ユーザーに 7 つ以上のルールを作成することができます。以下のコード例では、"John Connection" という対象ユーザーに簡易ルールを追加します。この例では、AND 演算子を使用して複数のルールを組み合わせます。そのメンバに対して 1 つのルールのみを真にするには、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 Connection"];
                    }
                    catch (AudienceArgumentException ex)
                    {
                        //your exception handling code here
                    }

                    ArrayList aRules = a.AudienceRules;
                    if (aRules == null)
                    {
                        aRules = new ArrayList();
                    }
                    else
                    {
                        ruleListNotEmpty = true;
                    }


                    try
                    {
                        if (ruleListNotEmpty)
                        {
                            aRules.Add(new AudienceRuleComponent(null, "AND", null));
                        }


                        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);
                        a.AudienceRules = aRules;
                        a.Commit();
                    }
                    catch (AudienceException e)
                    {
                        //Your exception handling code here
                    }
                }

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

        }
    }


}

関連項目

その他の技術情報

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