Sharepoint 2013: How to Programmatically Create Mysites for all Users of a Group

Sharepoint 2013: How to 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 takes 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);
            }
        }
    }
}
Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Naomi  N edited Revision 1. Comment: Minor edit

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Naomi  N edited Revision 1. Comment: Minor edit

Page 1 of 1 (1 items)