SharePoint で JavaScript オブジェクト モデルを使用してユーザーをフォローする

SharePoint JavaScript オブジェクト モデルを使用して、ユーザー フォロー機能を操作する方法を説明します。

SharePoint のユーザー フォロー機能を使用する理由

SharePoint では、ユーザー フォロー機能により、ユーザーが相互のつながりを維持できるように支援します。 たとえば、ユーザーが別のユーザーをフォローすると、フォローされたユーザーの投稿やアクティビティは、フォローしたユーザーのニュースフィードに表示されます。 ユーザーが気に掛けている別のユーザーに焦点を合わせるユーザー フォロー機能を使用すると、アプリやソリューションの社会性が向上します。 JavaScript オブジェクト モデルでは、フォローしているユーザーは SocialActor オブジェクトによって表されます。 JavaScript オブジェクト モデルでユーザー フォローのコア タスクを実行する場合は、SocialFollowingManager オブジェクトを使用します。 この記事では、ユーザー フォロー機能を操作するための JavaScript オブジェクト モデルの使用方法について説明します。

注:

SocialFollowingManager は、ユーザーとコンテンツのフォローに使用が推奨される API です。 ただし、PeopleManager オブジェクトには、amIFollowedBy メソッドや他のユーザーのフォロー状態を取得するメソッドなど、ユーザーをフォローするための追加的な機能も含まれます。

SharePoint JavaScript オブジェクト モデルを使用してユーザー フォロー機能を操作するための開発環境を設定する際の前提条件

ユーザー フォロー機能の操作に JavaScript オブジェクト モデルを使用するファーム ソリューションを作成するには、次に示す項目が必要になります。

  • 個人用サイトが構成されていて、現在のユーザーおよび対象ユーザーのユーザー プロファイルと個人用サイトが作成されている SharePoint
  • Visual Studio 2012
  • Office Developer Tools for Visual Studio 2012
  • ログオン ユーザー用に、User Profile Service アプリケーションの [ フル コントロール] アクセス許可
  • ログオン ユーザー用のローカル管理アクセス許可

Visual Studio 2012 でファーム ソリューションおよびアプリケーション ページを作成する

  1. Visual Studio を管理者として実行し、[ ファイル]、[ 新規作成]、[ プロジェクト] の順に選択します。

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

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

  4. プロジェクトの名前を FollowPeopleJSOM にして、[OK] ボタンをクリックします。

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

  6. [ ソリューション エクスプローラー] で、[ FollowPeopleJSOM] プロジェクトのショートカット メニューを開き、SharePoint のマップされた "Layouts" フォルダーを追加します。

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

    注:

    この記事のコード例では、ページ マークアップにカスタム コードを定義していますが、Visual Studio によって作成されるページの分離コードは使用しません。

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

  9. FollowPeople.aspx ファイルのマークアップで、"Main" asp:Content タグの間に次のコードを貼り付けます。 このコードによって、コントロールおよびスクリプト参照が定義されます。

    <span id="followResults"></span><br/><br />
    <button id="sendRequest" type="button"></button><br/>
    <span id="message" style="color: #FF0000;"></span>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>
    <SharePoint:ScriptLink name="SP.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />
    <SharePoint:ScriptLink name="SP.UserProfiles.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />
    <SharePoint:FormDigest id="FormDigest" runat="server"/>
    <script type="text/javascript">
        // Replace this comment with the code for your scenario.
    </script>
    

    注:

    "フォロワーを取得してフォローしているユーザー" の例では、ボタン コントロールやフォーム ダイジェスト コントロールは使用されません。これは、サーバー コンテンツを更新する操作にのみ必要です。 フォーム ダイジェストは、セキュリティ検証に使用されるメッセージ ダイジェストを生成します。

  10. タグ間のコメントを script 、次のいずれかのシナリオのコード例に置き換えます。

  11. ソリューションをテストするには、[ デバッグ] メニューの [ デバッグ開始] をクリックします。

コード例: SharePoint JavaScript オブジェクト モデルを使用してユーザーのフォローを開始または停止する

次のコード例では、現在のユーザーに対象ユーザーのフォローを開始または停止させます。 このコード例では、以下の方法を示しています。

  • isFollowed メソッドを使用して、現在のユーザーが対象ユーザーをフォローしているかどうかを確認します。
  • getFollowedCount メソッドを使用して、現在のユーザーがフォローしている人数を取得します。
  • follow メソッドを使用して、対象ユーザーのフォローを開始します。
  • stopFollowing メソッドを使用して、対象ユーザーのフォローを停止します。

注:

