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.
- 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.
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- 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.
- #Setting Credentials for o365 - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Setting Credentials for o365 - End
- 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.
- #Credentials for on premise site - Start
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)
- $ctx.Credentials = $creds
- #Credentials for on premise site - End
Retrieve Current User Followers
- Initialize the people manager object with the context to get the user profile data.
- With people manager object, use GetMyFollowers() method to retrieve current user (User running the script) followers information.
- Load the object and execute query to get the data.
- In the below code snippet, $follower will hold the basic profile information of user and $follower.UserProfileProperties will hold the entire property set of a particular user.
- # Get My Follower Profiles & Properties
- function GetMyFollowers(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $myFollowers = $peopleManager.GetMyFollowers()
- $ctx.Load($myFollowers)
- $ctx.ExecuteQuery()
- Write-Host "I have " $myFollowers.Count " followers"
- foreach($follower in $myFollowers){
- Write-Host "Display Name : " $follower.DisplayName
- Write-Host "Title : " $follower.Title
- $followerProfProp = $follower.UserProfileProperties
- # Other User Profile properties can be retrieved from $follower.UserProfileProperties using foreach loop
- Write-Host "Interests : " $follower.UserProfileProperties["SPS-Interests"]
- }
-
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor Red
- }
- }
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.
- # Get People current user is following
- function GetMyFollowing(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $myfollowingUsers = $peopleManager.GetPeopleFollowedByMe()
- $ctx.Load($myfollowingUsers)
- $ctx.ExecuteQuery()
- Write-Host "I am following " $myfollowingUsers.Count " People"
- foreach($myfollowingUser in $myfollowingUsers){
-
- Write-Host "Display Name : " $myfollowingUser.DisplayName
- Write-Host "Title : " $myfollowingUser.Title
-
- # Other User Profile properties can be retrieved from $follower.UserProfileProperties using foreach loop
- Write-Host "Interests : " $myfollowingUser.UserProfileProperties["SPS-Interests"]
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor Red
- }
- }
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.
- Initialize the people manager object with the context to get the user profile data.
- User GetFollowersFor(user) method with user ID and people manager to retrieve followers information about target user.
- Load the object and execute query to get the data.
- # Get Followers for target user
- function GetFollowers(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $followers = $peopleManager.GetFollowersFor("domain\userid")
- $ctx.Load($followers)
- $ctx.ExecuteQuery()
- Write-Host "User is followed by " $followers.Count " People"
- foreach($follower in $followers){
-
- Write-Host "Display Name : " $follower.DisplayName
- Write-Host "Title : " $follower.Title
-
- # Other User Profile properties can be retrieved from $follower.UserProfileProperties using foreach loop
- Write-Host "Interests : " $follower.UserProfileProperties["SPS-Interests"]
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor Red
- }
- }
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.
- The logic is same as that of above section. But use GetPeopleFollowedBy(UserID) method to get the target user's following data (people followed by user).
- # Get People following
- function GetFollowing(){
- try{
- $peopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($ctx)
- $followingUsers = $peopleManager.GetPeopleFollowedBy("domain\userid") # Login id of SharePoint account
- $ctx.Load($followingUsers)
- $ctx.ExecuteQuery()
- Write-Host "User is following " $followingUsers.Count " People"
- foreach($followingUser in $followingUsers){
-
- Write-Host "Display Name : " $followingUser.DisplayName
- Write-Host "Title : " $followingUser.Title
-
- # Other User Profile properties can be retrieved from $follower.UserProfileProperties using foreach
- Write-Host "Interests : " $followingUser.UserProfileProperties["SPS-Interests"]
- }
- }
- catch{
- Write-Host "$($_.Exception.Message)" -foregroundcolor Red
- }
- }
Note - The above set of operations needs to be called to get it executed.
- GetMyFollowers # Get my followers
- GetFollowers # Get Followers
- GetMyFollowing # Get my following
- 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.