Hi,
I am having a play with the social features in SharePoint 2013 using the server object model. I can retrieve a team site news feed with no problem, with associated likes, replies etc. However, when I go to write to the feed (e.g. by trying to "like" a post which I currently do not like), I am getting nothing but errors. Specifically...
Microsoft.Office.Server.Social.SPSocialException: The operation is invalid for the target's current state. Internal type name: Microsoft.Office.Server.Microfeed.MicrofeedException. Internal error code: 32. at Microsoft.Office.Server.Social.SPSocialUtil.TryTranslateExceptionAndThrow(Exception exception) at Microsoft.Office.Server.Social.SPSocialUtil.InvokeWithExceptionTranslation[T](ISocialOperationManager target, String name, Func`1 func) at Microsoft.Office.Server.Social.SPSocialFeedManager.<>c__DisplayClass45`1.<s2sinvoke>b__44() at Microsoft.Office.Server.Social.SPSocialUtil.InvokeWithExceptionTranslation[T](ISocialOperationManager target, String name, Func`1 func) at Microsoft.Office.Server.Social.SPSocialFeedManager.LikePost(String postId) at wall.getPosts()
Error code 32 is "InvalidLikeRequest". If I try to "unlike" the request, it returns error code 38 (InvalidUnlikeRequest_alreadyMarkedAsUnlike) which I obviously expect to happen, as you can't unlike something you don't already like. But what it tells me is that the thread / post ID I am passing to the feedManager.likepost(string ID) method is correct as the error changes depending on whether I already like it or not.
Abbreviated code below which should "like" the most recent post in a site news feed:
using (SPSite site = new SPSite(url))
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
using (new SPServiceContextScope(serviceContext))
{
SPUser thisUser = site.RootWeb.CurrentUser;
SPUserToken token = thisUser.UserToken;
UserProfileManager manager = new UserProfileManager(serviceContext);
UserProfile profile = manager.GetUserProfile(thisUser.LoginName);
SPSocialFeedManager feedManager = new SPSocialFeedManager(profile, serviceContext);
SPSocialFeed feed = feedManager.GetFeedFor(teamSiteAddress, new SPSocialFeedOptions());
SPSocialThread thread = feed.Threads[0];
feedManager.LikePost(thread.Id);
}
}
Any help greatly appreciated