3

We currently have groups of users that we'd like to push news out to their newsfeed. Obviously, their newsfeed is personal, but we've been selling it as the one stop shop for news pertinent for them. But this would require users to go around and follow all the announcements / blogs / etc. that we'd like to push their way.

Is there a way to change what a user follows without being that person?

Update Here's the eventual code I wrote inside a web service deployed to the farm. This code works for making a user follow a site, a document, tag, or user. It doesn't get a user to get notifications for a document library. I believe that would an alert setting somewhere. But I thought I'd post some code for others in case they needed this functionality.

var web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            using (var site = SPContext.Current.Site) {

                SPUtility.ValidateFormDigest();
                SPSecurity.RunWithElevatedPrivileges(delegate() {

                    var spServiceContext = SPServiceContext.GetContext(site);
                    var profileManager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(spServiceContext);
                    var userProfile = profileManager.GetUserProfile(parameters.username);
                    var followingManager = new Microsoft.Office.Server.Social.SPSocialFollowingManager(userProfile, spServiceContext);
                    var socialActor = new Microsoft.Office.Server.Social.SPSocialActorInfo();

                    socialActor.ContentUri = new System.Uri(parameters.listUrl);
                    socialActor.AccountName = userProfile.AccountName;
                    switch (parameters.urlType) {
                        case "site": socialActor.ActorType = Microsoft.Office.Server.Social.SPSocialActorType.Site; break;
                        case "document": socialActor.ActorType = Microsoft.Office.Server.Social.SPSocialActorType.Document; break;
                        case "user": socialActor.ActorType = Microsoft.Office.Server.Social.SPSocialActorType.User; break;
                        case "tag": socialActor.ActorType = Microsoft.Office.Server.Social.SPSocialActorType.Tag; break;
                    }

                    ret += "FollowingManagerMore: " + followingManager.FollowedSitesUri.ToString() + "<br/>";
                    if (!followingManager.IsFollowed(socialActor)) {
                        followingManager.Follow(socialActor);
                    }
                });

and the service contract part looks like this:

 [OperationContract]
        [WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Bare)]
        string MakeUserFollow(UseFollowParameters parameters);
1

It appears you are not the first person to have this requirement. Here is an example from http://blog.areflyen.no/2014/04/04/following-sites-using-powershell-and-the-social-api-in-sharepoint-2013/ using PowerShell to force all users to follow a site:

# Get UserProfile Manager
$site = Get-SPSite -Limit 1
$serviceContext = Get-SPServiceContext($site)
$profileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
$profiles = $profileManager.GetEnumerator()

# Iterates through all the user profiles
foreach ($profile in $profiles)
{
    $followingManager = New-Object Microsoft.Office.Server.Social.SPSocialFollowingManager($profile)

    # Create a new social actor object for the site to follow
    $socialActor = New-Object Microsoft.Office.Server.Social.SPSocialActorInfo
    $socialActor.ContentUri = "http://intranet/sites/important-news-from-corp" # REPLACE THIS WITH YOUR SITE
    $socialActor.ActorType = [Microsoft.Office.Server.Social.SPSocialActorType]::Site

    # Follow the mandatory site
    if (!$followingManager.IsFollowed($socialActor))
    {
        $followingManager.Follow($socialActor)
    }
}
  • That looks like what I want, but I get a Exception calling "IsFollowed" with "1" argument(s): "The target of the operation was not found. Internal type name: Microsoft.Office.Server.UserProfiles.SocialDataStoreException. Internal error code: 0." I'm running as the installer account. Does this code work for you? – jkandiko Sep 3 '14 at 15:25
  • I can't test because I don't have SP 2013 available. Is $socialActor valid? Here is a C# code sample using the IsFollowed method: blogs.msdn.com/b/ocarpen/archive/2012/08/04/… – Justin Russell Sep 3 '14 at 16:11
  • Not quite what I was looking for, but it provided the path to do it via c#. – jkandiko Oct 27 '14 at 16:49
  • I think this script is also on github : gist.github.com/aflyen/10836851 – Nullldata Apr 10 at 14:13

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.