SharePoint の .NET クライアント オブジェクト モデルを使用してソーシャル フィードに対する読み書きを行う

SharePoint の .NET クライアント オブジェクト モデルを使用してソーシャル フィードに対する読み書きを行うコンソール アプリケーションを作成します。

SharePointの .NET クライアント オブジェクト モデルを使用してソーシャル フィードに対する読み書きを行うコンソール アプリケーションを作成するための前提条件

ここで作成するコンソール アプリケーションは、対象ユーザーのフィードを取得し、番号付きリストにある各スレッドの最初の投稿を出力します。 さらに、選択されているスレッドに対する簡単なテキスト返信を発行します。 フィードへの投稿と返信は、どちらを発行する場合にも同じメソッド ( CreatePost ) を使用します。

コンソール アプリケーションを作成するには、次のものが必要です。

  • 個人用サイトが構成済みで、現在のユーザーと対象ユーザー向けに個人用サイトが作成され、対象ユーザーによっていくつかの投稿が作成されている SharePoint
  • Visual Studio 2012
  • ログオンしているユーザーの User Profile Service アプリケーションに対するフル コントロールのアクセス許可

注:

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

SharePoint のソーシャル フィードの操作に関する中心概念

表 1 は、作業を開始する前に知っておく必要がある重要な概念を説明する記事へのリンクをまとめたものです。

表 1. SharePoint ソーシャル フィードを操作するための主要な概念

記事のタイトル 説明
SharePoint でのソーシャル機能の開発の概要 ソーシャル フィードとマイクロブログの投稿を使用したプログラミングを開始し、ユーザーやコンテンツ (ドキュメント、サイト、タグの.md ファイル) のフォローや、ユーザー プロファイルの操作を行う方法を説明しています。
SharePoint でのソーシャル フィードの操作 ソーシャル フィードを操作するための一般的なプログラミング作業と、そうした作業の実行に使用する API について説明しています。

Visual Studio 2012 でコンソール アプリケーションを作成してクライアント アセンブリへの参照を追加する

  1. 開発用のコンピューターで、Visual Studio 2012 を開きます。

  2. メニュー バーで、[ ファイル]、[ 新規作成]、[ プロジェクト] の順に選択します。 . [ 新しいプロジェクト] ダイアログ ボックスで、上部のドロップダウン リストから [ .NET Framework 4.5] を選択します。

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

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

  5. 次のようにして、クライアント アセンブリへの参照を追加します。

    1. ソリューション エクスプローラーで、 ReadWriteMySite プロジェクトのショートカット メニューを開き、[ 参照の追加] を選択します。

    2. [ 参照マネージャー] ダイアログ ボックスで、次のアセンブリを選択します。

      • Microsoft.SharePoint.Client
      • Microsoft.SharePoint.Client.Runtime
      • Microsoft.SharePoint.Client.UserProfiles

      SharePoint を実行しているコンピューター上で開発を行っている場合、上記のアセンブリは [拡張機能] カテゴリ内にあります。 それ以外の場合は、ダウンロードしたクライアント アセンブリの場所を参照します (「SharePoint クライアント コンポーネント」を参照)。

  6. Program.cs ファイルに、次 using のステートメントを追加します。

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;

SharePointの .NET クライアント オブジェクト モデルを使用して対象ユーザーのソーシャル フィードを取得する

  1. サーバー URL と対象ユーザーのアカウント資格情報に関する変数を宣言します。

    const string serverUrl = "http://serverName/";
    const string targetUser = "domainName\\userName";
    

    注:

    コードを実行する前に、 と domainName\\userName プレースホルダーのhttp://serverName/値を必ず置き換えてください。

  2. メソッドで Main() 、SharePoint クライアント コンテキストを初期化します。

    ClientContext clientContext = new ClientContext(serverUrl);
    
  3. SocialFeedManager インスタンスを作成します。

    SocialFeedManager feedManager = new SocialFeedManager(clientContext);
    
  4. 取得するフィード コンテンツのパラメーターを指定します。

    SocialFeedOptions feedOptions = new SocialFeedOptions();
    feedOptions.MaxThreadCount = 10;
    

    既定のオプションでは、フィード内の最初の 20 スレッドが最終更新日で並び替えられた状態で返されます。

  5. 対象ユーザーのフィードを取得します。

    ClientResult<SocialFeed> feed = feedManager.GetFeedFor(targetUser, feedOptions);
    clientContext.ExecuteQuery();
    

    GetFeedFor は、スレッドのコレクションを ClientResult<T> Value プロパティに格納するオブジェクト 返します。

