This is the code I’ve used in one of my unit tests where I had to test if an opportunity would be automatically re-assigned to another user if its owner was disabled.
See the code below:
/// Updates the state of the CRM user.
/// Use the SystemUserState enum to set the status.
/// <param name=”userId”></param>
/// <param name=”state”></param>
private void UpdateSystemUserState(Guid userId, SystemUserState state)
Guid newContactId = Guid.Empty;
SetStateSystemUserRequest setStateSystemUserRequest = null;
SetStateSystemUserResponse setStateSystemUserResponse = null;
setStateSystemUserRequest = new SetStateSystemUserRequest();
setStateSystemUserRequest.SystemUserState = state;
setStateSystemUserRequest.SystemUserStatus = -1;
setStateSystemUserRequest.EntityId = userId;
setStateSystemUserResponse = (SetStateSystemUserResponse)crmService.Execute(setStateSystemUserRequest);