博客地址:http://blog.csdn.net/FoxDave
上一篇講了如何獲取用戶配置文件的相關屬性,它屬于SharePoint 2013社交功能的一個小的構成部分。社交功能是SharePoint 2013改進的一大亮點。可以在現有網站上開啟社交功能或者新建一個專門用于社交用途的社區網站,社交功能包括關注(人或內容)、艾特@、#等功能、有清晰的用戶積分制度等等。由于工作中不會有太多關于這方面的開發需求,并且個人覺得這部分做得挺不錯,基本的需求應該是夠用了(強大的或許就不在SharePoint上了),所以本篇只會用兩個小例子展示一下如何用客戶端對象模型與SharePoint社交功能進行交互來說明SharePoint 2013社交功能的開發,當然不僅限于客戶端對象模型,JSOM和REST也可以做類似的事情。
包括之前提到的用戶配置文件相關的開發,在用客戶端對象模型做社交相關功能的代碼交互開發時,需要引入Microsoft.SharePoint.Client、Microsoft.SharePoint.ClientRuntime和Microsoft.SharePoint.Client.UserProfiles這三個程序集,并在代碼文件頭部增加如下兩個引用:
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Social;
首先構建上下文對象:
ClientContext clientContext = new ClientContext("<你的網站URL>");
在Microsoft.SharePoint.Client.Social這個命名空間下,有SocialFeedManager、SocialFeedOptions、SocialPostCreationData和SocialFollowingManager等對象模型可供我們使用,分別跟訂閱、回帖、關注等有關。
獲取指定用戶的動態:
SocialFeedManager feedManager = new SocialFeedManager(clientContext);SocialFeedOptions feedOptions = new SocialFeedOptions();feedOptions.MaxThreadCount = 10;ClientResult<SocialFeed> feed = feedManager.GetFeedFor("<指定的用戶>", feedOptions);clientContext.ExecuteQuery();for (int i = 0; i < feed.Value.Threads.Length; i++){SocialThread thread = feed.Value.Threads[i];string postText = thread.RootPost.Text;Console.WriteLine("\t" + (i + 1) + ". " + postText);}
獲得指定用戶關注和被關注的人:
static void Main(string[] args){string serverUrl = "<你的網站URL>";string targetUser = "<指定的用戶>";ClientContext clientContext = new ClientContext(serverUrl);SocialFollowingManager followingManager = new SocialFollowingManager(clientContext);SocialActorInfo actorInfo = new SocialActorInfo();actorInfo.AccountName = targetUser;ClientResult<int> followedCount = followingManager.GetFollowedCount(SocialActorTypes.Users);ClientResult<SocialActor[]> followedResult = followingManager.GetFollowed(SocialActorTypes.Users);ClientResult<SocialActor[]> followersResult = followingManager.GetFollowers();clientContext.ExecuteQuery();Console.WriteLine("當前用戶關注的人數: ({0})", followedCount.Value);IterateThroughPeople(followedResult.Value);Console.WriteLine("\n誰關注此用戶:");IterateThroughPeople(followersResult.Value);Console.ReadKey();}static void IterateThroughPeople(SocialActor[] actors){foreach (SocialActor actor in actors){Console.WriteLine(" - {0}", actor.Name);Console.WriteLine("\t鏈接: {0}", actor.PersonalSiteUri);Console.WriteLine("\t頭像: {0}", actor.ImageUri);}}
更多內容請參考微軟MSDN文檔,寫得還是很詳細清晰的,如果我們在工作中遇到了相關內容的任務,能夠提供很有力的參考和幫助。