SharePoint で .NET クライアント オブジェクト モデルを使用してユーザーをフォローする

SharePoint .NET クライアント オブジェクト モデルを使用して、ユーザー フォロー機能を操作する方法について説明します。

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

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

注:

SocialFollowingManager は、ユーザーとコンテンツをフォローするためのコア機能を統合するため、重点的に取り組んでいます。 ただし、PeopleManager オブジェクトには、AmIFollowedBy(String) メソッドや他のユーザーのフォロー状態を取得するメソッドなど、ユーザーをフォローするための追加機能が含まれています。

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

.NET クライアント オブジェクト モデルを使用してユーザー フォロー機能を操作するコンソール アプリケーションを作成するには、次のものが必要です。

  • 個人用サイトが構成されていて、現在のユーザーおよび対象ユーザーのユーザー プロファイルと個人用サイトが作成されている SharePoint

  • Visual Studio 2012

  • ログオンしているユーザーの User Profile Service アプリケーションに対するフル コントロールのアクセス許可

注:

SharePoint を実行しているコンピューターで開発していない場合は、SharePoint クライアント アセンブリを含む SharePoint クライアント コンポーネント のダウンロードを取得します。

Visual Studio 2012 でのコンソール アプリケーションの作成

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

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

  3. [ テンプレート] ボックスの一覧で、[ Windows] を選択し、[ コンソール アプリケーション] テンプレートを選択します。

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

  5. 次のアセンブリへの参照を追加します。

  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.ClientRuntime
  • Microsoft.SharePoint.Client.UserProfiles
  1. Program クラスの内容を次のどちらかのシナリオのコード例に置き換えます。
  1. コンソール アプリケーションをテストするには、メニュー バーの [ デバッグ] を選択し、[ デバッグ開始] をクリックします。

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

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

  • IsFollowed メソッドを使用して、現在のユーザーがターゲット ユーザーをフォローしているかどうかを確認します。

  • GetFollowedCount メソッドを使用して、現在のユーザーがフォローしているユーザーの数を取得します。

  • Follow メソッドを使用して、ターゲット ユーザーの フォロー を開始します。

  • StopFollowing メソッドを使用して、ターゲット ユーザーのフォローを停止します。

このコード例では、Follow メソッドによって返される SocialFollowResult オブジェクトを使用して、ターゲット ユーザーのフォローを開始または停止するかどうかを決定します。

注:

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace FollowPeopleCSOM
{
    class Program
    {
        static ClientContext clientContext;
        static SocialFollowingManager followingManager;
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the target
            // server URL and target user.
            const string serverUrl = "http://serverName";
            const string targetUser = "domainName\\\\userName";

            // Get the client context.
            clientContext = new ClientContext(serverUrl);

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

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

            // Find out whether the current user is following the target user.
            ClientResult<bool> isFollowed = followingManager.IsFollowed(actorInfo);

            // Get the information from the server.
            clientContext.ExecuteQuery();

            Console.WriteLine("Was the current user following the target user? {0}\\n", isFollowed.Value);
            Console.Write("Initial count: ");

            // Get the current count of followed people.
            WriteFollowedCount();

            // Try to follow the target user. If the result is OK, then
            // the request succeeded.
            ClientResult<SocialFollowResult> result = followingManager.Follow(actorInfo);
            clientContext.ExecuteQuery();

            // If the result is AlreadyFollowing, then stop following 
            // the target user.
            if (result.Value == SocialFollowResult.AlreadyFollowing)
            {
                followingManager.StopFollowing(actorInfo);
                clientContext.ExecuteQuery();
            }

            // Handle other SocialFollowResult return values.
            else if (result.Value == SocialFollowResult.LimitReached
                || result.Value == SocialFollowResult.InternalError)
            {
                Console.WriteLine(result.Value);
            }

            // Get the updated count of followed people.
            Console.Write("Updated count: ");
            WriteFollowedCount();
            Console.ReadKey();
        }

        // Get the count of the people who the current user is following.
        static void WriteFollowedCount()
        {
            ClientResult<int> followedCount = followingManager.GetFollowedCount(SocialActorTypes.Users);
            clientContext.ExecuteQuery();
            Console.WriteLine("The current user is following {0} people.", followedCount.Value);
        }
    }
}

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

次のコード例では、現在のユーザーがフォローしているユーザーを取得し、現在のユーザーの後に続くユーザーを取得し、現在のユーザーの [次へ] People状態に関する情報を取得します。 以下の操作方法が記述されています。

  • IsFollowed メソッドを使用して、現在のユーザーがターゲット ユーザーをフォローしているかどうかを確認します。

  • GetFollowedCount メソッドを使用して、現在のユーザーがフォローしているユーザーの数を取得します。

  • GetFollowed メソッドを使用して、現在のユーザーがフォローしているユーザーを取得します。

  • GetFollowers メソッドを使用して、現在のユーザーをフォローしているユーザーを取得します。

  • ユーザーのグループを反復処理して、それぞれのユーザーの表示名、個人の URI、および画像の URI を取得します。

注:

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

namespace FollowPeopleCSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            // Replace the following placeholder values with the target
            // server URL and target user.
            const string serverUrl = "http://serverName";
            const string targetUser = "domainName\\\\userName";

            // Get the client context.
            ClientContext clientContext = new ClientContext(serverUrl);
            
            // Get the SocialFeedManager instance.
            SocialFollowingManager followingManager = new SocialFollowingManager(clientContext);

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

            // Find out whether the current user is following the target user.
            ClientResult<bool> isFollowed = followingManager.IsFollowed(actorInfo);

            // Get the count of people who the current user is following.
            ClientResult<int> followedCount = followingManager.GetFollowedCount(SocialActorTypes.Users);

            // Get the people who the current user is following.
            ClientResult<SocialActor[]> followedResult = followingManager.GetFollowed(SocialActorTypes.Users);

            // Get the people who are following the current user.
            ClientResult<SocialActor[]> followersResult = followingManager.GetFollowers();

            // Get the information from the server.
            clientContext.ExecuteQuery();

            // Write the results to the console window.
            Console.WriteLine("Is the current user following the target user? {0}\\n", isFollowed.Value);
            Console.WriteLine("People who the current user is following: ({0} count)", followedCount.Value);
            IterateThroughPeople(followedResult.Value);
            Console.WriteLine("\\nPeople who are following the current user:");
            IterateThroughPeople(followersResult.Value);
            Console.ReadKey();
        }

        // Iterate through the people and get each person's display
        // name, personal URI, and picture URI.
        static void IterateThroughPeople(SocialActor[] actors)
        {
            foreach (SocialActor actor in actors)
            {
                Console.WriteLine("  - {0}", actor.Name);
                Console.WriteLine("\\tPersonal URI: {0}", actor.PersonalSiteUri);
                Console.WriteLine("\\tPicture URI: {0}", actor.ImageUri);
            }
        }
    }
}

関連項目