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 Accessdenied error. while setting value to user profile property. This method in my custom web service. running on System Account application pool identity.

Execption:

***

Before spSiteUserToken.SystemAccount.UserToken; 
     After spSiteUserToken.SystemAccount.UserToken; 
     Inside using (SPSite spSite 
     After HttpContext.Current = null; 
     Access Denied: You may only modify your own profile, unless you are an administrator. STACKTRACE :   at Microsoft.Office.Server.UserProfiles.UserProfileValueCollection.CheckUpdatePermissions() 
       at Microsoft.Office.Server.UserProfiles.UserProfileValueCollection.set_Value(Object value) 
     at SharePoint.Webservices.SharePointCustomerManagement.<>c__DisplayClass16.<UpdateUserInUserProfiles>b__12() 
      at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state) 
      at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2() 
      at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) 
     at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) 
      at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) 
       at SharePoint.Webservices.SharePointCustomerManagement.UpdateUserInUserProfiles(Customer c, String personalSiteUrl, String siteCollectionUrl)

 private void UpdateUserInUserProfiles(Customer c, string personalSiteUrl, string siteCollectionUrl)
    {
        SPSite mySites = null;
        SPWeb userPersonalSite = null;
        HttpContext saved = HttpContext.Current;

        try
        {
            SPUserToken res = null;
            SPSite spSiteUserToken = new SPSite(siteCollectionUrl);
            bool oldCatchAccessDeniedException = spSiteUserToken.CatchAccessDeniedException;
            try
            {
                Debug.WriteLine("Before spSiteUserToken.SystemAccount.UserToken;");
                spSiteUserToken.CatchAccessDeniedException = false;
               // res = spSiteUserToken.SystemAccount.UserToken;
                res = GetSystemToken(spSiteUserToken);
                Debug.WriteLine("After spSiteUserToken.SystemAccount.UserToken;");
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                   using (SPSite spSite = new SPSite(siteCollectionUrl, res))
                   {
                         Debug.WriteLine("Inside using (SPSite spSite");
                         Office.ServerContext serverContext =
                         Office.ServerContext.GetContext(spSite);

                         UserProfileManager pmManager =
                         new UserProfileManager(serverContext);
                         string strUserName = _providerName +
                         c.CustomerNumber;
                         spSite.AllowUnsafeUpdates = true;
                         HttpContext.Current = null;
                         Debug.WriteLine("After HttpContext.Current = null;");
                         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();
                                                               _sbAuditTrail.Append("User profile updated in ProfileDatabase: " + siteCollectionUrl + " : ");
                       }
                       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();
                                                                                          _sbAuditTrail.Append("User profile updated in ProfileDatabase: " + siteCollectionUrl + " : ");
                       }
                       spSite.AllowUnsafeUpdates = false;
                   }
               });

            }
            catch (Exception ex)
            {
                Debug.WriteLine(GetErrorMessage(ex));                    
            }
            finally
            {
                spSiteUserToken.CatchAccessDeniedException = oldCatchAccessDeniedException;
            }

        }
        catch (Exception ex)
        {

            _errorCount = _errorCount + 1;
            _sbErrors.Append(GetErrorMessage(ex));
            LogMessage("Exception Occured in UpdateUserInUserProfiles: " + GetErrorMessage(ex), EventLogEntryType.Error);
        }
        finally
        {
             HttpContext.Current = saved;

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

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

    }
share|improve this question

2 Answers 2

Is the account a UPA Administrator? Check the settings on the Service Application.

share|improve this answer
up vote 0 down vote accepted

I solved this issue by moving HttpContext.Current = null; to top of Office.ServerContext serverContext = Office.ServerContext.GetContext(spSite);

Now it is working good.

share|improve this answer

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.