ユーザーが Web でメンバになっている対象ユーザーをすべて特定する

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

適用対象: SharePoint Server 2010

この例では、Audience オブジェクト モデルを使用して、ユーザーが特定の Web でメンバになっているすべての対象ユーザーを特定する方法を示します。結果には、Microsoft Exchange 配布リスト (DL)、SharePoint グループ、グローバル対象ユーザー、およびルールに基づいた対象ユーザーがすべて含まれることに注意してください。

AudienceType 列挙は、ユーザーが所属する対象ユーザーの種類を表します。列挙値は次のいずれかです。

  • DL

  • Global

  • SharePointGroup

この例を使用する前に、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);
                    SPWeb web = site.AllWebs[0];

                    try
                    {
                        ArrayList audienceIDNames = AudMgr.GetUserAudienceIDs("domainname\\username", true, web);
                        ArrayList audienceNames = new ArrayList();


                        for (int i = 0; i < audienceIDNames.Count; i++)
                        {
                            AudienceNameID arrTemp = (AudienceNameID)audienceIDNames[i];
                            audienceNames.Add(arrTemp.AudienceName);
                            Console.WriteLine(audienceNames[i].ToString());
                        }
                        Console.Read();

                    }
                    catch (AudienceException e)
                    {
                        //Your exception handling code here
                    }
                }

            }


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

        }
    }

}

関連項目

その他の技術情報

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