How to Follow a site URL with PowerShell in SharePoint 2013

Background

While migrating from SharePoint 2010 to 2013 we needed to take MyLinks and replace with new Social Site Following.   Given a CSV with user names and site URLs I researched PowerShell options for programmatically making users follow sites.   Now we can pre-populate Follow Sites before launch.   The below code did the trick.  shades_smile

 

Code

Function AddSiteToUserFollow ($siteUrl, $userName) {
	# Load site and user profile service
	$site = Get-SPSite $siteUrl
	$serviceContext = Get-SPServiceContext($site)
	$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
	if ($profileManager.UserExists($userName)) {
		# Load user profile
		$userProfile = $profileManager.GetUserProfile($userName)
		
		# Load social following manager
		$followingManager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($userProfile, $serviceContext)
		
		# Prepare following object
		$actorInfo = New-Object Microsoft.Office.Server.Social.SPSocialActorInfo
		$actorInfo.ContentUri = $site.Url
		$actorInfo.AccountName = $userProfile.AccountName
		$actorInfo.ActorType = "Site"
		
		# Add to followed sites
		$followingManager.Follow($actorInfo)
	}
}
AddSiteToUserFollow "http://sp15" "demo\sptest1"

 

Screenshots

image

image

References

Support

If the target user does not yet have a MySite created you could see the below error.  The fix is simply to have them visit MySite – or – to upgrade their site collection from 2010 to 2013.

Exception calling “Follow” with “1” argument(s): “No personal site exists for the current user, and no further information is available. Internal type name: Microsoft.Office.Server.UserProfiles.SocialDataStoreException. Internal error code: 1.”

3 Comments

      1. Hi Jeff, thanks for the reply! I have to be honest and admit that powershell is not my strong side here. I would be over the moon if you were able to help me abit with some code here 🙂 We have just gone live with SP2013 and we are having huge issues getting users to follow important tags to actually utilize the information that`s available.
        Do you think you could help me out and get me on my way? (almost on my knees here) 🙂

Leave a Comment

Return to Top ▲Return to Top ▲