Share via


SharePoint で .NET クライアント オブジェクト モデルを使用して投稿の作成と削除、およびソーシャル フィードの取得を行う

SharePoint .NET クライアント オブジェクト モデルを使用して、マイクロブログの投稿を作成および削除する方法とソーシャル フィードを取得する方法を説明します。

SharePoint のソーシャル フィードとは

SharePoint の場合、ソーシャル フィードは、会話、1 つのマイクロブログの投稿、または通知のコレクションを表します。 スレッドには、最初の投稿と返信の投稿のコレクションが含まれています。これらの投稿が、会話、1 つのマイクロブログの投稿、または通知を表します。 .NET クライアント オブジェクト モデルでは、フィードは SocialFeed オブジェクトで表され、スレッドは SocialThread オブジェクトで表され、投稿と返信は SocialPost オブジェクトで表されます。 .NET クライアント オブジェクト モデルで、フィード関連のコア タスクを実行するには、SocialFeedManager オブジェクトを使用します。 この記事では、.NET クライアント オブジェクト モデルを使用してソーシャル フィードを操作するコンソール アプリケーションの作成方法について説明します。

SocialFeedManager の操作の詳細、または他の API を使用してソーシャル フィードを操作する方法の詳細については、「SharePoint でのソーシャル フィードの操作」を参照してください。

SharePointの .NET クライアント オブジェクト モデルを使用してソーシャル フィードを操作する開発環境をセットアップするための前提条件

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

  • 個人用サイトが構成済みで、現在のユーザー用の個人サイトとターゲット ユーザー用の個人サイトを作成済みであり、ターゲット ユーザーを現在のユーザーがフォローしていて、ターゲット ユーザーによる投稿がいくつかある SharePoint

  • Visual Studio 2012

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

注:

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

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

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

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

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

  4. プロジェクト名に「SocialFeedCSOM」という名前を付け、[ OK] をクリックします。

  5. 以下のアセンブリに参照を追加します。

    • Microsoft.SharePoint.Client
    • Microsoft.SharePoint.ClientRuntime
    • Microsoft.SharePoint.Client.UserProfiles
  6. Program クラスの内容を以下のいずれかのシナリオのコード例で置き換えます。

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

コード例: SharePointの .NET クライアント オブジェクト モデルを使用した、ソーシャル フィードへの投稿と返信の公開

以下のコードは、現在のユーザーから投稿と返信の公開を行う例です。 以下の操作の方法が記述されています。

  • 投稿内容を定義する。 この例では、投稿にリンクが含まれています。

  • CreatePost メソッドを使用し、targetId パラメーターとして null を渡して、現在のユーザーのフィードに投稿を発行します。

  • GetFeed メソッドを使用して、現在のユーザーのニュースフィードの種類を取得します。

  • フィードを反復処理して、返信可能なすべてのスレッドを見つけて、スレッドと投稿に関する情報を取得する。

  • CreatePost メソッドを使用し、スレッド識別子を targetId パラメーターとして渡して、投稿に返信します。

注:

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


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

namespace SocialFeedCSOM
{
    class Program
    {
        static void Main(string[] args)
        {
            // Replace the following placeholder value with the target server URL.
            const string serverUrl = "http://serverName/";

            Console.Write("Type your post text:  ");

            // Create a link to include in the post.
            SocialDataItem linkDataItem = new SocialDataItem();
            linkDataItem.ItemType = SocialDataItemType.Link;
            linkDataItem.Text = "link";
            linkDataItem.Uri = "http://bing.com";

            // Define properties for the post.
            SocialPostCreationData postCreationData = new SocialPostCreationData();
            postCreationData.ContentText = Console.ReadLine() + " Plus a {0}.";
            postCreationData.ContentItems = new SocialDataItem[1] { linkDataItem };

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

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

            // Publish the post. This is a root post, so specify null for the
            // targetId parameter. 
            feedManager.CreatePost(null, postCreationData); 
            clientContext.ExecuteQuery();

            Console.WriteLine("\\nCurrent user's newsfeed:");

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

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

            // Create a dictionary to store the Id property of each thread. This 
            // code example stores the Id so you can select a thread to reply to.
            Dictionary<int, string> idDictionary = new Dictionary<int, string>();

            // Iterate through each thread in the feed.
            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);

