Wednesday, January 8, 2020

How to migrate Personal Views, Dashboard and Charts in Dynamic CRM


I was involved in Data migration activity for one of my customer’s and whose CRM Users have heavily created Personal Views, Personal Dashboard and Personal Charts too. It seems the reporting was required daily.

Why I ended up creating C# Console App
This client wanted to move to latest version of Dynamic CRM and wanted to move from one tenant to another tenant, so simply taking DB backup and migrating it with Deployment manager was rejected by Microsoft itself as during 2018 it was not supported, so I help them with their migration activity in combination of some supported tools and little bit of C# applications.

Here in this article, I am sharing C# code to migrate
  • Personal Views,
  • Personal Dashboard and
  • Personal Charts




C# Code Sample


  • Code is provided with comments to help with better understanding. 
  • Recommend to run it in Debugging more to see it working. 


ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
CrmServiceClient orgSvc_Source = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Source"].ConnectionString);
CrmServiceClient orgSvc_Target = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Target"].ConnectionString);

if (!(orgSvc_Source.IsReady && orgSvc_Target.IsReady))
{
    Console.ReadKey();
    return;
}
QueryExpression userQueryExp = new QueryExpression("systemuser") { ColumnSet = new ColumnSet(true), NoLock = true };

EntityCollection systemUsersFromSource = orgSvc_Source.RetrieveMultiple(userQueryExp);

foreach (Entity userRecSource in systemUsersFromSource.Entities)
{
    if (userRecSource.Attributes["userlicensetype"].ToString() == "-1")
    {
        continue;   // User must have valid License Type
    }
    if (userRecSource.Id != Guid.Parse("72F2F31C-EF96-45EB-AAB5-F84FAE173D78"))
    {
        continue;   // If you need to Skip any Specific Users
    }

    Guid instanceOneUserId = userRecSource.Id;
    orgSvc_Source.CallerId = instanceOneUserId;

    QueryExpression userSavedQueries = new QueryExpression("userquery")
                    { ColumnSet = new ColumnSet() { AllColumns = true }, NoLock = true };

    QueryExpression query1 = new QueryExpression("userqueryvisualization")
                    { ColumnSet = new ColumnSet() { AllColumns = true }, NoLock = true };

    QueryExpression query2 = new QueryExpression("userform")
                    { ColumnSet = new ColumnSet() { AllColumns = true }, NoLock = true };

    //Fetch all personal views, charts and dashboards
    List personalViews = orgSvc_Source.RetrieveMultiple(userSavedQueries).Entities.ToList();
    List personalCharts = orgSvc_Source.RetrieveMultiple(query1).Entities.ToList();
    List personalDashboards = orgSvc_Source.RetrieveMultiple(query2).Entities.ToList();

               
    EntityCollection systemUsersFromTarget = orgSvc_Target.RetrieveMultiple(userQueryExp);
    foreach (Entity userRecTarget in systemUsersFromTarget.Entities)
    {
        if (!(userRecTarget.Id == Guid.Parse("325BF9DE-AF11-E911-A994-000D3A33AFD1")))
        {
            continue;   // Here you can decide which user you wish to Assign
        }
        Guid instanceTwoUserId = userRecTarget.Id;
        orgSvc_Target.CallerId = instanceTwoUserId;

        //Personal Views
        personalViews.ForEach(e =>
        {  
            Console.WriteLine(e.Attributes["name"].ToString());
            // On my case we have only ONE Root BU, please handle you case specifically
            e.Attributes["owningbusinessunit"] =
                    new EntityReference("businessunit", Guid.Parse("33EA22FE-F004-E911-A956-000D3A37FBCE"));

            e.Attributes["ownerid"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["owninguser"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["modifiedby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["createdby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);                       
            orgSvc_Target.Create(e);
        });

        //Personal Charts
        personalCharts.ForEach(e =>
        {
            Console.WriteLine(e.Attributes["name"].ToString());
            e.Attributes["owningbusinessunit"] =
                    new EntityReference("businessunit", Guid.Parse("33EA22FE-F004-E911-A956-000D3A37FBCE"));

            e.Attributes["ownerid"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["owninguser"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["modifiedby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["createdby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            orgSvc_Target.Create(e);
        });

        //Personal Dashboards
        personalDashboards.ForEach(e =>
        {
            Console.WriteLine(e.Attributes["name"].ToString());
            e.Attributes["owningbusinessunit"] =
                    new EntityReference("businessunit", Guid.Parse("33EA22FE-F004-E911-A956-000D3A37FBCE"));

            e.Attributes["ownerid"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["owninguser"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["modifiedby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            e.Attributes["createdby"] = new EntityReference(userRecTarget.LogicalName, userRecTarget.Id);
            orgSvc_Target.Create(e);
        });
    }//end foreach inner
}//end foreach outer





I wish you all the best with the code.

Thanks
Vipin Jaiswal
vipinjaiswal12@gmail.com

No comments: