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

Hi i am using below code for update sharepoint userprofile

try
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
      {

        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);
            }
            else
            {
                profile = profileManager.GetUserProfile(sAccount);
            }
            web.AllowUnsafeUpdates = false;
        }
        // profile["AccountName"].Value = createusert1.UserName;
        profile["FirstName"].Value = "Test";                    
        profile.Commit();

        });
}
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

2 Answers 2

up vote 3 down vote accepted

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

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

And also, this code

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

leads to memory leaks, why not to use simply

SPContext.Current.Site.SystemAccount.UserToken;
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

 
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.