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

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;
   try
   {
      // 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;
                        newUser.Commit();
                   }
                   else
                   {
                       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;
                       updateUser.Commit();
                     }                           
                 }

               }
                //});
            }
            catch (Exception ex)
            {

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

                if (mySites != null)
                {
                    mySites.Close();
                }
            }

        }

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)
share|improve this question
add comment

1 Answer

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).

share|improve this answer
    
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 Jan 12 '12 at 23:18
add comment

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.