Domanda

I m trying to update the user profile "State" custom property using below code but getting following error.

We have even tried to restart the user profile service but it couldn't fix the issue.

Any help is highly appreciated.

 public static void UpdateUserProfile(SPSite site, string sAccount, string State)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    using (SPSite _site = new SPSite(site.ID))
                    {
                        var serviceContext = SPServiceContext.GetContext(site);
                        var profileManager = new UserProfileManager(serviceContext);
                        if (!profileManager.UserExists(sAccount)) profileManager.CreateUserProfile(sAccount);
                        var userProfile = profileManager.GetUserProfile(sAccount);
                        userProfile["State"].Value = State;
                        userProfile.Commit();
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("ERROR {0} : {1}.", System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message));
            }
        }

An exception of type 'Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code

Additional information: UserProfileDBCache_WCFLogging :: ProfileDBCacheServiceClient.GetUserData threw exception: Access is denied.

È stato utile?

Soluzione

You should firstly check the permission your code running for retrieve User Profile information.

SPSecurity.RunWithElevatedPrivileges Method is used for running the code block under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.

In your code snippet, UserProfileManager object is used, so please authorize permission(in ServiceApplications-->User Profile Service Application) to this site collection administrator.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top