Follow a document in Office 365 SharePoint site using .NET CSOM

Home | Office 365 | Follow a document in Office 365 SharePoint site using .NET CSOM

In this article, I’d be covering on how to programmatically follow a document in Office 365 SharePoint Site using .NET Client Side Object Model (CSOM). I will be using SharePointOnlineCredentials available in Microsoft.SharePoint.Client.Runtime assembly to connect to Office 365 SharePoint site. In my previous posts related to Office 365, I was leveraging the MSOnlineClaimsHelper (Active Authentication) by Wictor Wilen, which is still valid and I believe the same functionality has been brought into SharePointOnlineCredentials class..

Create a Console Application in Visual Studio 2013 and name it as FollowDocument

Create a new class by name FollowDocument.cs

Add reference to the following assemblies in FollowDocument.cs

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

Add the following namespaces to FollowDocument.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using Microsoft.SharePoint.Client.UserProfiles;
using Microsoft.SharePoint.Client.Social
;

Add a public method named StartFollowing with following code snippet

using (var context = new ClientContext(“https://sharepointsundar.sharepoint.com/”))
            {
                String docurl = “
https://yoursite.sharepoint.com/Shared%20Documents/HOL_High%20Trust%20Provider%20App.docx”;
             
                String UserName = userid@yoursite.onmicrosoft.com;
                String Password = “your password”;

                SecureString SecurePassword = GetSecureString(Password);                            
                context.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);              

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

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

                //Check whether the current user is already following the item
                ClientResult<bool> bFollowFlag = oSocialFollowingManager.IsFollowed(oSocialActorInfo);
                context.ExecuteQuery();
                Console.WriteLine(“Is the current user following this document:” + bFollowFlag.Value);

                if (! bFollowFlag.Value)
                {
                    //If the document is not already followed by the user, start following it
                    ClientResult<SocialFollowResult> oResult = oSocialFollowingManager.Follow(oSocialActorInfo);
                    context.ExecuteQuery();
                }                                          

                               Console.ReadLine();           
            }
       
        }

In the above method, we are connecting to Office 365 SharePoint site using SharePointOnlineCredentials class, instantiating the SocialFollowingManager class and the SocialActorInfo class. We check for the status of Following of a Document by a user using ‘IsFollowed’ property of SocialFollowManager. Based on the Follow Status, we invoke ‘Follow’ method on SocialFollowManager class, to follow a document.

The next step is to Implement a Private method called GetSecureString() to get the SecureString object (representing the  password of Office 365 SharePoint site).

private static SecureString GetSecureString(String Password)
       {
           SecureString oSecurePassword = new SecureString();

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

           }
           return oSecurePassword;
       }

 

Finally, we invoke the FollowDocument.StartFollowing method from Main method in Program.cs

static void Main(string[] args)
        {

            FollowDocument oFollow = new FollowDocument();
            oFollow.StartFollowing();
           
        }

image

 Subscribe to my blog

Leave a Reply