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

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.


string accountName = SPContext.Current.Web.CurrentUser.LoginName;"ProfileUserControl:btnSave_Click() Saving user profile properties.");
    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();
    HttpContext.Current = currentContext;
    //UserProfile userProfile = SPSparshService.GetUserProfile("");


share|improve this question
Ideally when is this done? – variable Mar 24 '14 at 12:19
Do you feel there is any drawback of using this on production server? – variable Mar 24 '14 at 12:21
Please post your added link as an answer (with an abstract), that i really good information! Questionable if this is still valid for 2013, but I would expect it to bee – Robert Lindgren Mar 24 '14 at 12:30
I am unable to understand the exact concept. Can you post the answer. – variable Mar 24 '14 at 12:32
up vote 3 down vote accepted

From the link you supplied:

In case your business needs require users apart from Administrators for User Profile Service Application to be able to create organization profiles (not sure why you’d want that though), the workaround is to set HttpContext.Current to null like code below.

SPSecurity.RunWithElevatedPrivileges(delegate() {
    HttpContext httpCtx = HttpContext.Current;
    HttpContext.Current = null;

    using (SPSite site = new SPSite("http://yourserver/"))
        // code omitted here
    } // end of using
    HttpContext.Current = httpCtx; }); 

The workaround makes the service account of User Profile Service Application to execute the code. The service account should be in Administrators for User Profile Service Application with Full Control permission.

So, setting the HttpContext to null should only be done when you have to let users not having "Manage Profiles" permissions on the UPS to do changes to UPS.

The fact that RunWithElevatedPrivileges does not work is probably due to the UserProfileManager not being part on the Microsoft.SharePoint API, but rather the Microsoft.Office.Server API.

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.