ファーム ソリューションおよびアプリケーション ページを作成する」の手順で追加した script タグの間に以下のコードを貼り付けます。 Then, change the placeholder value for the targetUser variable before you run the code.

// Replace the placeholder value with the account name of the target user.
var targetUser = 'domain\\userName';

var clientContext;
var followingManager;
var actorInfo;
var isFollowed;
var followedCount;

// Ensure that the SP.UserProfiles.js file is loaded before running your code.
$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(getFollowingStatus, 'SP.UserProfiles.js');
});

// Get the Following status of the current user.
function getFollowingStatus() {

    // Get the current client context.
    clientContext = SP.ClientContext.get_current();

    // Get the SocialFeedManager instance.
    followingManager = new SP.Social.SocialFollowingManager(clientContext);

    // Create a SocialActorInfo object to represent the target user.
    actorInfo = new SP.Social.SocialActorInfo();
    actorInfo.set_accountName(targetUser);

    // Find out whether the current user is following the target user.
    isFollowed = followingManager.isFollowed(actorInfo);
    followedCount = followingManager.getFollowedCount(1);

    // Get the information from the server.
    clientContext.executeQueryAsync(showFollowingStatus, requestFailed)
}

// Show the Following status of the current user.
function showFollowingStatus() {
    var results = '';
    results += 'Is the current user following the target user? ' + isFollowed.get_value();
    results += '<br/>Total count of followed people: ' + followedCount.get_value();
    $('#followResults').html(results);

    // Initialize the button for this example.
    $('#sendRequest').click(
        function () {
            $('#message').empty();
            toggleFollowingStatus();
        });
    $('#sendRequest').text('Toggle following status');
}

// Follow or stop following the target user.
function toggleFollowingStatus() {
    if (isFollowed.get_value() === false) {
        followingManager.follow(actorInfo);
    }
    else if (isFollowed.get_value() === true) {
        followingManager.stopFollowing(actorInfo);
    }
    clientContext.executeQueryAsync(getFollowingStatus, requestFailed);
}

// Failure callback.
function requestFailed(sender, args) {
    $('#message').html('Error: ' + args.get_message());
}

コード例: SharePoint JavaScript オブジェクト モデルを使用してフォロワーとフォロー対象ユーザーを取得する

次のコード例では、現在のユーザーがフォローしているユーザーを取得し、現在のユーザーによるフォロー対象のユーザーを取得します。 以下にその方法を説明します。

  • getFollowed メソッドを使用して、現在のユーザーがフォローしているユーザーを取得します。
  • getFollowers メソッドを使用し、User アクター タイプを表す 1 を渡して、現在のユーザーをフォローしているユーザーを取得します。
  • ユーザーのグループを反復処理して、それぞれのユーザーの表示名、個人用サイトの URL、および画像の URL を取得します。

注:

ファーム ソリューションとアプリケーション ページscript作成手順で追加したタグの間に、次のコードを貼り付けます。

var followed;
var followers;

// Ensure that the SP.UserProfiles.js file is loaded before running your code.
$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(getFollowedAndFollowers, 'SP.UserProfiles.js');

    // Hide the button for this example.
    $('#sendRequest').hide();
});

// Get the Following status of the current user.
function getFollowedAndFollowers() {

    // Get the current client context.
    var clientContext = SP.ClientContext.get_current();

    // Get the SocialFeedManager instance.
    var followingManager = new SP.Social.SocialFollowingManager(clientContext);

    // Get followed people and followers.
    followers = followingManager.getFollowers();
    followed = followingManager.getFollowed(1);

    // Send the request to the server.
    clientContext.executeQueryAsync(showFollowedAndFollowers, requestFailed)
}

// Show the Following status of the current user.
function showFollowedAndFollowers() {
    var results = 'The current user is following ' + followed.length + ' people: <br/>';

    for (var i = 0; i < followed.length; i++) {
        var user = followed[i];
        var name = user.get_name();
        var personalSiteUri = user.get_personalSiteUri();
        var pictureUri = user.get_imageUri();

        results += '<br/>' + name + '<br/>' + personalSiteUri + '<br/>' + pictureUri + '<br/>';
    }

    results += '<br/>The current user is followed by ' + followers.length + ' people: <br/>';

    for (var i = 0; i < followers.length; i++) {
        var user = followers[i];
        var name = user.get_name();
        var personalSiteUri = user.get_personalSiteUri();
        var pictureUri = user.get_imageUri();

        results += '<br/>' + name + '<br/>' + personalSiteUri + '<br/>' + pictureUri + '<br/>';
    }
    $('#followResults').html(results);
}

// Failure callback.
function requestFailed(sender, args) {
    $('#message').html('Error: ' + args.get_message());
}

関連項目