2

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);
            }
        }
    }

1 Answer 1

2

Just use SetLike method. You can also do it via client object model (JavaScript), I think it's prettier. Sample.

2
  • Do you have an example using SetLike? It keeps saying the list ID is not a valid GUID and I've tried it with curly braces, without curly braces, and without dashes.
    – Fenoec
    Dec 5, 2013 at 19:15
  • Yikes; the JS method is ... not very pretty, IMO. JS does seem to strip out curly braces, though (_spPageContextInfo.pageListId.substring(1, 37),) but doesn't seem to be enough on the server-side. Dec 19, 2013 at 17:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.