Following an user on behalf of someone in SharePoint 2013 social

In enterprise social scenarios, imposing following users on behalf of another user is actually a must. Just think of the internal procedures – everyone from HR needs to follow the head of the HR department, since he’s going to publish important info on the different social channels. Or, everyone in the sales department needs to follow the sales bot user, which regularly publishes the important events from the CRM system… hundreds of scenarios.

This is not possible to achieve with SharePoint Client Object Model. But it works just fine with Server Side Object Model:

using (SPSite site = new SPSite(siteName))

    SPServiceContext context = SPServiceContext.GetContext(site);

    UserProfileManager profileManager = new UserProfileManager(context);

    if (profileManager.UserExists(username))
        UserProfile userProfile = profileManager.GetUserProfile(username);

        //get the Social Feed Manager of the current user
        SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, context);

        //But, since the FollowingManager property of the feedManager is not aware of the user context, feed manager is in THIS scenario useless
        //we need to create SPSocialFollowingManager object in user context, and to use that for the following purposes
        SPSocialFollowingManager followingManager = new SPSocialFollowingManager(userProfile, context);

        SPSocialActorInfo actor = new SPSocialActorInfo();
        actor.ActorType = SPSocialActorType.User;
        actor.AccountName = tofollow;


        //to stop following an user, you would do something like this:

        Console.WriteLine(string.Format("User Profile for user {0} does not exist", username));