SharePoint 2013: Programmatically Create MySites for all Users of a Group

SharePoint 2013: Programmatically Create MySites for all Users of a Group

Recently my site admins wanted to create MySites of all users of a Sharepoint group programmatically and this requirement was a recurring one. Hence I developed a tool to carry this out. This tool wants two inputs:
  1. Site URL (Where the user group is created)
  2. Name of the user group.
This tool will pick users of the group one by one and will check if mysite for that user exists or not. If it does not exist, a MySite would be created and a message would be published, similarly in case a MySite exists the same message would be published. Try catch blocks are used to catch and publish and exception message.

 The code is below:

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
 
namespace MysiteCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please enter the site URL");
                String url = Console.ReadLine();
                SPSite site = new SPSite(url);
                SPWeb web = site.OpenWeb();
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                Console.WriteLine("Please enter the group name");
                String groupName = Console.ReadLine();
                UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
                SPGroup group = web.Groups[groupName];
                SPUserCollection users = group.Users;
                foreach (SPUser user in users)
                {
                    if (userProfileManager.UserExists(user.LoginName))
                    {
                        UserProfile uProfile = userProfileManager.GetUserProfile(user.RawSid);
                        if (uProfile.PersonalSite != null)
                        {
                            Console.WriteLine("Mysite Already exists for the user " + uProfile.AccountName);
                        }
                        else
                        {
                            uProfile.CreatePersonalSite();
                            Console.WriteLine("Mysite successfully created for the user " + uProfile.AccountName);
                        }
                    }
                }
                Console.WriteLine("This is completed, please press any key to exit");
                Console.ReadLine();
            }
 
 
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


Other Languages

This article is also available in the following languages:

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
 
namespace MysiteCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please enter the site URL");
                String url = Console.ReadLine();
                SPSite site = new SPSite(url);
                SPWeb web = site.OpenWeb();
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                Console.WriteLine("Please enter the group name");
                String groupName = Console.ReadLine();
                UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
                SPGroup group = web.Groups[groupName];
                SPUserCollection users = group.Users;
                foreach (SPUser user in users)
                {
                    if (userProfileManager.UserExists(user.LoginName))
                    {
                        UserProfile uProfile = userProfileManager.GetUserProfile(user.RawSid);
                        if (uProfile.PersonalSite != null)
                        {
                            Console.WriteLine("Mysite Already exists for the user " + uProfile.AccountName);
                        }
                        else
                        {
                            uProfile.CreatePersonalSite();
                            Console.WriteLine("Mysite successfully created for the user " + uProfile.AccountName);
                        }
                    }
                }
                Console.WriteLine("This is completed, please press any key to exit");
                Console.ReadLine();
            }
 
 
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
 
namespace MysiteCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please enter the site URL");
                String url = Console.ReadLine();
                SPSite site = new SPSite(url);
                SPWeb web = site.OpenWeb();
                SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                Console.WriteLine("Please enter the group name");
                String groupName = Console.ReadLine();
                UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
                SPGroup group = web.Groups[groupName];
                SPUserCollection users = group.Users;
                foreach (SPUser user in users)
                {
                    if (userProfileManager.UserExists(user.LoginName))
                    {
                        UserProfile uProfile = userProfileManager.GetUserProfile(user.RawSid);
                        if (uProfile.PersonalSite != null)
                        {
                            Console.WriteLine("Mysite Already exists for the user " + uProfile.AccountName);
                        }
                        else
                        {
                            uProfile.CreatePersonalSite();
                            Console.WriteLine("Mysite successfully created for the user " + uProfile.AccountName);
                        }
                    }
                }
                Console.WriteLine("This is completed, please press any key to exit");
                Console.ReadLine();
            }
 
 
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}