I am trying to create MySites for users programmatically but I am receiving the following error:
My Site creation failure for user '' for site URL 'http://goaccel-app2:1000/sites/mysites/personal/'.
Exception:
Microsoft.Office.Server.UserProfiles.PersonalSiteCreateException: A failure was encountered while attempting to create the site. ---> Microsoft.SharePoint.SPException: User cannot be found.
I am using Forms Based Authentication (FBA) with SharePont 2010 Foundation
This is the code I am using:
public void MySite(SPUser AdminUser,string username, string sitehost)
{
sitehost agrument given <mysitehost domain>
var superToken = AdminUser.UserToken;
UserProfile profile = null;
HttpContext con = HttpContext.Current;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string sAccount = string.Empty;
using (SPSite site = new SPSite(sitehost, superToken))
{
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPServiceContext context = SPServiceContext.GetContext(site);
HttpContext.Current = null;
UserProfileManager profileManager = new UserProfileManager(context);
// SPUser NewUser = web.EnsureUser(web.CurrentUser.LoginName);
SPUser NewUser = web.EnsureUser(username);
SPUser NewUser = web.EnsureUser(web.CurrentUser.LoginName);
sAccount = NewUser.LoginName;
if (profileManager.UserExists(sAccount))
{
profile = profileManager.GetUserProfile(sAccount);
if (profile.PersonalSite == null)
{
profile.CreatePersonalSite();
SPSite newSite = new SPSite(profile.PersonalSite.ID);
SPWeb newWeb = newSite.RootWeb;
newWeb.SiteAdministrators.Add(NewUser.LoginName, NewUser.Email, NewUser.Name, NewUser.Notes);
newWeb.Features.Add(new Guid("9DCBD980-1FE9-421B-90FD-4FE766D02A65"));
}
}