I am getting 401 Access denied error while getting userprofile info. I am running below code in the Web Services. Web Service application Identity is the SharePoint System Account.

private void UpdateUserInUserProfiles(Customer c, string personalSiteUrl, string siteCollectionUrl)
   SPSite mySites = null;
   SPWeb userPersonalSite = null;
      // SPSecurity.RunWithElevatedPrivileges(delegate()
           using (SPSite spSite = new SPSite(siteCollectionUrl))
              using(SPWeb spWeb = spSite.OpenWeb() )
                  Office.ServerContext serverContext = Office.ServerContext.GetContext(spSite);
                  UserProfileManager pmManager = new UserProfileManager(serverContext);
                  string strUserName = _providerName + c.CustomerNumber;
                  HttpContext context = HttpContext.Current;
                  if (HttpContext.Current != null)
                     if (context.Items["HttpHandlerSPWeb"] == null)
                         context.Items["HttpHandlerSPWeb"] = spSite.RootWeb;
                     if (context.Items["Microsoft.Office.ServerContext"] == null)
                          context.Items["Microsoft.Office.ServerContext"] = serverContext;
                   if (!pmManager.UserExists(strUserName))
                        UserProfile newUser1 = pmManager.CreateUserProfile(strUserName);
                        UserProfile newUser = pmManager.GetUserProfile(strUserName);
                        newUser["PreferredName"].Value = c.FullName;
                        newUser["FirstName"].Value = c.FirstName;
                        newUser["LastName"].Value = c.LastName;
                        newUser["WorkEmail"].Value = c.Email;
                       UserProfile updateUser = pmManager.GetUserProfile(strUserName);
                       updateUser["PreferredName"].Value = c.FullName;
                       updateUser["FirstName"].Value = c.FirstName;
                       updateUser["LastName"].Value = c.LastName;
                       updateUser["WorkEmail"].Value = c.Email;

            catch (Exception ex)

                _errorCount = _errorCount + 1;
                LogMessage("Exception Occured in UpdateUserInUserProfiles: " + GetErrorMessage(ex), EventLogEntryType.Error);
                if (userPersonalSite != null)

                if (mySites != null)


Exception That I am getting is :

Message: Exception Occured in UpdateUserInUserProfiles: Thread was being aborted. STACKTRACE :   at System.Threading.Thread.AbortInternal()
   at System.Threading.Thread.Abort(Object stateInfo)
   at System.Web.HttpResponse.End()
   at Microsoft.SharePoint.Utilities.SPUtilityInternal.SendResponse(HttpResponse response, Int32 code, String strBody)
   at Microsoft.SharePoint.Utilities.SPUtilityInternal.Send401(HttpResponse response)
   at Microsoft.SharePoint.Utilities.SPUtility.SendAccessDeniedHeader(Exception ex)
   at Microsoft.SharePoint.Utilities.SPUtility.EnsureAuthentication(SPWeb web)
   at Microsoft.Office.Server.UserProfiles.UserProfileGlobal.GetCurrentUserName()
   at Microsoft.Office.Server.UserProfiles.UserProfileManager.get_strCurrentAccountName()
   at Microsoft.Office.Server.UserProfiles.UserProfileManager.UserExists(String strAccountName)
   at SharePoint.Webservices.SharePointCustomerManagement.UpdateUserInUserProfiles(Customer c, String personalSiteUrl, String siteCollectionUrl)

1 Answer 1


Is this a web service running outside sharepoint? or is it deployed to sharepoint? Can you tell me why you have those hacks regarding the HttpContext in your code, that shouldn't be necessary?

I have kinda forgotten how it works in 2007, but in the SSP's profile permissions you can grant an account permission to iterate (loop) over profiles, I think the account needs to have "Manage user profiles" permission. The farm account does NOT automatically have this permission, the SSP service account does (separation of concerns).

  • Yes, Web service running in sharepoint environment only. I also remove hack code still it is not working. I just added farm account to Manage User Profiles. Still the same error.
    – James123
    Commented Jan 12, 2012 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.