bool CreateMySiteOnServer(string serverUrl)
{
bool returnValue = false;
Logger.LogVerbose("CreateMySiteOnServer for URL:" + serverUrl);
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite lowPrivilegeSite = new SPSite(serverUrl);
SPSite site = new SPSite(serverUrl, lowPrivilegeSite.UserToken);
lowPrivilegeSite.Dispose();
using (site)
{
Logger.LogVerbose("Using Site:" + site.Url);
bool allowUnsafeUpdates = site.AllowUnsafeUpdates;
bool contextAllowUnsafeUpdates = SPContext.Current.Web.AllowUnsafeUpdates;
UserProfile profile;
try
{
site.AllowUnsafeUpdates = true;
SPContext.Current.Web.AllowUnsafeUpdates = true;
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(serviceContext);
if (false == profileManager.UserExists(GetCurrentUser()))
{
Logger.LogVerbose("Creating User Profile.");
profile = profileManager.CreateUserProfile(GetCurrentUser());
}
else
{
Logger.LogVerbose("Getting User Profile.");
profile = profileManager.GetUserProfile(GetCurrentUser());
}
// Create Personal Site
Logger.LogVerbose("Creating Personal Site.");
profile.CreatePersonalSite();
// If success, we set return value - failure to CreatePersonalSite will throw an exception
returnValue = true;
}
catch (Exception ex)
{
Logger.LogException("CreateMySite.CreatePersonalSite (inner try)", ex);
}
site.AllowUnsafeUpdates = allowUnsafeUpdates;
SPContext.Current.Web.AllowUnsafeUpdates = contextAllowUnsafeUpdates;
}
}
);
}
catch (Exception ex)
{
Logger.LogException("CreateMySite.CreatePersonalSite (outer try) ", ex);
}
return returnValue;
}