Monday, 29 July 2013

How to programmatically create mysites of all users of a group in sharepoint 2013

Hi,

Recently a requirement was posted to me where 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.

After this the tool will pick users of the group one by one and will check if mysite for that user exists or not. If not 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 as below:

using System;
using System.Collections.Generic;
using System.Web;

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);
            }
        }
    }

}


I hope this will help you out

Thanks,
Rahul Rashu







1 comment:

  1. Hi, Icant find the dll location for following namespace
    using Microsoft.SharePoint.Administration;

    also checked 15/ISAPI folder please suggest

    ReplyDelete