Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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()".

enter image description here

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"

share|improve this question
    
What is 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
    
Can you check the ULS logs to see if there are any other details about the error generated on the CreatePersonalSite() method? –  Mike Oryszak Nov 13 '11 at 14:45
    
@James: portalSite = intranet site collection; I already tried to pass the MySite Host as an SPSite obj , but no help... I haven't done both though - ensure user on both site collections... –  Sven Gillis Nov 13 '11 at 15:22
    
@Mike: I'll check this later - but I have my doubts that the ULS logs will provide more useful info; Thx –  Sven Gillis Nov 13 '11 at 15:23
7  
Pre creating my sites is a *really bad idea, and considered a bad practice. You end up with a lot of site collections to maintain that will probably never be used.. –  Anders Rask Nov 13 '11 at 16:02
add comment

2 Answers

It is the problem with your console application where SPContext won't be there . You can create SPSite collection with URL and try directly.

using (SPSite site = new SPSite("http://myspsURL"))
{
    serviceContext serviceContext= ServerContext.GetContext(site);
    UserProfileManager userprofileManager = new UserProfileManager(serviceContext);
    UserProfile u = profileManager.GetUserProfile(sAccount);
    u.CreatePersonalSite();
    foreach (UserProfile u in profileManager)
    {
        if (u.PersonalSite == null)
        {        
            try
            {   bool allowsafeupdate = portalSite.RootWeb.AllowUnsafeUpdates 
                //http://solutionarchitects.org/articles/2011/02/24/better-way-to-do-spweb-allowunsafeupdates.htm   
                portalSite.RootWeb.AllowUnsafeUpdates = true;
                portalSite.RootWeb.EnsureUser(u["AccountName"].ToString());
                u.CreatePersonalSite();
                portalSite.RootWeb.AllowUnsafeUpdates = allowsafeupdate ;
            }
            catch(Exception ex)
            {
                 Console.WriteLine(ex.ToString());
            }
        }
    }
}
share|improve this answer
    
I am not sure how to format my answer :) –  Rajesh Nov 14 '11 at 3:05
    
ServerContext is obsolete in SP2010, We should use SPServiceContext instead. Anyway, SPContext cannot be established in this manner. –  Amit Kumawat Nov 14 '11 at 6:46
    
@Rajesh: I already pass an SPSite object to the SPServiceContext method - to create 'a context' –  Sven Gillis Nov 14 '11 at 7:15
add comment

SPWeb.EnsureUser works in conjunction with membership and role providers which are configured in web.config and so You will need HttpContext( or SPContext) for it. Normally, It will not work in console application because HttpContext in console app is null!

It will work in codebehind of a webpage in the site collection as HttpContext is available.

To use SPWeb.EnsureUser in console application, you will need to

  1. Create app.config for console application.

  2. Copy the system.web>rolemanager and system.web>memebership sections in app.config

  3. Try to run console application with above app.config settings.
share|improve this answer
    
Thanks Amit for your answer, but I don't use a custom membership / role provider. As stated in the post of Mastykarz - You only need that extra web.config if you have a custom membership / role provider blog.mastykarz.nl/… –  Sven Gillis Nov 14 '11 at 7:13
    
Are you able to manually add those users(who didn't access the portal before) in any of the site groups? –  Amit Kumawat Nov 14 '11 at 9:30
    
Yes that works... –  Sven Gillis Nov 14 '11 at 18:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.