SharePoint でサーバー オブジェクト モデルを使用して、ユーザー プロファイルと組織プロファイルを操作する

SharePointのユーザー プロファイルやユーザー プロファイル プロパティを SharePointのサーバー オブジェクト モデルを使用してプログラムによって作成、取得、変更する方法を説明します。

SharePointのユーザー プロファイルとは

SharePoint では、ユーザー プロファイルは SharePoint ユーザーを表します。 ユーザー プロファイルのプロパティは、ユーザーとプロパティそのものについての情報を表します。 プロパティの例としては、ユーザーのアカウント名や電子メール アドレス、プロパティのデータ型などがあります。 サーバー オブジェクト モデルを使用すると、ユーザー プロファイル、プロファイルのサブタイプ、プロファイルのプロパティをプログラムによって作成、取得、変更できます。

注:

ユーザー プロファイルを操作するための一般的なプログラミング タスクと、タスクの実行に使用する API の詳細については、「 SharePoint でのユーザー プロファイルの操作」を参照してください。

SharePoint サーバー オブジェクト モデルを使用してユーザー プロファイルを処理するための開発環境を設定するための前提条件。

ユーザー プロファイルとユーザー プロファイル プロパティを処理するためにサーバー オブジェクト モデルを使用するコンソール アプリケーションを作成するには、以下のものが必要になります。

  • 現在のユーザー用にプロファイルが作成された SharePoint Server 2013。

  • Visual Studio 2012.

  • ユーザー プロファイル オブジェクトを作成、取得、変更するためのアクセス許可。 (プロファイルの作成と変更には、 ユーザー プロファイルの変更 アクセス許可が必要です)。

