2015-01-15 2 views
0

1 개의 사업부에서 새로 설치 한 1 개의 사용자로 전체 부하를 옮기려고합니다. 저는 CRM 전문가가 아니며, 사용자를 이동시켜야한다는 정보를 받았습니다. 기존 역할을 먼저 확인한 다음 새 BU로 이동 한 다음 BU가 아닌 역할을 복원 한 다음 수정하십시오. 사용자에 대한 예제는 다음과 같습니다주어진 사용자 목록에 대한 역할을 업데이트하려면 어떻게해야합니까?

BU1
역할 :
BU1Admin
BU1Read 전용
계약

이동이에 : BU2
역할 :
BU2Admin
BU2Read 전용
계약

이 예제에서 사용자는 자신의 관리 역할과 읽기 전용 역할을 BU2로 수정해야하지만 계약서는 동일하게 유지해야합니다.

  Dictionary<Guid, string> UserRoles = new Dictionary<Guid, string>(); 

      QueryExpression query = new QueryExpression(); 
      query.EntityName = "systemuserroles"; 
      ColumnSet cols = new ColumnSet(); 
      cols.Attributes = new string[] { "systemuserid", "roleid" }; 
      query.ColumnSet = cols; 

      LinkEntity le = new LinkEntity(); 
      le.LinkFromEntityName = "systemuserroles"; 
      le.LinkFromAttributeName = "roleid"; 
      le.LinkToEntityName = "role"; 
      le.LinkToAttributeName = "roleid"; 

      LinkEntity le2 = new LinkEntity(); 
      le2.LinkFromEntityName = "systemuserroles"; 
      le2.LinkFromAttributeName = "systemuserid"; 
      le2.LinkToEntityName = "systemuser"; 
      le2.LinkToAttributeName = "systemuserid"; 

      // Find only users in BU1 
      ConditionExpression ce = new ConditionExpression(); 
      ce.AttributeName = "businessunitid"; 
      ce.Operator = ConditionOperator.Equal; 
      ce.Values = new string[] { BU1 Guid }; 

      le2.LinkCriteria = new FilterExpression(); 
      le2.LinkCriteria.Conditions = new ConditionExpression[] { ce }; 

      query.LinkEntities = new LinkEntity[] { le, le2 }; 

      try 
      { 
       //This call doesn't work and fails saying RetrieveMultiple doesn't support entities of type systemuserroles. 
       BusinessEntityCollection UserRolesCollection = crmService.RetrieveMultiple(query); 
       foreach (BusinessEntity be in UserRolesCollection.BusinessEntities) 
       { 
        //Do my stuff here  
       } 
      } 
      catch (SoapException se) 
      { 
       throw new Exception("Error occurred." + se.Detail); 
      } 

다음 단계, 나는 새로운 역할과 사용자를 다음하면됩니다 업데이트 할 수 있도록하려면 : 내 최초의 생각은 이런 식으로 뭔가를 사용하여 역할 이름과 함께 systemuserid 년대를 검색하는 쿼리를 생성하는 것이 었습니다 . 나는 이것이 내가 가지고있는 문제들에 기초하여 행해질 수 있는지 알지 못한다. 어떤 도움이라도 대단히 감사 할 것입니다. 나는 DynamicEntity가 this을 읽은 후에 여기에 어떤 용도로 사용 될지 궁금합니다.

업데이트 : 당신이 AssignUserRolesRole 요청 클래스 here를 사용하여 사용자 역할을 업데이트 할 수 있습니다처럼

것 같습니다. 그러나, 나는 아직도 순간 검색 비트에 아직도 붙어있어. 내가 SQL에 의지 할 필요가 있는지 궁금하네요?

+0

웹 서비스를 통해이 작업을 수행 한 경험이 없지만 SQL 경로에 관심이 있으시면 언제든지 알려 주시기 바랍니다. SQL을 통해이 작업을 수행하는 것은 Microsoft에서 지원하지 않습니다. – Zach

+0

