I am using below code for update SharePoint user profile
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 user profile but after complete this code execution error come in browser object reference not set to an instance of an object.
Why its happening? Any idea? It comes for any user update his own profile also. No error in code but error in browser.