2

In SP 2013 on prem, I am developing a web part using c#. All my functions work except follow/unfollow a site for a user.

So I am turning here for some inspiration, looking for some code snippets or blogs/links. Please share if you got some good references or snippets.

2

You can use below code snippet to follow a site

//GET USER PROFILE MANAGER
SPServiceContext serverContext = SPServiceContext.GetContext(siteobject);
UserProfileManager profileManager = new UserProfileManager(serverContext);
UserProfile profile = profileManager.GetUserProfile(loginName);
if (profile != null)
{
    //Social following manager object for the user's profile
    SPSocialFollowingManager manager = new SPSocialFollowingManager(profile);
    SPSocialActorInfo actorInfo = new SPSocialActorInfo();
    actorInfo.ContentUri = new Uri(url);//url of site to follow
    actorInfo.AccountName = targetUser.LoginName;
    actorInfo.ActorType = SPSocialActorType.Site;
    manager.Follow(actorInfo);
}

In case of UnFollow

//GET USER PROFILE MANAGER
SPServiceContext serverContext = SPServiceContext.GetContext(siteobject);
UserProfileManager profileManager = new UserProfileManager(serverContext);
UserProfile profile = profileManager.GetUserProfile(loginName);
if (profile != null)
{
    //Social following manager object for the user's profile
    SPSocialFollowingManager manager = new SPSocialFollowingManager(profile);
    SPSocialActorInfo actorInfo = new SPSocialActorInfo();
    actorInfo.ContentUri = new Uri(url);//url of site to unfollow
    actorInfo.AccountName = targetUser.LoginName;
    actorInfo.ActorType = SPSocialActorType.Site;
    manager.StopFollowing(actorInfo);
}
  • Thanks for your code, mate. I am having issues making it work. On manager.StopFollowing(actorInfo);, I get this error; An exception of type 'Microsoft.Office.Server.Social.SPSocialException' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code. Have you had the same issue ? – Ilyas Jun 20 '16 at 19:15
2

Follow Method that takes a SPWeb object and a SPUser object.

SPServiceContext serverContext = SPServiceContext.GetContext(web.Site);
UserProfileManager profileManager = new UserProfileManager(serverContext);
string userString = user.LoginName.ToString();
UserProfile userProfile = profileManager.GetUserProfile(userString);

        if (userProfile != null)
        {
            SPSocialFollowingManager manager = new   
            SPSocialFollowingManager(userProfile);
            SPSocialActorInfo actorInfo = new SPSocialActorInfo();
            actorInfo.ContentUri = new Uri(web.Url);
            actorInfo.AccountName = user.LoginName;
            actorInfo.ActorType = SPSocialActorType.Site;
            manager.Follow(actorInfo);
        }

https://stackoverflow.com/questions/23271133/how-do-i-follow-site-in-sharepoint-2013-with-ssom

here is the MSDN guideline https://msdn.microsoft.com/en-us/library/office/jj163217.aspx?f=255&MSPPError=-2147217396

Also this one http://www.learningsharepoint.com/2012/12/31/programmatically-follow-documents-in-sharepoint-2013/

  • I have tried this code as reference, and many others. I always get an error: An exception of type 'Microsoft.Office.Server.Social.SPSocialException' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code. Have you had the same issue, and do you know about any work around ? – Ilyas Jun 20 '16 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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