Introduction

In this article, you will learn how to retrieve people following (followed by user) or followers information from SharePoint user profile programmatically in different ways using CSOM with PowerShell on SharePoint 2013 / SharePoint online.

SharePoint My Sites should be configured on the environment as a prerequisite.

Steps Involved

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  1. Add the references using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and UserProfile dll.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
  2. Initialize client context object with the site URL.
    1. $siteURL = ""
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
  3. If you are trying to access SharePoint Online site then you need to setup the site credentials with credentials parameter and load it to the client context.
    1. #Setting Credentials for o365 - Start
    2. $userId = ""
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
    5. $ctx.credentials = $creds
    6. #Setting Credentials for o365 - End
  4. If you are trying to access the SharePoint on premise site then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
    1. #Credentials for on premise site - Start
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString
    3. $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)
    4. $ctx.Credentials = $creds
    5. #Credentials for on premise site - End

Retrieve Current User Followers

Retrieve Current user following data

In the above section, we have seen that how we can get current user followers. Now we will see how we can retrieve following (people followed by user) information of a user who is running the script. The steps followed are very similar to the above section. Only the method is changed to get the target user properties.

  1. # Get People current user is following
  2. function GetMyFollowing(){
  3. try{
  4. $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
  5. $myfollowingUsers = $peopleManager.GetPeopleFollowedByMe()
  6. $ctx.Load($myfollowingUsers)
  7. $ctx.ExecuteQuery()
  8. Write-Host "I am following " $myfollowingUsers.Count " People"
  9. foreach($myfollowingUser in $myfollowingUsers){
  10. Write-Host "Display Name : " $myfollowingUser.DisplayName
  11. Write-Host "Title : " $myfollowingUser.Title
  12. # Other User Profile properties can be retrieved from $follower.UserProfileProperties using foreach loop
  13. Write-Host "Interests : " $myfollowingUser.UserProfileProperties["SPS-Interests"]
  14. }
  15. }
  16. catch{
  17. Write-Host "$($_.Exception.Message)" -foregroundcolor Red
  18. }
  19. }

Retrieve Followers for Target User

In the above sections, we have seen how we can retrieve followers or following information of user who is running the script. Now, we will see how we can get the follower information for target users.

Retrieve Following User Information for Target User

In the above section, we have seen how we can retrieve followers information for target user. Now, we will see, how we can get the following information for target users.
Note - The above set of operations needs to be called to get it executed.
  1. GetMyFollowers # Get my followers
  2. GetFollowers # Get Followers
  3. GetMyFollowing # Get my following
  4. GetFollowing # Get Following
Summary

Thus, you have learned how to retrieve people following or people followed by user information with various approaches using CSOM with PowerShell on SharePoint 2013 / SharePoint online sites.