SharePoint の .NET クライアント オブジェクト モデルを使用して反復処理とソーシャル フィードからの読み取りを行う

次のコードは、フィード内のスレッドに対して反復処理を行います。 各スレッドに CanReply 属性があるかどうかを確認し、スレッド識別子とルート投稿のテキストを取得します。 また、(スレッドへの返信に使用される) スレッド識別子を格納するための辞書を作成し、最初の投稿のテキストをコンソールに書き込みます。

Dictionary<int, string> idDictionary = new Dictionary<int, string>();
for (int i = 0; i < feed.Value.Threads.Length; i++)
{
    SocialThread thread = feed.Value.Threads[i];
    string postText = thread.RootPost.Text;
    if (thread.Attributes.HasFlag(SocialThreadAttributes.CanReply))
    {
        idDictionary.Add(i, thread.Id);
        Console.WriteLine("\\t" + (i + 1) + ". " + postText);
    }
}

SharePointの .NET クライアント オブジェクト モデルを使用してソーシャル フィードへの返信を投稿する

  1. (UI 関連のみ) 返信先のスレッドを取得し、ユーザーに返信内容の入力を求めます。

    Console.Write("Which post number do you want to reply to?  ");
    string threadToReplyTo = "";
    int threadNumber = int.Parse(Console.ReadLine()) - 1;
    idDictionary.TryGetValue(threadNumber, out threadToReplyTo);
    Console.Write("Type your reply:  ");
    
  2. 返信の定義を行います。 次のコードは、コンソール アプリケーションから返信テキストを取得します。

    SocialPostCreationData postCreationData = new SocialPostCreationData();
    postCreationData.ContentText = Console.ReadLine();
    
  3. 返信を発行します。 threadToReplyTo パラメーターは、スレッドの Id プロパティを表します。

    feedManager.CreatePost(threadToReplyTo, postCreationData);
    clientContext.ExecuteQuery();
    

    注:

    CreatePost メソッドは、最初のパラメーターに null を渡すことによって、現在のユーザーのフィードにルート投稿を発行するためにも使用されます。

  4. (UI 関連のみ) プログラムを終了します。

    Console.WriteLine("Your reply was published.");
    Console.ReadKey(false);
    
  5. コンソール アプリケーションをテストするには、メニュー バーの [ デバッグ] を選択し、[ デバッグ開始] をクリックします。

コード例: SharePointの .NET クライアント オブジェクト モデルを使用してフィードの取得と投稿への返信を行う

以下に、Program.cs ファイルの完全なコード例を示します。

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

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

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

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

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

            // Specify the parameters for the feed content that you want to retrieve.
            SocialFeedOptions feedOptions = new SocialFeedOptions();
            feedOptions.MaxThreadCount = 10;

            // Get the target owner's feed (posts and activities) and then run the request on the server.
            ClientResult<SocialFeed> feed = feedManager.GetFeedFor(targetUser, feedOptions);
            clientContext.ExecuteQuery();

            // Create a dictionary to store the Id property of each thread. This code example stores
            // the ID so a user can select a thread to reply to from the console application.
            Dictionary<int, string> idDictionary = new Dictionary<int, string>();
            for (int i = 0; i < feed.Value.Threads.Length; i++)
            {
                SocialThread thread = feed.Value.Threads[i];

                // Keep only the threads that can be replied to.
                if (thread.Attributes.HasFlag(SocialThreadAttributes.CanReply))
                {
                    idDictionary.Add(i, thread.Id);

                    // Write out the text of the post.
                    Console.WriteLine("\\t" + (i + 1) + ". " + thread.RootPost.Text);
                }
            }
            Console.Write("Which post number do you want to reply to?  ");

            string threadToReplyTo = "";
            int threadNumber = int.Parse(Console.ReadLine()) - 1;
            idDictionary.TryGetValue(threadNumber, out threadToReplyTo);

            Console.Write("Type your reply:  ");

            // Define properties for the reply.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = Console.ReadLine();

            // Post the reply and make the changes on the server.
            feedManager.CreatePost(threadToReplyTo, postCreationData);
            clientContext.ExecuteQuery();

            Console.WriteLine("Your reply was published.");
            Console.ReadKey(false);

            // TODO: Add error handling and input validation.
        }
    }
}

次の手順

.NET クライアント オブジェクト モデルを使用したソーシャル フィードに関する読み書きのタスクの実行方法については、以下を参照してください。

関連項目