I have a console application with the following piece of code:
SPServiceContext serviceContext = SPServiceContext.GetContext(portalSite);
UserProfileManager profileManager = new UserProfileManager(serviceContext);
foreach (UserProfile u in profileManager)
{
if (u.PersonalSite == null)
{
try
{
portalSite.RootWeb.AllowUnsafeUpdates = true;
portalSite.RootWeb.EnsureUser(u["AccountName"].ToString());
u.CreatePersonalSite();
portalSite.RootWeb.AllowUnsafeUpdates = false;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
It works only for users who already logged in on the portal once before. I got the following error when I run that script for some users who didn't have access yet to the environment. The code crashes at "EnsureUser()".

If I skip the "EnsureUser()" method, the code crashes on the "CreatePersonalSite()" method with a PersonalSiteCreateException.
As far as I know, the "EnsureUser()" will ensure that the user exists. When a user enters the portal for the first time - a new record will be created in the User Information List. If we use this method after a user logged in for the first time, the user will exist. The same goes up for the u.CreatePersonalSite(). Is this correct? Am I missing something?
How can I create mysites for profiles who didn't access the portal before? Thanks in advance!!!!
Edit: I changed "poralSite" in the code fragment to "portalSite"
portalSite? Your MySite Host? Another site? If it's not the MySite Host, I'd try ensuring the user there first. – James Love Nov 13 '11 at 13:38