                    // Get properties from the root post and thread.
                    // If a thread contains more than two replies, the server returns
                    // a thread digest that contains only the two most recent replies.
                    // To get all replies, call SocialFeedManager.GetFullThread.
                    SocialPost rootPost = thread.RootPost;
                    SocialActor author = thread.Actors[rootPost.AuthorIndex];
                    Console.WriteLine(string.Format("{0}. {1} said \\"{2}\\" ({3} replies)",
                        (i + 1), author.Name, rootPost.Text, thread.TotalReplyCount));
                }
            }
            Console.Write("\\nWhich thread 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. This example reuses the 
            // SocialPostCreationData object that was used to create a post.
            postCreationData.ContentText = Console.ReadLine();

            // Publish the reply and make the changes on the server.
            ClientResult<SocialThread> result = feedManager.CreatePost(threadToReplyTo, postCreationData);
            clientContext.ExecuteQuery();

            Console.WriteLine("\\nThe reply was published. The thread now has {0} replies.", result.Value.TotalReplyCount);
            Console.ReadLine();
         }
    }
}

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

次のコード例では、現在のユーザーとターゲット ユーザーのフィードを取得します。 以下の操作方法が記述されています。

  • GetFeed メソッドを使用して、現在のユーザーの PersonalNews、および Timelineフィード タイプを取得する。

  • GetFeedFor メソッドを使用して、ターゲット ユーザーの個人用フィードの種類を取得します。

  • フィードを反復処理して非参照スレッドをすべて検出し、それらのスレッドと投稿に関する情報を取得する。 参照スレッドは、別のスレッドに関する情報を含む通知を表します。 たとえば、誰かに関するユーザーのメンションが投稿内にあると、元の投稿へのリンクと投稿についての他のメタデータを含む MentionReference 型のスレッドがサーバーによって生成されます。

フィード タイプの詳細については、「フィード タイプの概要」を参照してください。 参照スレッドの詳細については、「 SharePoint ソーシャル フィードの参照スレッドとダイジェスト スレッド」を参照してください。

注:

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


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

namespace SocialFeedCSOM
{
    class Program
    {
        static string owner;
        static void Main(string[] args)
        {

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

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

            // Get the SocialFeedManager instance. 
            // Load the instance to get the Owner property.
            SocialFeedManager feedManager = new SocialFeedManager(clientContext);
            clientContext.Load(feedManager, f => f.Owner);

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

            // Get all feed types for current user and get the Personal feed
            // for the target user.
            ClientResult<SocialFeed> personalFeed = feedManager.GetFeed(SocialFeedType.Personal, feedOptions);
            ClientResult<SocialFeed> newsFeed = feedManager.GetFeed(SocialFeedType.News, feedOptions);
            ClientResult<SocialFeed> targetUserFeed = feedManager.GetFeedFor(targetUser, feedOptions);

            // Change the sort order to optimize the Timeline feed results.
            feedOptions.SortOrder = SocialFeedSortOrder.ByCreatedTime;
            ClientResult<SocialFeed> timelineFeed = feedManager.GetFeed(SocialFeedType.Timeline, feedOptions);

            // Run the request on the server.
            clientContext.ExecuteQuery();

            // Get the name of the current user within this instance.
            owner = feedManager.Owner.Name;

            // Iterate through the feeds and write the content.
            IterateThroughFeed(personalFeed.Value, SocialFeedType.Personal, true);
            IterateThroughFeed(newsFeed.Value, SocialFeedType.News, true);
            IterateThroughFeed(timelineFeed.Value, SocialFeedType.Timeline, true);
            IterateThroughFeed(targetUserFeed.Value, SocialFeedType.Personal, false);

            Console.ReadKey(false);
        }

