I've created a custom web part that includes a button to like a blog post and I'm trying to figure out how to implement it programmatically. I think I'm close but my GetAllLikers(postID) function call is throwing the error "The target of the operation was not found". This kind of makes sense because how does that function know which post list to query? The "announcements" subsite is a blog template site and the actual Posts exist in a list within this subsite. I've looked at all of my constructor calls and can't find anywhere to add this information. Also tried opening the site at the list level like:
SPSite site = new SPSite(rootUrl + "announcements/Lists/Posts");
but no luck. Current code:
protected void Like_Post_Click(string postID)
{
//Grab our Announcements Blog subsite
SPSite site = new SPSite(rootUrl + "announcements");
SPWeb web = site.OpenWeb();
SPUser user = web.CurrentUser;
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(serviceContext);
if(profileManager.UserExists(user.LoginName))
{
UserProfile userProfile = profileManager.GetUserProfile(user.LoginName);
//get the Social Feed Manager of the selected user
SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, serviceContext);
//SPList posts = web.Lists["Posts"];
//SPListItem post = posts.GetItemById(Convert.ToInt32(postID));
//Get the likes for a specific post
//*****THIS IS THE CALL THAT BREAKS*****
//I'd really like this to take a SPListItem instead of a postID
//How does it know where to grab the post???
SPSocialActor[] likers = feedManager.GetAllLikers(postID);
if (likers.Where(co => co.AccountName == userProfile.AccountName).FirstOrDefault() == null)
{
feedManager.LikePost(postID);
}
else
{
feedManager.UnlikePost(postID);
}
}
}