Programmatically Follow Documents in SharePoint 2013

Share this post :

Summary : Programmatically Follow Documents in SharePoint 2013,SharePoint follow sites object model,Follow Documents Object Model,Get followed documents get followed documents SharePoint programmatically,Use SPSocialFollowingManager to follow Files

Social is a big thing in SharePoint 2013.Users can Follow Content,Sites and even People to get their updates listed in User’s MySite’s NewsFeed.The functionality is great for Admins or Managers who want to follow any updates done to a Site or a Document.With the Introduction of these Social capabilities the new Microsoft.Office.Server.Social namespace is also added to interact with social feeds and to follow people and content from custom solutions for SharePoint Server 2013 and apps for SharePoint.

Because We’re So Good, Our Content gets Copied very Often.If you are not reading this on, please read the Original article Here for details, comments and updates on this post.

In this post we will look at an example of how to Programmatically Follow Documents in SharePoint 2013 using the SPSocialFollowingManager class. This class is a part of new Microsoft.Office.Server.Social namespace.
In this example we have added a Button on a Custom Application Page that was deployed using Visual Studio.This “Follow” button when clicked will run the code to make a user Follow a document in Shared Documents library.

In this example we use Server Object model to make user follow the document.

Copy and paste the below Code to add a Button in your webpart or Application page’s aspx

<asp:button runat="server" text="Follow" id="followBtn" />

Copy and paste the below Code in your Webparts or Application page’s Code Behind.

Classes –
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Social;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Portal.UserProfiles;
using Microsoft.SharePoint.Utilities;

//Adding the Button On click event
protected void Page_Load(object sender, EventArgs e)
followBtn.Click += new EventHandler(this.followBtn_Click);

//Adding the function to Follow the document
void followBtn_Click(Object sender,EventArgs e)

//Get the targeted user

SPWeb currentWeb = SPContext.Current.Web;
SPUser currentUser = SPContext.Current.Web.CurrentUser;
String _docToFollow = "/Shared%20Documents/Organization%20Rule%20Book.docx";

//Get the UserProfil for target user
SPServiceContext serverContext = SPServiceContext.GetContext(currentWeb.Site);
UserProfileManager profileManager = new UserProfileManager(serverContext);
UserProfile profile = profileManager.GetUserProfile(currentUser.LoginName);
if (profile != null)

//Create a Social Manager profile
SPSocialFollowingManager _followManager = new SPSocialFollowingManager(profile);

//Create a Social actor with current web
SPSocialActorInfo newActor = new SPSocialActorInfo();

// this works, because the protocol is included in the string
Uri serverUri = new Uri(currentWeb.Url);

// needs UriKind arg, or UriFormatException is thrown
Uri relativeUri = new Uri(_docToFollow, UriKind.Relative);

newActor.ContentUri = new Uri(serverUri, relativeUri);

newActor.AccountName = currentUser.LoginName;

newActor.ActorType = SPSocialActorType.Document;

//Follow on this actor


After the code runs the current user will start following the said document.
You might also want to see other SharePoint 2013 Developer Tutorials and Samples

Original article : Here