Dynamics CRM - in C # Plugin System Administrator privileges in order to update the Entity

Scene Description:

      1. When using CRM systems, there is often a need to be in the other Entity Entity of Record update, or update itself in the post, where you need to use update () method (the SDK or updateObject ( ) method), but the implementation of update method, you need to User Entity to be updated have permission to Assign to the;

      2. Due to the CRM system can be configured for User privileges, so the User may not have the Assign to correspond Entity authority now requires basic configuration permissions do not change the User on the update of the Entity (execution update).

      Based on the above two points, we can use the System Administrator to perform the update operation Plugin, the following two methods:

 

Solution one:

      Queries user ID with administrator role using the Linq (or otherwise), and use it to perform update:

using (OrganizationServiceContext orgService = new OrganizationServiceContext(service))
{
    var systemUserId = (from userrole in orgService.CreateQuery<Entities.SystemUserRoles>()
                        join role in orgService.CreateQuery<Entities.Role>()
                        on userrole.RoleId equals role.Id
                        where role.Name == "System Administrator"
                        select userrole.SystemUserId).FirstOrDefault();

    if (systemUserId != null)
    {
        IOrganizationService SystemService = factory.CreateOrganizationService(systemUserId);
        SystemService.Update(new_entity);
    }//end if systemUserId != null
}

      The presence of such a joint system by user role query, to get the Administrator role user ID approach a big drawback: that if the current user does not have permission to read the permission correspondence table, unable to obtain the administrator role user ID, will lead to code does not perform the update operation. It is listed here this method is not recommended.

 

Solution two:

      Not by querying the user ID of the administrator role method, pass a null parameter directly when CreateOrganizationService (), it will rank as an administrator to create a service directly.

IOrganizationService SystemService = factory.CreateOrganizationService(null);
SystemService.Update(new_entity);

        Recommended to perform update operations using this method, it will not cause problems because the permissions required to update failure.

Guess you like

Origin www.cnblogs.com/Sunny20181123/p/10939261.html