Change MySite portal connection for all users in SharePoint

A MySite is a site specific for a user in SharePoint. The MySite portal connection allows to specify the parent site of the MySite. This usually appears on a breadcrumbs menu at the top of the page. Basically, it should point to the parent portal. Sometimes you need to change this connection.

If you need to change the MySite portal connection for just one user it is easy to do it through the Site Settings option. If you want to do that for all the users that will create a MySite in the future, then that is easy too (you can view an example on the link provided above). But if you want to change the MySite portal connection for all existing users, then the easiest way to do it is programmatically.

In short, here is the code that will let you change all MySite portal connection for all the users on a given web site:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
	using (SPSite web = new SPSite(webUrl))
	{
		ServerContext serverContext = ServerContext.GetContext(web);
		UserProfileManager profileManager = new UserProfileManager(serverContext);

		foreach (UserProfile userProfile in profileManager)
		{
			using (SPSite mySite = userProfile.PersonalSite)
			{
				if (mySite != null)
				{
					mySite.PortalUrl = portalUrl;
					mySite.PortalName = portalName;
				}
			}
		}
	}
});

 

The code is pretty straightforward and easy to understand. If you want to remove the portal connection just give a null value to PortalUrl.

Important note: you need to have sufficient privileges to perform this action on the server where you are running this application (privileges to change the user profiles, etc.).

I've made a console application (executable and source code available at the end) that you can run on the server and it will let you change or remove the portal connection for all MySites of a specific web. You just need to specify the arguments to the executable.

The console application supports the following options:

-change [web URL] [new portal URL] [new portal name]
Changes the portal connection for each MySite of all the users in the given web.
 
-remove [web URL]
Removes the portal connection for each MySite of all the users in the given web.
 
-help | -h | -?
Displays help information.

 

You can also use the SPPortalConnection tool available in ClodePex. Haven't tested it yet, but it has a feature that can be deployed.