How to get Count and List of documents and sites followed by a User in Office 365 SharePoint 2013 Site using CSOM

Home | Office 365 | How to get Count and List of documents and sites followed by a User in Office 365 SharePoint 2013 Site using CSOM

In this post, we will see how to retrieve list of followers for a document in Office 365 SharePoint site using .NET Client Object Model (CSOM).

Create a Console application and name it as ‘GetFollowers’

Add a reference to the following assemblies to the Console application.

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client.UserProfiles.dll

Add a new C# class and name it as GetFollower.cs

Copy and paste the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using Microsoft.SharePoint.Client.Social;
using System.Security;

namespace GetFollowers
{
    public class GetFollowers
    {

        public void RetrieveFollowedCount()
        {
            using (var oClientContext = new ClientContext(“
https://yoursite.sharepoint.com/”))
            {

                string UserName = “userid@yoursite.onmicrosoft.com”;
                string Password = “password”;

                SecureString oSecureString = GetSecureString(Password);

                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, oSecureString);

                //Instantiate SocialFollowingManager instance
                SocialFollowingManager oSocialFollowingManager = new SocialFollowingManager(oClientContext);

                //Instantiate SocialActorInfo class
                //SocialActorInfo oSocialActorInfo = new SocialActorInfo();
                //oSocialActorInfo.ContentUri = sDocUrl;
                //oSocialActorInfo.ActorType = SocialActorType.Document;

                //Invoke this private method to get the count of followed documents and sites
                GetFollowedCount(oClientContext, oSocialFollowingManager);

                //Invoke this private method to get actual list of documents and sites (objects)
                GetFollowedDocuments(oClientContext, oSocialFollowingManager);

                    Console.ReadLine();

               

            }
        }

        private static void GetFollowedCount(ClientContext oClientContext, SocialFollowingManager oSocialFollowingManager)
        {
            //Set the SocialActorType as documents to get count of documents followed by user
            ClientResult<int> oFollowedDocsCount = oSocialFollowingManager.GetFollowedCount(SocialActorTypes.Documents);
            oClientContext.ExecuteQuery();
            Console.WriteLine(“n”+”Total number of documents followed   :” + oFollowedDocsCount.Value.ToString());

            //Set the SocialActorType as documents to get count of sites followed by user
            ClientResult<int> oFollowedSiteCount = oSocialFollowingManager.GetFollowedCount(SocialActorTypes.Sites);
            oClientContext.ExecuteQuery();
            Console.WriteLine(“n”+”Total number of sites followed    :” + oFollowedSiteCount.Value.ToString());

            //Set the SocialActorType as documents to get count of user followed by user
            ClientResult<int> oFollowedUserCount = oSocialFollowingManager.GetFollowedCount(SocialActorTypes.Users);
            oClientContext.ExecuteQuery();
            Console.WriteLine(“n”+”Total number of users followed    :” + oFollowedUserCount.Value.ToString());
        }

        private static void GetFollowedDocuments(ClientContext oClientContext, SocialFollowingManager oSocialFollowingManager)
        {

            ClientResult<SocialActor[]> oSocialActors = oSocialFollowingManager.GetFollowed(SocialActorTypes.Documents);
            oClientContext.ExecuteQuery();

            foreach (SocialActor oSocialActor in oSocialActors.Value)
            {
                Console.WriteLine(“n”+”Name of followed document    :”+oSocialActor.Name);
                Console.WriteLine(“n”+”Document uri    :”+oSocialActor.ContentUri);
                Console.WriteLine(“n”+”Status    :”+oSocialActor.Status);

            }
        }

        //this method formats the input string into array of characters and place into SecureString object
        private static SecureString GetSecureString(String Password)
       {
           SecureString oSecurePassword = new SecureString();

           foreach (Char c in Password.ToCharArray())
           {
               oSecurePassword.AppendChar(c);

           }
           return oSecurePassword;
       }

 

       
        }
   
}

The whole idea is that we are leveraging GetFollowedCount  method of SocialFollowingManager class to get count of documents and sites and we are leveraging GetFollowed method of SocialManager class to actually find out the documents (in the above).

Invoke this class from Program.cs

static void Main(string[] args)
       {

           GetFollowers oGetFollowers = new GetFollowers();

           oGetFollowers.RetrieveFollowedCount();
       }

 

Run the application and we will see the results below.

image

 Subscribe to my blog

Leave a Reply