1

I found this script at MSDN forum

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
$web = Get-SPWeb https://www.contoso.com/teams/yourteam
$context = [Microsoft.SharePoint.SPServiceContext]::GetContext([Microsoft.SharePoint.SPSite]$web.Site)
$upm =   New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager $context 
$users = $web.AssociatedMemberGroup.Users | where IsDomainGroup -eq $false
$useraccounts = $users | select loginname | Foreach {"$($_.loginname)"}

foreach ($user in $users) {
    $profile = $upm.GetUserProfile($user.UserLogin) 
    #$objectToFollowRelativeToWeb = "/"
    if ($profile)
    {
        #Create a Social Manager profile
        #SPSocialFollowingManager 
        $followManager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($profile, $context)
        $following = $followManager.GetFollowed([Microsoft.Office.Server.Social.SPSocialActorTypes]::Users)
        for ($i=$following.Count - 1; $i -ge 0; $i--) {
            $followed = $following[$i]
            $acctname = $followed.AccountName
            #if claims:
            $acctname = 'i:0#.w|'+$acctname
            if ($acctname -iin $useraccounts ) {
                [Microsoft.Office.Server.Social.SPSocialFollowResult]$res = $followManager.StopFollowing($followed)
                Write-Verbose "$($user.UserLogin) stop following $acctname result is $res"
            }
        }

    }
}

I believe the logic is correct and all the module being called seems correct. However after I run above script on my fine nothing have changed. The users are still following the same users.

After some investigation, under PeopleManager there is also StopFollowing method. (the script above is calling SocialFollowingManager.StopFollowing) Do you think this two "Stopfollowing" are running differently? How could I fix above script?

1

The following revised script will work, if you want to stop all users from following other users. The problem with the original script was that .StopFollowing() requires a SPSocialActorInfo object and not an SPSocialActor, which is non-intuitive. Also important is that the user who runs this script must have permissions on the user profile service application in Central Administration. To make it easy, grant yourself full control by selecting the user profile service and clicking on the permissions button in the ribbon. Or otherwise run the script as a user who does have full control permissions over the user profile service application.

$x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
$x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
$x= [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
$x= [System.Reflection.Assembly]::LoadWithPartialName("System.Web")
#
Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
$url = "http://sp2013dev/"
$site = Get-SPSite $url
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$profiles = $profileManager.GetEnumerator()

while ($profiles.MoveNext()) {
  $profile = $profiles.Current
   if ($profile)
    {
        #Create a Social Manager profile
        #SPSocialFollowingManager 
        $followManager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($profile, $context)
        $following = $followManager.GetFollowed([Microsoft.Office.Server.Social.SPSocialActorTypes]::Users)
        for ($i=$following.Count - 1; $i -ge 0; $i--) {
            $follower = $following[$i]

            $finfo =  New-Object Microsoft.Office.Server.Social.SPSocialActorInfo
            $finfo.AccountName = $follower.AccountName
            $finfo.ActorType = $follower.ActorType

            #if claims:
            #$acctname = 'i:0#.w|'+$acctname

            $res = $followManager.StopFollowing($finfo)
            Write-Host "$($finfo.AccountName) stop following $($profile.DisplayName) result is $res"

        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.