we are trying to access the user profile properties via SOM. Code is taken from reference as mentioned below.
My question is: Why is HttpContext.Current set to null?. Code works even if we comment out this line. What is the purpose?
There is an article about why it is set to null here.
Code:
string accountName = SPContext.Current.Web.CurrentUser.LoginName;
Logger.info("ProfileUserControl:btnSave_Click() Saving user profile properties.");
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite sc = new SPSite(SPContext.Current.Site.ID);
SPServiceContext context = SPServiceContext.GetContext(sc);
//ServerContext context = ServerContext.GetContext(sc);
HttpContext currentContext = HttpContext.Current;
HttpContext.Current = null;
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile userProfile = profileManager.GetUserProfile(accountName);
userProfile[PropertyConstants.WorkPhone].Value = txtWorkPhone.Text.Trim();
userProfile[PropertyConstants.CellPhone].Value = txtMobilePhone.Text.Trim();
userProfile[PropertyConstants.HomePhone].Value = txtHomePhone.Text.Trim();
userProfile[PropertyConstants.School].Value = txtSchool.Text.Trim();
userProfile[PropertyConstants.Skills].Value = txtSkills.Text.Trim();
userProfile.Commit();
HttpContext.Current = currentContext;
//UserProfile userProfile = SPSparshService.GetUserProfile("");
});
Reference: http://www.fewlines4biju.com/2011/01/how-to-save-user-profile-properties.html