SharePoint で JavaScript オブジェクトモデルを使用して、ユーザー プロファイル プロパティを取得する

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

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

ユーザー プロパティとユーザー プロファイル プロパティは、表示名、電子メール、肩書き、その他のビジネス情報や個人情報など、SharePoint ユーザーに関する情報を提供します。 クライアント側の API では、PersonProperties オブジェクトとその userProfileProperties プロパティから、これらのプロパティにアクセスします。 userProfileProperties プロパティにはすべてのユーザー プロファイル プロパティが含まれていますが、PersonProperties オブジェクトには一般的に使われるプロパティ (accountNamedisplayNameemail など) が含まれていて、より簡単にアクセスできます。

PeopleManager オブジェクトには、JavaScript オブジェクト モデルを使用してユーザー プロパティとユーザー プロファイル プロパティを取得するために使用できる次のメソッドが含まれています。

クライアント API のユーザー プロファイル プロパティは読み取り専用です (プロファイル画像を除き、 PeopleManager.setMyProfilePicture メソッドを使用して変更できます)。 その他のユーザー プロファイル プロパティを変更する場合は、サーバー オブジェクト モデルを使用する必要があります。 ユーザー プロファイルの操作の詳細については、「 SharePoint でのユーザー プロファイルの操作」を参照してください。

注:

クライアント側の UserProfile オブジェクトには、サーバー側のバージョンにあるユーザー プロパティのすべては含まれていません。 ただし、クライアント側バージョンは、現在のユーザーの個人サイトを作成するメソッドを提供します。 それを取得するには、ProfileLoader.getUserProfile メソッドを使用します。

SharePoint JavaScript オブジェクト モデルを使用してユーザー プロパティを取得するために開発環境をセットアップする際の前提条件

JavaScript オブジェクト モデルを使用してユーザー プロパティを取得するアプリケーション ページを作成するには、以下のものが必要となります。

  • 現在のユーザーとターゲット ユーザーに対して作成されたプロファイルがある SharePoint

  • Visual Studio 2012

  • Office Developer Tools for Visual Studio 2013

  • 現在のユーザーのユーザー プロファイル サービス アプリケーションにアクセスするための フル コントロール接続権限

Visual Studio 2012 でのアプリケーション ページの作成

  1. SharePoint を実行しているサーバーで Visual Studio を開き、[ファイル][新規作成][プロジェクト] の順に選択します。

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

  3. [ テンプレート] リストで、[ Office/SharePoint] を展開し、[ SharePoint ソリューション] カテゴリを選択し、[ SharePoint プロジェクト] テンプレートを選択します。

  4. プロジェクトに「UserProfilesJSOM」という名前を付け、[ OK] ボタンを選択します。

  5. [ SharePoint カスタマイズ ウィザード] ダイアログ ボックスに、ターゲットの SharePoint サイトの URL を入力し、[ ファーム ソリューションとして配置する]、[ 完了] ボタンの順に選択します。

  6. [ ソリューション エクスプローラー] で 「UserProfilesJSOM」プロジェクトのショートカット メニューを開き、SharePoint「レイアウト」マップ フォルダーを追加します。

  7. レイアウト フォルダーで、UserProfilesJSOM フォルダーのショートカット メニューを開いて、UserProfiles.aspx という名前の新しい SharePoint アプリケーション ページを追加します。

    注: この記事のコード例は、ページ マークアップでカスタム コードを定義していますが、ページ用に Visual Studio が生成する分離コード クラス ファイルは使用しません。

  8. UserProfiles.aspx ページのショートカット メニューを開いて、[スタートアップ アイテムとして設定] を選択します。

  9. UserProfiles.aspx ページのマークアップでは、"Main"の asp:Content タグ内に次のコードを貼り付けます。 このコードは、クエリ結果を表示する span コントロール、SharePoint JavaScript クラス ライブラリ ファイルを参照する SharePoint:ScriptLink コントロール、カスタム ロジックを含めるための script タグを追加します。

<span id="results"></span><br />
<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server"
    ondemand="false" localizable="false" loadafterui="true" />
