SharePoint の .NET クライアント オブジェクト モデルを使用してドキュメントとサイトをフォローする

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

コンテンツのフォローに .NET クライアント オブジェクト モデルを使用する方法

SharePoint ユーザーは、ドキュメント、サイト、およびタグをフォローすると、ニュースフィードでアイテムに関する更新を取得して、フォローしているドキュメントやサイトをすぐに開くことができます。 アプリやソリューションで .NET クライアント オブジェクト モデルを使用すると、現在のユーザーの代理として、コンテンツのフォローの開始、終了、およびフォローしているコンテンツの取得を実行できます。 この記事では、.NET クライアント オブジェクト モデルを使用してドキュメントとサイトのコンテンツのフォロー機能を操作するコンソール アプリケーションの作成方法について説明します。

次に示すオブジェクトは、コンテンツのフォロー タスクに対応する主要な API です。

注:

これらの API はユーザーのフォロー タスクにも使用しますが、SocialFollowingManager から使用できる GetSuggestions メソッドと GetFollowers メソッドはユーザーのフォローのみをサポートしていて、コンテンツのフォローはサポートしていません。 SocialFollowingManager の使用方法の詳細については、「SharePoint のコンテンツのフォロー」と「SharePoint のユーザーのフォロー」を参照してください。 ユーザーのフォロー方法を示すコード例については、「 方法: SharePoint の .NET クライアント オブジェクト モデルを使用してユーザーをフォローする」を参照してください。

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

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

  • SharePoint (個人用サイトが構成済みであり、現在のユーザーの個人用サイトが作成済みであり、ドキュメントが SharePoint ドキュメント ライブラリにアップロード済みであること)

  • Visual Studio 2012

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

注:

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

SharePoint .NET クライアント オブジェクト モデルを使用してコンテンツのフォロー機能を操作するコンソール アプリケーションの作成

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

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

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

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

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

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

コード例: SharePointの .NET クライアント オブジェクト モデルを使用して、コンテンツのフォローを開始および停止する

次のコード例では、現在のユーザーによる対象アイテムのフォローを開始または停止します。 次の操作を行う方法を示します。

  • IsFollowed メソッドを使用して、現在のユーザーが特定のドキュメントまたはサイトをフォローしているかどうかを確認します。

  • Follow メソッドを使用して、ドキュメントまたはサイトの フォロー を開始します。

  • StopFollowing メソッドを使用して、ドキュメントまたはサイトのフォローを停止します。

  • GetFollowedCount メソッドを使用して、現在のユーザーがフォローしているドキュメントまたはサイトの数を取得します。

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

注:

コードを実行する前に、変数 serverUrlcontentUrl のプレースホルダーの値を変更してください。 ドキュメントの代りにサイトを使用する場合は、コメント化されている変数を使用します。


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

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

            // Replace the following placeholder values with the URL of the target
            // server and target document (or site).
            const string serverUrl = "http://serverName";
            const string contentUrl = @"http://serverName/libraryName/fileName";
            const SocialActorType contentType = SocialActorType.Document;
            // Do not use a trailing '/' for a subsite.
            //const string contentUrl = @"http://serverName/subsiteName"; 
            //const SocialActorType contentType = SocialActorType.Site;

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

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

            // Create a SocialActorInfo object to represent the target item.
            SocialActorInfo actorInfo = new SocialActorInfo();
            actorInfo.ContentUri = contentUrl;
            actorInfo.ActorType = contentType;

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

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

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

            // Get the current count of followed items.
            WriteFollowedCount(actorInfo.ActorType);

            // Try to follow the target item. 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 item.
            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 items.
            Console.Write("Updated count: ");
            WriteFollowedCount(actorInfo.ActorType);
            Console.ReadKey();
        }

        // Get the count of the items that the current user is following.
        static void WriteFollowedCount(SocialActorType type)
        {

            // Set the parameter for the GetFollowedCount method, and
            // handle the case where the item is a site. 
            SocialActorTypes types = SocialActorTypes.Documents;
            if (type != SocialActorType.Document)
            {
                types = SocialActorTypes.Sites;
            }

            ClientResult<int> followedCount = followingManager.GetFollowedCount(types);
            clientContext.ExecuteQuery();
            Console.WriteLine("{0} followed {1}", followedCount.Value, types.ToString().ToLower());
        }
    }
}

コード例: SharePoint .NET クライアント オブジェクト モデルを使用してフォローしているコンテンツを取得する

次のコード例では、現在のユーザーがフォローしているドキュメントとサイトを取得し、ユーザーのコンテンツのフォロー状況に関する情報を取得します。 次の操作を行う方法を示します。

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

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

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

  • コンテンツのグループを反復処理して、それぞれのアイテムの名前、コンテンツの URL、および URI を取得します。

注:

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


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

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

            // Replace the following placeholder values with the URLs of
            // the target server, document, and site.
            const string serverUrl = "http://serverName";
            const string docContentUrl = @"http://serverName/libraryName/fileName";
            const string siteContentUrl = @"http://serverName/subsiteName"; // do not use a trailing '/' for a subsite

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

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

            // Create SocialActorInfo objects to represent the target 
            // document and site.
            SocialActorInfo docActorInfo = new SocialActorInfo();
            docActorInfo.ContentUri = docContentUrl;
            docActorInfo.ActorType = SocialActorType.Document;
            SocialActorInfo siteActorInfo = new SocialActorInfo();
            siteActorInfo.ContentUri = siteContentUrl;
            siteActorInfo.ActorType = SocialActorType.Site;

            // Find out whether the current user is following the target
            // document and site.
            ClientResult<bool> isDocFollowed = followingManager.IsFollowed(docActorInfo);
            ClientResult<bool> isSiteFollowed = followingManager.IsFollowed(siteActorInfo);

            // Get the count of documents and sites that the current
            // user is following.
            ClientResult<int> followedDocCount = followingManager.GetFollowedCount(SocialActorTypes.Documents);
            ClientResult<int> followedSiteCount = followingManager.GetFollowedCount(SocialActorTypes.Sites);

            // Get the documents and the sites that the current user
            // is following.
            ClientResult<SocialActor[]> followedDocResult = followingManager.GetFollowed(SocialActorTypes.Documents);
            ClientResult<SocialActor[]> followedSiteResult = followingManager.GetFollowed(SocialActorTypes.Sites);

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

            // Write the results to the console window.
            Console.WriteLine("Is the current user following the target document? {0}", isDocFollowed.Value);
            Console.WriteLine("Is the current user following the target site? {0}", isSiteFollowed.Value);
            if (followedDocCount.Value > 0)
            {
                IterateThroughContent(followedDocCount.Value, followedDocResult.Value);
            } if (followedSiteCount.Value > 0)
            {
                IterateThroughContent(followedSiteCount.Value, followedSiteResult.Value);
            }
            Console.ReadKey();
        }

        // Iterate through the items and get each item's display
        // name, content URI, and absolute URI.
        static void IterateThroughContent(int count, SocialActor[] actors)
        {
            SocialActorType actorType = actors[0].ActorType;
            Console.WriteLine("\\nThe current user is following {0} {1}s:", count, actorType.ToString().ToLower());
            foreach (SocialActor actor in actors)
            {
                Console.WriteLine("  - {0}", actor.Name);
                Console.WriteLine("\\tContent URI: {0}", actor.ContentUri);
                Console.WriteLine("\\tURI: {0}", actor.Uri);
            }
        }
    }
}

関連項目