SharePoint 2013 Unfollow Site using CSOM


The SocialFollowingManager class can be used to follow or unfollow content/site/people/tag.  To get it working with SharePoint App, you'll need to request Core (Social) permission. But that'll only work if you install the app on My Site Host.

To get it working for any app, you'll need to request Tenant permission.  However, Tenant Full Control will not work.  You have to request Tenant Manage permission instead.

Once you have requested, you can use the follow code to stop follow a site.

private void unfollowSite(ClientContext clientContext)
        {
            Site siteToStopFollow = clientContext.Site;
            clientContext.Load(siteToStopFollow, s => s.Url);
            clientContext.ExecuteQuery();

            SocialFollowingManager followMgr = new SocialFollowingManager(clientContext);
            //clientContext.Load(followMgr);
            clientContext.ExecuteQuery();

            SocialActorInfo actorInfo = new SocialActorInfo();
            actorInfo.ContentUri = siteToStopFollow.Url;
            actorInfo.ActorType = SocialActorType.Site;
            // Find out whether the current user is following the target item.
            ClientResult isFollowed = followMgr.IsFollowed(actorInfo);

            // Get the information from the server.
            clientContext.ExecuteQuery();

            bool isFollowing = isFollowed.Value;

            if (isFollowing)
            {
                followMgr.StopFollowing(actorInfo);
                clientContext.ExecuteQuery();
            }

        }



var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

using (var clientContext= spContext.CreateUserClientContextForSPHost())
{
     if (context != null)
    {
          unfollowSite(context);
     }
}

Comments

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

Add spell check dictionary in SharePoint 2010, SharePoint 2013, and Office 365