SharePointのサーバー オブジェクト モデルを使用してユーザー プロファイルを操作するコンソール アプリケーションを作成する

  1. Visual Studio を開き、[ ファイル]、[ [新規]、[ プロジェクト] を選択します。

  2. [ 新しいプロジェクト] ダイアログ ボックスで、上部のドロップダウン リストから [ .NET Framework 4.5] を選択します。

  3. [ テンプレート] リストで [ Windows] を選択し、さらに [ コンソール アプリケーション] を選択します。

  4. UserProfilesSSOM プロジェクトを指名し、OK ボタンをクリックします。

注: プロジェクトの ビルド プロパティにおいて 32 ビット優先 設定が選択されていないことを確認してください。

  1. 以下のアセンブリに参照を追加します。
  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfiles
  • Microsoft.SharePoint
  • system.web
  1. Program クラスの内容を、以下のシナリオのいずれかのコード例に置き換えます。
  1. コンソール アプリケーションをテストするには、メニュー バーの [ デバッグ] を選択し、[ デバッグ開始] をクリックします。 変更は、サーバーの全体管理のユーザー プロファイル サービス アプリケーションの [プロファイル サービスの管理] ページで確認できます。

コード例: SharePointのサーバー オブジェクト モデルを使用してユーザー プロファイルを作成する

SharePointにおいて、ユーザー プロファイルは SharePoint ユーザーを表します。 プロファイルのタイプおよびサブタイプは、プロファイルを従業員や顧客のようなグループに分類する際に役立ちます。 プロファイルのタイプとサブタイプは、共通のプロファイル プロパティを設定したり、属性をサブタイプのレベルで設定したりするために使用されます。 SharePoint Server には、既定のユーザー プロファイル サブタイプが用意されています。

次のコード例では、既定のユーザー プロファイル サブタイプに関連付けられている UserProfile オブジェクトを作成します。 ユーザー プロファイル プロパティの一部は、Active Directory ドメイン サービス など、ユーザー アカウントを含むディレクトリからインポートされた情報に基づいて自動的に設定されます。 カスタム サブタイプを作成するコード例については、 ProfileSubtype を参照してください。

注:

コードを実行する前に、domain\username とservername プレースホルダーの値を変更します。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username" and "servername" with actual values.
            string newAccountName = "domain\\username";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {

                    // Create a user profile that uses the default user profile
                    // subtype.
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    UserProfile userProfile = userProfileMgr.CreateUserProfile(newAccountName);

                    Console.WriteLine("A profile was created for " + userProfile.DisplayName);
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

コード例: SharePoint サーバー オブジェクト モデルを使用してユーザー プロファイル プロパティを作成する

ユーザー プロファイル プロパティは、ユーザーに関する個人または組織の情報を記述します。 カスタム プロファイル プロパティを作成して、SharePoint プロファイル プロパティの既定のセットに追加することができます。

プロファイル プロパティとその属性は、リンクされたオブジェクトのセット ( CoreProperty オブジェクト、 ProfileTypeProperty オブジェクト、 ProfileSubtypeProperty オブジェクト) で表されます。

次のコード例では、URL データ型 (または必要に応じて、複数値文字列データ型を持つ CoreProperty) を持つ CoreProperty を作成します。 さらに、状態、プライバシー、およびプロパティのその他の設定を定義する ProfileTypeProperty および ProfileTypeProperty を作成しています。 ProfileSubtypeProperty.DefaultPrivacy プロパティは、プロパティおよびその他の個人用サイトのコンテンツの表示/非表示を制御します。 プロファイル プロパティ値として可能なデータ型の完全な一覧については、 PropertyDataType を参照してください。

注:

コードを実行する前に、サーバー名のプレースホルダー値を変更してください。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "servername" with an actual value.
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                    CorePropertyManager corePropMgr = profilePropMgr.GetCoreProperties();

                    // Create a URL property.
                    CoreProperty coreProp = corePropMgr.Create(false);
                    coreProp.Name = "AppsWebsite";
                    coreProp.DisplayName = "Apps site";
                    coreProp.Type = PropertyDataType.URL;
                    coreProp.Length = 100;
                    corePropMgr.Add(coreProp);

                    //// Create a multivalue property.
                    //// To create this property, comment out the previous
                    //// block of code and uncomment this block of code.
                    //CoreProperty coreProp = corePropMgr.Create(false);
                    //coreProp.Name = "PublishedAppsList";
                    //coreProp.DisplayName = "Published apps";
                    //coreProp.Type = PropertyDataType.StringMultiValue;
                    //coreProp.IsMultivalued = true;
                    //coreProp.Length = 100;
                    //corePropMgr.Add(coreProp);

                    // Create a profile type property and make the core property 
                    // visible in the Details section page.
                    ProfileTypePropertyManager typePropMgr = profilePropMgr.GetProfileTypeProperties(ProfileType.User);
                    ProfileTypeProperty typeProp = typePropMgr.Create(coreProp);
                    typeProp.IsVisibleOnViewer = true;
                    typePropMgr.Add(typeProp);

                    // Create a profile subtype property.
                    ProfileSubtypeManager subtypeMgr = ProfileSubtypeManager.Get(serviceContext);
                    ProfileSubtype subtype = subtypeMgr.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties(subtype.Name);
                    ProfileSubtypeProperty subtypeProp = subtypePropMgr.Create(typeProp);
                    subtypeProp.IsUserEditable = true;
                    subtypeProp.DefaultPrivacy = Privacy.Public;
                    subtypeProp.UserOverridePrivacy = true;
                    subtypePropMgr.Add(subtypeProp);

                    Console.WriteLine("The properties were created.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

コード例: SharePoint サーバー オブジェクト モデルを使用してユーザー プロファイルを取得、変更する

次のコード例では、コンテキスト内のすべてのユーザー プロファイルを取得し、ユーザーの DisplayName プロパティの値を変更します。 ほとんどのプロファイル プロパティには UserProfile.Item アクセサーを使用してアクセスします。

注:

コードを実行する前に、domain\username とservername プレースホルダーの値を変更します。


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username" and "servername" with actual values.
            string targetAccountName = "domain\\username";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {

                    // Retrieve and iterate through all of the user profiles in this context.
                    Console.WriteLine("Retrieving user profiles:");
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    IEnumerator userProfiles = userProfileMgr.GetEnumerator();
                    while (userProfiles.MoveNext())
                    {
                        UserProfile userProfile = (UserProfile)userProfiles.Current;
                        Console.WriteLine(userProfile.AccountName);
                    }

                    // Retrieve a specific user profile. Change the value of a user profile property
                    // and save (commit) the change on the server.
                    UserProfile user = userProfileMgr.GetUserProfile(targetAccountName);
                    Console.WriteLine("\\nRetrieving user profile for " + user.DisplayName + ".");
                    user.DisplayName = "Pat";
                    user.Commit();
                    Console.WriteLine("\\nThe user\\'s display name has been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

コード例: SharePoint サーバー オブジェクト モデルを使用してユーザー プロファイル プロパティの属性を取得、変更する

次のコード例では、特定のユーザー プロパティとその属性を表すプロパティのセットを取得し、 CoreProperty.DisplayName 属性、 ProfileTypeProperty.IsVisibleOnViewer 属性、 ProfileSubtypeProperty.PrivacyPolicy 属性を変更します。 これらの変更はプロパティのセットにグローバルに適用されます。 ProfileSubtypeProperty.PrivacyPolicy はユーザーがそのプロパティの値を入力する必要があるかどうかを指定しています。 PrivacyPolicy はユーザー プロファイル プロパティにのみ適用されます。

注:

コードを実行する前に、サーバー名のプレースホルダー値を変更してください。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "servername" with an actual value.
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                try
                {
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");

                    // Retrieve a specific property set (a profile subtype property and
                    // its associated core and profile type properties).
                    // Changing these properties affects all instances of this property set.
                    ProfileSubtypeProperty subtypeProp = subtypePropMgr.GetPropertyByName(PropertyConstants.Title);
                    CoreProperty coreProp = subtypeProp.CoreProperty;
                    ProfileTypeProperty typeProp = subtypeProp.TypeProperty;

                    Console.WriteLine("Property name: " + coreProp.DisplayName);
                    Console.WriteLine("IsVisibleOnViewer = " + typeProp.IsVisibleOnViewer);
                    Console.WriteLine("PrivacyPolicy = " + subtypeProp.PrivacyPolicy);
                    Console.WriteLine("Press Enter to change the values.");
                    Console.Read();

                    // Change attributes on the properties and save (commit) the changes
                    // on the server.
                    coreProp.DisplayName = "Position";
                    coreProp.Commit();
                    typeProp.IsVisibleOnViewer = true;
                    typeProp.Commit();
                    subtypeProp.PrivacyPolicy = PrivacyPolicy.OptOut;
                    subtypeProp.Commit();
                    Console.WriteLine("The property attributes have been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

コード例: SharePoint サーバー オブジェクト モデルを使用してユーザー プロファイルの値を取得、変更する

以下のコード例は、すべての UserProfile 型プロパティを取得し、特定のユーザーのプロパティ値を取得しています。 その後、単一値の PictureUrl プロパティ、および複数値の PastProjects プロパティを変更しています。 プロファイル プロパティ名定数の完全な一覧については、 PropertyConstants を参照してください。

注:

コードを実行する前に、domain\username、、 http://servername/docLib/pic.jpg、および servername プレースホルダーの値を変更します。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
namespace UserProfilesSSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace "domain\\username," "http://servername/docLib/pic.jpg," and "servername" with actual values.
            string accountName = "domain\\username";
            string newPictureUrl = "http://servername/docLib/pic.jpg";
            using (SPSite site = new SPSite("http://servername"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);

                try
                {
                    UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                    ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(serviceContext).ProfilePropertyManager;
                   
                    // Retrieve all properties for the "UserProfile" profile subtype,
                    // and retrieve the property values for a specific user.
                    ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile");
                    UserProfile userProfile = userProfileMgr.GetUserProfile(accountName);
                    IEnumerator<ProfileSubtypeProperty> userProfileSubtypeProperties = subtypePropMgr.GetEnumerator();
                    while (userProfileSubtypeProperties.MoveNext())
                    {
                        string propName = userProfileSubtypeProperties.Current.Name;
                        ProfileValueCollectionBase values = userProfile.GetProfileValueCollection(propName);
                        if (values.Count > 0)
                        {

                            // Handle multivalue properties.
                            foreach (var value in values)
                            {
                                Console.WriteLine(propName + ": " + value.ToString());
                            }
                        }
                        else
                        {
                            Console.WriteLine(propName + ": ");
                        }
                    }
                    Console.WriteLine("Press Enter to change the values.");
                    Console.Read();

                    // Change the value of a single-value user property.
                    userProfile[PropertyConstants.PictureUrl].Value = newPictureUrl;

                    // Add a value to a multivalue user property.
                    userProfile[PropertyConstants.PastProjects].Add((object)"Team Feed App");
                    userProfile[PropertyConstants.PastProjects].Add((object)"Social Ratings View web part");

                    // Save the changes to the server.
                    userProfile.Commit();
                    Console.WriteLine("The property values for the user have been changed.");
                    Console.Read();
                }

                catch (System.Exception e)
                {
                    Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                    Console.Read();
                }
            }
        }
    }
}

関連項目