Check if SharePoint item is followed programmatically

25/02/2016 11:56

Function bellow checks if SharePoint document is followed by user. Code to follow/unfollow document is at link Follow SharePoint document programmatically


public static bool IsItemFollowed(SPWeb web, SPUser user, string listUrl, int itemId)
{
 bool isFollowed = false;
 try
 {
  var list = web.GetList(listUrl);
  var item = list.GetItemById(itemId);

  if (item.File != null)
  {
   var serviceContext = SPServiceContext.GetContext(web.Site);
   var userProfileManager = new UserProfileManager(serviceContext);
   
   var userProfile = userProfileManager.GetUserProfile(user.LoginName);

   SPSocialFollowingManager socialManager = new SPSocialFollowingManager(userProfile, serviceContext);
         
   string documentUrl = item.File.Url;
   string documentAbsoluteUrl = SPUrlUtility.CombineUrl(item.Web.Url, documentUrl);

   var actor = new SPSocialActorInfo();

   actor.ContentUri = new Uri(documentAbsoluteUrl);
   actor.ActorType = SPSocialActorType.Document;
   actor.AccountName = user.LoginName;

   isFollowed = socialManager.IsFollowed(actor);
  }
  
 }
 catch (Exception ex)
 {
  //handling
  return isFollowed;
 }
}