@Zach 내 생각은 중간 집을 만드는 것입니다. SystemUserRoles 테이블에서 필요한 데이터를 검색하는 함수를 작성한 다음 코드에서 호출하여 사전에 넣습니다. 그런 다음 CrmService를 사용하여 AssignUserRolesRole을 사용하여 각 사용자의 역할을 업데이트합니다. SQL을 통해 업데이트를하는 것은 업데이트해야 할 모든 장소를 확신하므로 매우 무서운 것 같습니다. – sr28

답변

0

마지막으로 API를 통해이를 수행 할 수있는 방법을 찾았습니다. 본질적으로 2 쿼리입니다. 1은 모든 사용자의 systemuserid를 가져오고 두 번째는 각 systemuserid에 대한 roleids를 얻습니다. 이것은 모든 사용자의 현재 역할을 나에게 줄 것이다.

저는 이전 롤 GUID를 새 롤 GUID에 매핑하는 사전을 만듭니다.

그런 다음 비즈니스 단위를 변경하여 모든 역할을 제거하고 사전과 이전에 저장된 사용자 역할을 사용하여 각 사용자의 새 역할에 매핑 할 수 있습니다.

class UserRoles 
{ 
    public string Username { get; set; } 
    public Guid Value { get; set; } 
    public List<Guid> Roles { get; set; } 

    public UserRoles() 
    { 
     Roles = new List<Guid>(); 
    } 
} 

참고 : 이것은이되지