        // Iterate through the feed and write to the console window.
        static void IterateThroughFeed(SocialFeed feed, SocialFeedType feedType, bool isCurrentUserOwner)
        {
            SocialThread[] threads = feed.Threads;

            // If this is the target user's feed, get the user's name.
            // A user is the owner of all threads in his or her Personal feed.
            if (!isCurrentUserOwner)
            {
                SocialThread firstThread = threads[0];
                owner = firstThread.Actors[firstThread.OwnerIndex].Name;
            }
            Console.WriteLine(string.Format("\\n{0} feed type for {1}:", feedType.ToString(), owner));

            // Iterate through each thread in the feed.
            foreach (SocialThread thread in threads)
            {

                // Ignore reference thread types.
                if (thread.ThreadType == SocialThreadType.Normal)
                {

                    // Get properties from the root post and thread. 
                    // If a thread contains more than two replies, the server returns
                    // a thread digest that contains only the two most recent replies.
                    // To get all replies, call SocialFeedManager.GetFullThread.
                    SocialPost rootPost = thread.RootPost;
                    SocialActor author = thread.Actors[rootPost.AuthorIndex];
                    Console.WriteLine(string.Format("  - {0} posted \\"{1}\\" on {2}. This thread has {3} replies.",
                        author.Name, rootPost.Text, rootPost.CreatedTime.ToShortDateString(), thread.TotalReplyCount));
                }
            }
        }
    }
}

コード例: SharePoint .NET クライアント オブジェクト モデルを使用してソーシャル フィードから投稿と返信を削除する

次のコードは、現在のユーザーの個人フィードから投稿または返信を削除する例です。 以下の操作方法が記述されています。

  • GetFeed メソッドを使用して、現在のユーザーの個人用フィードの種類を取得します。

  • フィード内のスレッドを反復処理して、最初の投稿と返信に関する情報を取得する。

  • DeletePost メソッドを使用してルート投稿、返信、またはスレッドを削除します (ルート投稿を削除すると、スレッド全体が削除されます)。

注:

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


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

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

            // Replace the following placeholder value with the target SharePoint server.
            const string serverUrl = "http://serverName/";

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

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

            Console.WriteLine("\\nCurrent user's personal feed:");

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

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

            // Create a dictionary to store the Id property of each post and
            // reply. This code example stores the Id so you can select a post
            // or a reply to delete.
            Dictionary<int, string> idDictionary = new Dictionary<int, string>();

            // Iterate through each thread in the feed.
            for (int i = 0; i < feed.Value.Threads.Length; i++)
            {
                SocialThread thread = feed.Value.Threads[i];
                SocialPost rootPost = thread.RootPost;

                // Only keep posts that can be deleted.
                if (rootPost.Attributes.HasFlag(SocialPostAttributes.CanDelete)) 
                {
                    idDictionary.Add(i, rootPost.Id);

                    Console.WriteLine(string.Format("{0}. \\"{1}\\" has {2} replies.",
                        (i + 1), rootPost.Text, thread.TotalReplyCount));

                    // Get the replies.
                    // If a thread contains more than two replies, the server returns
                    // a thread digest that contains only the two most recent replies.
                    // To get all replies, call SocialFeedManager.GetFullThread.
                    if (thread.TotalReplyCount > 0)
                    {
                        foreach (SocialPost reply in thread.Replies)
                        {

                            // Only keep replies that can be deleted.
                            if (reply.Attributes.HasFlag(SocialPostAttributes.CanDelete)) 
                            {
                                i++;
                                idDictionary.Add(i, reply.Id);

                                SocialActor author = thread.Actors[reply.AuthorIndex];
                                Console.WriteLine(string.Format("\\t{0}. {1} replied \\"{2}\\"",
                                    (i + 1), author.Name, reply.Text));
                            }
                        }
                    }
                }
            }
            Console.Write("\\nEnter the number of the post or reply to delete. "
                + "(If you choose a root post, the whole thread is deleted.)");
            string postToDelete = "";
            int postNumber = int.Parse(Console.ReadLine()) - 1;
            idDictionary.TryGetValue(postNumber, out postToDelete);

            // Delete the reply and make the changes on the server.
            ClientResult<SocialThread> result = feedManager.DeletePost(postToDelete);
            clientContext.ExecuteQuery();

            // DeletePost returns digest thread if the deleted post is not the
            // root post. If it is the root post, the whole thread is deleted
            // and DeletePost returns null.
            if (result.Value != null)
            {
                SocialThread threadResult = result.Value;
                Console.WriteLine("\\nThe reply was deleted. The thread now has {0} replies.", threadResult.TotalReplyCount);
            }
            else
            {
                Console.WriteLine("\\nThe post and thread were deleted.");
            }
            Console.ReadKey(false);
        }
    }
}

次の手順

方法: SharePoint でメンション、タグ、サイトおよびドキュメントへのリンクを投稿に含める

関連項目