2

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"

5
  • 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
    Commented Nov 13, 2011 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? Commented Nov 13, 2011 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... Commented Nov 13, 2011 at 15:22
  • @Mike: I'll check this later - but I have my doubts that the ULS logs will provide more useful info; Thx Commented Nov 13, 2011 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.. Commented Nov 13, 2011 at 16:02

2 Answers 2

2

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.

3
  • 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/… Commented Nov 14, 2011 at 7:13
  • Are you able to manually add those users(who didn't access the portal before) in any of the site groups? Commented Nov 14, 2011 at 9:30
  • Yes that works... Commented Nov 14, 2011 at 18:16
-1

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());
            }
        }
    }
}
3
  • I am not sure how to format my answer :)
    – Rajesh
    Commented Nov 14, 2011 at 3:05
  • ServerContext is obsolete in SP2010, We should use SPServiceContext instead. Anyway, SPContext cannot be established in this manner. Commented Nov 14, 2011 at 6:46
  • @Rajesh: I already pass an SPSite object to the SPServiceContext method - to create 'a context' Commented Nov 14, 2011 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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