Sunday, January 16, 2011

Using the CRM API to move users to another Business Unit

Hello Gang,

I recently had to write a piece of code to move users to different business units via the CRM API (v4).  To to move the user to a new business unit API uses a SetBusinessSystemUserRequestmessage.  We ran into a problem with this message.  The SetBusinessSystemUserRequest message needs Security Principal to assign the moving users' records to.  If you want to move the user between business units and retain record ownership you can set the Security Principal to the moving user:

SetBusinessSystemUserRequest sbsur = 
new SetBusinessSystemUserRequest(); sbsur.BusinessId = businessId; SecurityPrincipal sp = new SecurityPrincipal(); sp.PrincipalId = movingUserId; sp.Type = SecurityPrincipalType.User; sbsur.ReassignPrincipal = sp; sbsur.UserId = movingUserId;

This code will not work.  Each entity in CRM has a owningbusinessunit column in its base table.  When executing this code the owningbusinessunit column is not updated.  To work around this bug.  Do the following:

SetBusinessSystemUserRequest sbsur = 
new SetBusinessSystemUserRequest(); sbsur.BusinessId = businessId; SecurityPrincipal sp = new SecurityPrincipal(); sp.PrincipalId = Guid.Empty; sbsur.ReassignPrincipal = sp; sbsur.UserId = movingUserId;

Kind of silly if you ask me...  But it works.

Happy coding!

No comments:

Post a Comment