Disclaimer

These steps represent my own limited testing. Make sure you are aware of consequences of executing the cmdlets below before you try them on your dev or production tenant.


Challenge


There are several ways to add a followed site in the context of a user, but none that would let us impersonate the user when following a site.  Faced recently with a bit of a conundrum when migrating sites from on-prem to online, I found a hidden gem in the OneDrive Social list.   



Social List


In every user's personal site there is a list called Social. The list is created after the user has followed an item, file or a site. Within the list you can see 2 folders: Private and FollowedSites.

Within the FollowedSites folder you can find sites a user is following. An example of that is item no 3 - Communication site.


 

Getting followed items


In order to get followed items, get the items of a Social List. The property FsObjType allows you to distinguish between folders and files. Choose 1 for folders. Mind the syntax:
Get-PnPListItem -List Social | where {$_['FSObjType'] -eq 1}

 

Choose 0 for items:
Get-PnPListItem -List Social | where {$_['FSObjType'] -eq 0}


For single items you can get their fieldvalues, where under URL property you can find the site url that the user is following:
(Get-PnPListItem -List Social -Id 422).FieldValues




Get all followed sites' urls


$FollowedItems = Get-PnPListItem -List Social | where {$_['FSObjType'] -eq 0}
 
foreach($FollowedItem in $FollowedItems) {
   Write-Host $FollowedItem.FieldValues['Url']
}

If you want to have the urls exported to a file, use the following lines:
$FollowedItems = Get-PnPListItem -List Social | where {$_['FSObjType'] -eq 0}
   
foreach($FollowedItem in $FollowedItems) {
Out-File -FilePath C:\users\Public\followedsites.txt -InputObject $FollowedItem.FieldValues['Url'] -Append
}

You can also create a custom object to have a list of urls matched to a username.    


Adding followed items


In order to add a followed site, create an item in the Social List:

Add-PnPListItem -List Social -Folder Private\FollowedSites  -Values @{"Title" = "TestTitle"; "Url = "https://test.sharepoint.com"; "SiteId" = "000-0000-4234234"; "ContentTypeId" = "0x011fc478237948739498327"}




See Also