Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

October 2014

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.”

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