<script type="text/javascript">
    // Replace this comment with the code for your scenario.
</script>
  1. ユーザー プロファイル プロパティを取得するロジックを追加するには、 script タグに囲まれたコメントを次のシナリオのいずれかのコード例と置き換えます。
  1. アプリケーション ページをテストするには、メニューバーで [ デバッグ]、[ デバッグ開始] の順に選択します。 web.config ファイルを変更するように求められた場合は、[ OK] ボタンをクリックします。

コード例: SharePoint JavaScript オブジェクト モデルで、PersonProperties オブジェクトとその userProfileProperties プロパティからユーザー プロファイル プロパティを取得する

次のコード例は、PersonProperties オブジェクトとその userProfileProperties プロパティをクエリすることで、ターゲット ユーザーのユーザー プロファイル プロパティを取得する方法を示しています。 この例では以下の方法を示します。

注:

アプリケーション ページの作成」の手順で UserProfiles.aspx に追加した script タグの間に次のコードを貼り付けます。 コードを実行する前に、domainName\\userName プレースホルダー値を置換してください。 (このコード例では、分離コード クラス ファイルは使用しません。)


var personProperties;

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

function getUserProperties() {

    // Replace the placeholder value with the target user's credentials.
    var targetUser = "domainName\\userName";

    // Get the current client context and PeopleManager instance.
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Get user properties for the target user.
    // To get the PersonProperties object for the current user, use the
    // getMyProperties method.
    personProperties = peopleManager.getPropertiesFor(targetUser);

    // Load the PersonProperties object and send the request.
    clientContext.load(personProperties);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {

    // Get a property directly from the PersonProperties object.
    var messageText = " \\"DisplayName\\" property is "
        + personProperties.get_displayName();

    // Get a property from the UserProfileProperties property.
    messageText += "<br />\\"Department\\" property is "
        + personProperties.get_userProfileProperties()['Department'];
    $get("results").innerHTML = messageText;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}

コード例: SharePoint JavaScript オブジェクト モデルの getUserProfilePropertiesFor メソッドを使用してユーザー プロファイル プロパティのセットを取得する

次のコード例では、getUserProfilePropertiesFor メソッドを使用して、ターゲット ユーザーの指定された一連のユーザー プロファイル プロパティの値を取得します。 この例では、以下の方法を示します。

注:

アプリケーション ページの作成」の手順で UserProfiles.aspx に追加した script タグの間に次のコードを貼り付けます。 コードを実行する前に、domainName\\\\userName プレースホルダー値を置換してください。 (このコード例では、分離コード クラス ファイルは使用しません。)


var userProfileProperties;

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');

function getUserProperties() {

    // Replace the placeholder value with the target user's credentials.
    var targetUser = "domainName\\\\userName";

    // Get the current client context and PeopleManager instance.
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // Specify the properties to retrieve and target user for the 
    // UserProfilePropertiesForUser object.
    var profilePropertyNames = ["PreferredName", "Department"];
    var userProfilePropertiesForUser = 
        new SP.UserProfiles.UserProfilePropertiesForUser(
            clientContext,
            targetUser,
            profilePropertyNames);

    // Get user profile properties for the target user.
    // To get the value for only one user profile property, use the
    // getUserProfilePropertyFor method.
    userProfileProperties = peopleManager.getUserProfilePropertiesFor(
        userProfilePropertiesForUser);

    // Load the UserProfilePropertiesForUser object and send the request.
    clientContext.load(userProfilePropertiesForUser);
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
}

// This function runs if the executeQueryAsync call succeeds.
function onRequestSuccess() {
    var messageText = "\\"PreferredName\\" property is " 
        + userProfileProperties[0];
    messageText += "<br />\\"Department\\" property is " 
        + userProfileProperties[1];
    $get("results").innerHTML = messageText;
}

// This function runs if the executeQueryAsync call fails.
function onRequestFail(sender, args) {
    $get("results").innerHTML = "Error: " + args.get_message();
}

関連項目