SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi i am using below code for update sharepoint userprofile


        HttpContext con = HttpContext.Current;
        SPSite tempSite = new SPSite(SPContext.Current.Site.Url);
        SPUserToken superToken = tempSite.SystemAccount.UserToken;
        string sAccount = string.Empty;
        UserProfile profile = null;
        using (SPSite site = new SPSite(SPContext.Current.Site.Url, 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(spuser);
            sAccount = NewUser.LoginName;
            if (!profileManager.UserExists(sAccount))
                profile = profileManager.CreateUserProfile(sAccount);
                profile = profileManager.GetUserProfile(sAccount);
            web.AllowUnsafeUpdates = false;
        // profile["AccountName"].Value = createusert1.UserName;
        profile["FirstName"].Value = "Test";                    

catch (Exception ex) {}

this code works perfect and save data in userprofile but after complete this code execution error come in browser object reference not set to an instance of an object

why this done. any idea it's comes for any user update his own profile also. no error in code but error in browser. why? any Idea?

share|improve this question
up vote 3 down vote accepted

Your code set HttpContext.Current = null;, but do not restore it, use try-finally approach:

HttpContext con = null;
  con = HttpContext.Current;
catch(Exception ex){}
   HttpContext.Current = con;

And also, this code

SPSite tempSite = new SPSite(SPContext.Current.Site.Url);

leads to memory leaks, why not to use simply

share|improve this answer
thanks kai your solution working for me... – Vishal_Sharepoint May 28 '12 at 6:01

You can validate this using the Powershell as mentioned below.

Set-SPUser -Identity ‘Domain\userid’ -DisplayName ‘test’ –Web http://yoursitecollection

After you run this script will notice the display changes for the selected AD user. However post completion of the script, you will get an object not found shown in the powershell window.

Try this after you have run the above script and notice the changes to the user property.

Set-SPUser -Identity ‘domain\userid’ –Web http://yoursitecollection –SyncFromAD

share|improve this answer

Your Answer


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.