Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting users in CRM Dynamics 2011

I'm fully aware that deleting users (SystemUser Entity) in CRM Dynamics 2011 is not supported by Microsoft.

However, we're currently developing a tool to support our User Provisioning needs. In order to be able to write integration tests for this tool, it seems necessary to be able to remove users afterwards, so that we can rollback our test environment to the original state.
Currently, we're doing this by restoring organizations from SQL backups, but this is too time consuming to do for each test run.

Update

So far, the best solution we have is to create a user in the integration test, assert everything we need to assert, and afterwards "clean it up" by disabling the user and removing its AD credentials, so that we can re-use those credentials for the next run of the test.

However, as we're only looking for a solution for a test environment, I would really like to have a solution that cleans everything up properly: deleting the records in SQL seems like the way to go. Due to the complex DB structure, however, I was hoping that someone could provide scripts for this.

Update2

We've created the script to manually delete the user from SQL (see accepted answer). This is not supported, so only use it in test environments, if you know what you're doing.

like image 893
Joris Van Regemortel Avatar asked Jun 17 '13 08:06

Joris Van Regemortel


People also ask

How do I remove a user from Dynamics CRM?

Click Settings > Security > Users. Select Disabled Users view. Select a user. Remove personal data, and then click Save.

Can we delete user from CRM?

Point your mouse over the user you want to delete and click the Settings icon. Select the user you want to delete and click Delete This User. Read the consequences of deleting a user and confirm the action. The user will now be deleted from Zoho CRM and appear under the Deleted Users view.

How do I add a user in Dynamics CRM?

Open the Microsoft 365 admin center. In the left pane, select Users, and then select Active users. On the Active users page, select Add a user. On the Set up the basics page, fill in the information for the new user.

How do I change ownership in Dynamics CRM?

Follow these steps. Open a customer engagement app (Dynamics 365 Sales, Dynamics 365 Customer Service, Dynamics 365 Field Service, Dynamics 365 Marketing, and Dynamics 365 Project Service Automation). Select a record such as an account. Change the Owner or Owner Business Unit in the form header or form summary.


2 Answers

The following script is not supported by Microsoft. Using it might harm, brick, blow up or molest your CRM organization, deployment, server and career.
Never use this.
Ever.

That being said, we used this, and it worked fine for our purpose: cleaning up our test environment after running AddSystemUser tests.

Some other things to keep in mind :

  • We are using CRM Dynamics 2011 UR10 On-Premises
  • Our test user doesn't have any related records, it's just an isolated user
  • We're using AD authentication
USE OrganizationName_MSCRM
BEGIN TRANSACTION
DECLARE @username AS VARCHAR(50)

-- CHANGE THIS -- 
SET @username = 'domain\username'
-- DONT CHANGE ANYTHING AFTER THIS --

DECLARE @userId AS UNIQUEIDENTIFIER
SET @userId = (SELECT SystemUserId  FROM dbo.SystemUserBase WHERE DomainName = @username)

DECLARE @orgid AS UNIQUEIDENTIFIER
SET @orgid = (SELECT OrganizationId FROM dbo.SystemUserBase WHERE systemuserid = @userid)

DECLARE @userEmail AS VARCHAR(MAX)
SET @useremail = (SELECT InternalEMailAddress FROM dbo.SystemUserBase WHERE SystemUserId = @userid)

DECLARE @userfullname AS VARCHAR(max)
SET @userfullname = (SELECT fullname FROM dbo.systemuserbase WHERE systemuserid = @userid)

DECLARE @queueid AS UNIQUEIDENTIFIER
SET @queueid = (SELECT queueid FROM dbo.SystemUserBase WHERE SystemUserId = @userid)

DECLARE @ownerid AS UNIQUEIDENTIFIER
SET @ownerid = (SELECT ownerid FROM dbo.OwnerBase WHERE name = @userfullname)

DELETE FROM dbo.SystemUserExtensionBase WHERE SystemUserId = @userId
DELETE FROM dbo.UserSettingsBase WHERE SystemUserId = @userId
DELETE FROM dbo.TeamMembership WHERE SystemUserId = @userId
DELETE FROM dbo.SystemUserPrincipals WHERE systemuserid = @userId
DELETE FROM dbo.SystemUserRoles WHERE systemuserid = @userId
DELETE FROM dbo.SystemUserBusinessUnitEntityMap WHERE systemuserid = @userid
DELETE FROM dbo.UserQueryBase WHERE OwnerId = @userid
DELETE FROM dbo.SystemUserProfiles WHERE SystemUserId = @userId
DELETE FROM dbo.SystemUserBase WHERE SystemUserId = @userid
DELETE FROM dbo.QueueBase WHERE QueueId = @queueid
DELETE FROM dbo.PrincipalEntityMap WHERE PrincipalId = @ownerid
DELETE FROM dbo.PrincipalObjectAccess WHERE principalid = @ownerid
DELETE FROM dbo.OwnerBase WHERE ownerid = @ownerid
DELETE FROM dbo.EmailSearchBase WHERE EmailAddress = @userEmail
DELETE FROM dbo.ResourceBase WHERE name = @userfullname
DELETE FROM dbo.CalendarRuleBase WHERE CalendarId IN (SELECT CalendarId FROM dbo.CalendarBase WHERE PrimaryUserId = @userid)
DELETE FROM dbo.CalendarBase WHERE primaryuserid = @userId
DELETE FROM dbo.InternalAddressBase WHERE parentid = @userId

DELETE FROM mscrm_config..SystemUserOrganizations WHERE CrmUserId = @userid AND OrganizationId = @orgid

COMMIT
like image 50
Joris Van Regemortel Avatar answered Sep 21 '22 19:09

Joris Van Regemortel


Maybe you can try this work around. Rather than just disabling them, change the active directory name to something else, then disable the records.

For example, your script could look as follows (assuming AD authentication):

Create AD users msmith, bmiller, and jdoe.

Perform tests and validation.

Update msmith to testuser1
Update bmiller to testuser2
Update jdoe to testuser3

Deactivate testuser1, testuser2, testuser3

Next test will need to use testuser4, testuser5, testuser6, which means you'll need to create quite a few dummy accounts, but it may be easier to do that, than mess with the CRM SQL database.

For my unit tests where I need a User, I actually mock out the IOrganizationService call for just SystemUser requests, and return a mocked SystemUser entity without it actually hitting CRM. I would suggest that as well, but it sounds like you're attempting to actually test System User creation, so probably isn't an option in this case.

like image 21
Daryl Avatar answered Sep 22 '22 19:09

Daryl