private void UpdateUsers() 
{ 
    //Create mapping of old guid to new guid. 
    Dictionary<Guid, Guid> LookupRoleGuids = new Dictionary<Guid, Guid>(); 
    //Add guid mapping 
    LookupRoleGuids.Add(new Guid(Old Guid), new Guid(New Guid)); 

    QueryExpression query = new QueryExpression(); 
    query.EntityName = "systemuser"; 
    query.ColumnSet = new AllColumns(); 

    ConditionExpression ce2 = new ConditionExpression(); 
    ce2.AttributeName = "systemuserid"; 
    ce2.Operator = ConditionOperator.Equal; 
    ce2.Values = new string[] { User Id }; //Can alter to retrieve for a BU 

    FilterExpression filter = new FilterExpression(); 
    filter.Conditions = new ConditionExpression[] { ce2 }; 
    query.Criteria = filter; 

    try 
    { 
     BusinessEntityCollection UserCollection = crmService.RetrieveMultiple(query); 

     //store the roles of the Users. 
     StoreUserRoles(UserCollection); 

     foreach (BusinessEntity be in UserCollection.BusinessEntities) 
     { 
      //Update users BU. 
      Guid newBu = new Guid(New BU Guid); 
      systemuser su = (systemuser)be; 
      SetBusinessSystemUserRequest setBUreq = new SetBusinessSystemUserRequest(); 
      setBUreq.BusinessId = newBu; 
      setBUreq.UserId = su.systemuserid.Value; 
      SecurityPrincipal assignee = new SecurityPrincipal(); 
      assignee.PrincipalId = new Guid(su.systemuserid.Value.ToString()); 
      setBUreq.ReassignPrincipal = assignee; 
      SetBusinessSystemUserResponse assigned = (SetBusinessSystemUserResponse)crmService.Execute(setBUreq); 

      //Get users existing roles 
      if (UserRolesList.Count() > 0) 
      { 
       UserRoles ur = UserRolesList.Where(x => x.Value == su.systemuserid.Value) 
              .Select(x => x).First(); 

       //Get new role guids based on mapping 
       Guid[] roleguids = LookupRoleGuids.Where(x => ur.Roles.Contains(x.Key)) 
                .Select(x => x.Value) 
                .ToArray(); 

       //Assign new roles 
       AssignUserRolesRoleRequest addRoles = new AssignUserRolesRoleRequest(); 
       addRoles.UserId = su.systemuserid.Value; 
       addRoles.RoleIds = roleguids; 
       crmService.Execute(addRoles); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     throw new Exception("Error occurred updating users BU or assigning new roles. Error: " + ex.Message); 
    } 
} 

public void StoreUserRoles(BusinessEntityCollection UserRolesCollection) 
{ 
    UserRolesList = new List<UserRoles>(); 

    foreach (BusinessEntity be in UserRolesCollection.BusinessEntities) 
    { 
     systemuser u = (systemuser)be; 
     UserRoles ur = new UserRoles(); 
     ur.Username = u.domainname; 
     ur.Value = u.systemuserid.Value; 
     UserRolesList.Add(ur); 
    } 

    AddRolesToList(ref UserRolesList); 
} 

private void AddRolesToList(ref List<UserRoles> URList) 
{ 
    //Get all roles for a given user guid 
    QueryExpression query = new QueryExpression(); 
    query.EntityName = "role"; 
    ColumnSet cols = new ColumnSet(); 
    cols.Attributes = new string[] { "roleid" }; 
    query.ColumnSet = cols; 

    LinkEntity le = new LinkEntity(); 
    le.LinkFromEntityName = "role"; 
    le.LinkToEntityName = "systemuserroles"; 
    le.LinkFromAttributeName = "roleid"; 
    le.LinkToAttributeName = "roleid"; 

    LinkEntity le2 = new LinkEntity(); 
    le2.LinkFromEntityName = "systemuserroles"; 
    le2.LinkToEntityName = "systemuser"; 
    le2.LinkFromAttributeName = "systemuserid"; 
    le2.LinkToAttributeName = "systemuserid"; 

    foreach(UserRoles ur in URList) 
    { 
     //loop through the list of userroles and alter the conditional expression with the user's guid. 
     ConditionExpression ce = new ConditionExpression(); 
     ce.AttributeName = "systemuserid"; 
     ce.Operator = ConditionOperator.Equal; 
     ce.Values = new string[] { ur.Value.ToString() }; 

     le2.LinkCriteria = new FilterExpression(); 
     le2.LinkCriteria.Conditions = new ConditionExpression[] { ce }; 

     le.LinkEntities = new LinkEntity[] { le2 }; 
     query.LinkEntities = new LinkEntity[] { le }; 

     try 
     { 
      BusinessEntityCollection RoleGuidsCollection = crmService.RetrieveMultiple(query); 
      foreach (BusinessEntity be in RoleGuidsCollection.BusinessEntities) 
      { 
       role r = (role)be; 
       ur.Roles.Add(r.roleid.Value); 
      } 
     } 
     catch (SoapException se) 
     { 
      throw new Exception("Error occurred retrieving Role Ids for " + ur.Username + " (" + ur.Value + "). " + se.Detail.InnerXml); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error occurred retrieving Role Guids for " + ur.Username + " (" + ur.Value + "). " + ex.Message); 
     } 
    } 
} 

내가 클래스라는 UserRoles를 만든 각 사용자에 대한 역할을 저장하려면 : 나는 비슷한 상황에있는 사람이 코드를 함께했다 완전히 테스트되었지만 내가 원하는 것을 수행하는 것처럼 보이기 때문에 사용자를 다른 BU로 이동시키고 이전 BU를 기반으로 새 역할을 설정할 수 있습니다.

1

다음은 사용자 이동을위한 지원되지 않는 SQL 솔루션입니다. 방금 시도했는데 모든 것이 잘 작동하는 것 같습니다. 당신은 dev 환경에서 먼저 그것을 시험해 볼 수 있습니다. 그렇지 않으면 하이브리드 솔루션의 출발점이 될 수도 있습니다.

/* Switch Business Unit */ 
update su set businessunitid = (select top 1 businessunitid from businessunit where name = '[New Business Unit Name]') 
--select fullname, businessunitidname 
from systemuser su 
where businessunitidname = '[Current Business Unit Name]' 

/* Identifies users who have roles where the business unit does not match the user's business unit and updates them to match */ 
update bridge set roleid = newRole.roleid 
--select su.fullname, oldRole.name, oldRole.businessunitidname, newRole.name, newRole.businessunitidname 
from systemuser su 
JOIN systemuserroles bridge ON su.systemuserid = bridge.systemuserid 
JOIN [role] oldRole ON bridge.roleid = oldRole.roleid 
JOIN [role] newRole ON oldRole.name = newRole.Name and newRole.BusinessUnitIdName = '[New Business Unit Name]' 
where oldRole.BusinessUnitId <> su.BusinessUnitId 
+0

감사합니다. 나는 여전히 내 길을 내려 놓을 것이지만 막 다른 골목에 이르면 나는 이것에 의지해야 할 것이다. – sr28

관련 문제