7

我完全清楚 Microsoft 不支持在 CRM Dynamics 2011 中删除用户(SystemUser 实体)。

但是,我们目前正在开发一种工具来支持我们的用户配置需求。为了能够为这个工具编写集成测试,之后似乎有必要能够删除用户,以便我们可以将我们的测试环境回滚到原始状态。
目前,我们通过从 SQL 备份恢复组织来做到这一点,但这对于每次测试运行来说都太耗时了。

更新

到目前为止,我们最好的解决方案是在集成测试中创建一个用户,断言我们需要断言的所有内容,然后通过禁用用户并删除其 AD 凭据来“清理”它,以便我们可以重用那些下次运行测试的凭据。

但是,由于我们只是在寻找测试环境的解决方案,我真的很想有一个解决方案来正确清理所有内容:删除 SQL 中的记录似乎是要走的路。然而,由于复杂的数据库结构,我希望有人可以为此提供脚本。

更新2

我们创建了脚本以从 SQL 中手动删除用户(请参阅接受的答案)。这是不支持的,所以只在测试环境中使用它,如果你知道你在做什么的话。

4

6 回答 6

10

Microsoft 不支持以下脚本。使用它可能会损害、破坏、破坏或骚扰您的 CRM 组织、部署、服务器和职业。
永远不要使用这个。
曾经。

话虽如此,我们使用了它,它对我们的目的很好:在运行测试后清理我们的测试环境AddSystemUser

其他一些要记住的事情:

  • 我们正在使用 CRM Dynamics 2011 UR10 On-Premises
  • 我们的测试用户没有任何相关记录,只是一个孤立用户
  • 我们正在使用 AD 身份验证
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
于 2013-06-20T11:07:12.567 回答
2

也许你可以试试这个解决方法。与其禁用它们,不如将活动目录名称更改为其他名称,然后禁用记录。

例如,您的脚本可能如下所示(假设 AD 身份验证):

创建 AD 用户msmithbmillerjdoe

执行测试和验证。

将msmith更新为testuser1将bmiller
更新 为testuser2将jdoe 更新为testuser3

停用testuser1testuser2testuser3

下一个测试将需要使用testuser4testuser5testuser6,这意味着您需要创建相当多的虚拟帐户,但这样做可能比弄乱 CRM SQL 数据库更容易。

对于我需要用户的单元测试,我实际上模拟了仅针对 SystemUser 请求的 IOrganizationService 调用,并返回一个模拟的 SystemUser 实体,而它实际上并未触及 CRM。我也建议这样做,但听起来您正在尝试实际测试系统用户的创建,因此在这种情况下可能不是一个选项。

于 2013-06-17T12:30:23.997 回答
2

这是我在 CRM 2015 中基于 Joris 代码所做的

USED ORGANIZATIONNAME_MSCRM

BEGIN TRANSACTION

DECLARE @username AS VARCHAR(50)



-- CHANGE THIS --

SET @username = 'AD\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)



DECLARE @MSCRMUserID as UNIQUEIDENTIFIER

SET @MSCRMUserID = (SELECT Userid FROM mscrm_config..SystemUserOrganizations WHERE CrmUserId = @userid AND OrganizationId = @orgid)

DECLARE @authinfo as NVARCHAR(255)

SET @authinfo = (SELECT Authinfo FROM mscrm_config..SystemUserAuthentication WHERE Userid = @MSCRMUserID)

DECLARE @SUid as UNIQUEIDENTIFIER

SET @Suid = (SELECT id FROM mscrm_config..SystemUserAuthentication WHERE Userid = @MSCRMUserID)



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.TeamMembership WHERE SystemUserId = @userId



DELETE FROM dbo.QueueMembership  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.MailboxBase WHERE OwnerId = @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.InternalAddressBase WHERE parentid = @userId

DELETE FROM dbo.CalendarBase WHERE primaryuserid = @userId



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

DELETE FROM mscrm_config..SystemUserAuthentication WHERE authinfo = @authinfo

DELETE FROM mscrm_config..SystemUser WHERE id = @MSCRMUserID



rollback

--COMMIT

运行查询并确保它正在删除正确的数据后,您可以取消注释 COMMIT 语句并注释 Rollback 语句

希望能帮助到你。

于 2016-03-13T05:25:37.880 回答
2

只是对 Dynamics CRM 2016 (On Premise) 进行了一些细微的修改,以允许我遇到一些常见的约束错误。同样,这完全不受支持,风险自负。(当我发现其他约束错误时,我会更新。)

BEGIN TRANSACTION
DECLARE @username AS VARCHAR(50)
/* CHANGE THIS LINE ONLY */
SET @username = 'DOMAIN\USERNAME'
/* END CHANGES */
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)
DECLARE @MSCRMUserID as UNIQUEIDENTIFIER
SET @MSCRMUserID = (SELECT Userid FROM mscrm_config..SystemUserOrganizations WHERE CrmUserId = @userid AND OrganizationId = @orgid)
DECLARE @authinfo as NVARCHAR(255)
SET @authinfo = (SELECT Authinfo FROM mscrm_config..SystemUserAuthentication WHERE Userid = @MSCRMUserID)
DECLARE @SUid as UNIQUEIDENTIFIER
SET @Suid = (SELECT id FROM mscrm_config..SystemUserAuthentication WHERE Userid = @MSCRMUserID)
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.TeamMembership WHERE SystemUserId = @userId
DELETE FROM dbo.QueueMembership  WHERE SystemUserId = @userId
update dbo.SdkMessageFilterBase set ModifiedOnBehalfBy = NULL where ModifiedOnBehalfBy = @userid
update dbo.SdkMessageFilterBase set CreatedOnBehalfBy = NULL where CreatedOnBehalfBy = @userid
DELETE FROM dbo.SystemUserBase WHERE SystemUserId = @userid
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.QueueBase WHERE QueueId = @queueid
DELETE FROM dbo.PrincipalEntityMap WHERE PrincipalId = @ownerid
DELETE FROM dbo.PrincipalObjectAccess WHERE PrincipalId = @ownerid
DELETE FROM dbo.UserEntityUISettingsBase WHERE OwnerID = @userid
DELETE FROM dbo.UserApplicationMetadataBase WHERE OwnerID = @userid
DELETE FROM dbo.PostFollowBase WHERE OwnerID = @userid
DELETE FROM dbo.MailboxBase WHERE OwnerId = @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.InternalAddressBase WHERE parentid = @userId
DELETE FROM mscrm_config..SystemUserOrganizations WHERE CrmUserId = @userid AND OrganizationId = @orgid
DELETE FROM mscrm_config..SystemUserAuthentication WHERE authinfo = @authinfo
DELETE FROM mscrm_config..SystemUser WHERE id = @MSCRMUserID
/* rollback */
COMMIT
于 2017-11-07T19:07:01.693 回答
1

您可以使用完全不受支持的方式通过 SQL 删除它们。
只要您在集成测试环境中执行此操作,我认为危害会相对较低。

要查找添加用户时数据库中发生的更改,您可以

  • 复制您当前的数据库
  • 添加用户
  • 将新的数据库状态与之前的状态进行比较(例如:redgate 的 SQL 比较)

每次汇总都可能会发生这种变化,因此请确保不要将其用于任何关键或生产代码。

于 2013-06-19T07:26:05.370 回答
0

这不受支持,但是直接从 SQL 中删除记录怎么样?

我自己从来没有尝试过,我不想在生产环境中这样做,但如果它只是为了测试/开发,最糟糕的情况就是你破坏了一个开发环境。

于 2013-06-17T09:36:45.983 回答