Pages

Tuesday, April 15, 2014

Migrating selected entities data from UAT MS Dynamics 2011 to PROD MS Dynamics CRM 2011

Migrating selected entities data from UAT MS Dynamics 2011 to PROD MS Dynamics CRM 2011

Recently i engaged a new project to migrating data from one CRM server to another CRM server for selected entities.
Challenging work is moving email related entity data to another CRM server.
Here i going to discuss only email related entity code work.

I did this work using both CRM service and direct sql update.

1) i created 2 service, let assume one is UAT and another is PROD
public static OrganizationServiceProxy U_CRMServiceProxy
{
    get
    {
    var uri = new Uri(U_URI);

    //Authenticate using credentials of the logged in user;      
    var cntCredentials = new ClientCredentials();
    cntCredentials.Windows.ClientCredential.Domain = Domain;
    cntCredentials.Windows.ClientCredential.UserName = U_User;
    cntCredentials.Windows.ClientCredential.Password = U_Pass;
    //cntCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

    var serviceProxy = new OrganizationServiceProxy(uri, null, cntCredentials, null);
    //serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    serviceProxy.EnableProxyTypes();

    return serviceProxy;
    }
}



2)Annotation entity

using GCEntity = DNS.CRM.Entities;

private void Annotation()
{
    GCEntity.XrmServiceContext P_XRM = new GCEntity.XrmServiceContext(P_CRMServiceProxy);
    GCEntity.XrmServiceContext U_XRM = new GCEntity.XrmServiceContext(U_CRMServiceProxy);

    var uqry = from a in U_XRM.AnnotationSet
          join m in U_XRM.dns_matchSet on a.ObjectId.Id equals m.dns_matchId.Value
          join f in U_XRM.dns_auditSet on m.dns_MatchEmployee.Id equals f.dns_auditId.Value
          select new
          {
          a.ObjectId.Id, a.Subject, a.IsDocument, a.NoteText, a.MimeType, a.DocumentBody, a.FileSize,  a.FileName,
          f.dns_EmployeeNumber
          };

    //Console.WriteLine(uqry.ToList().Count);

    foreach (var uvt in uqry)
    {
    int dns_EmployeeNumber = uvt.dns_EmployeeNumber.Value;
    Guid batch_guid = new Guid("742A1234-B6C1-A214-B96E-123456789EE5");

    var pqry = (from m in P_XRM.dns_matchSet
        join f in P_XRM.dns_auditSet on m.dns_MatchEmployee.Id equals f.dns_auditId.Value
        where
          f.dns_EmployeeNumber == dns_EmployeeNumber
          && f.dns_BatchId.Id == batch_guid
        select new
        {
            m.dns_matchId
        }).Single();
       
    //Console.WriteLine(pqry.dns_matchId.Value);
    Guid p_match_guid = pqry.dns_matchId.Value;

    GCEntity.Annotation anno = new GCEntity.Annotation();
    anno.ObjectId = new EntityReference("dns_match", p_match_guid);
    anno.Subject = uvt.Subject;
    anno.IsDocument = uvt.IsDocument.Value;
    anno.NoteText = uvt.NoteText;
    anno.MimeType = uvt.MimeType;
    anno.DocumentBody = uvt.DocumentBody;
    anno.FileName = uvt.FileName;
    P_CRMServiceProxy.Create(anno);
    }

}


3)Email entity
private void Email()
{
    var uqry = from d in U_XRM.EmailSet
           join m in U_XRM.dns_matchSet on d.RegardingObjectId.Id equals m.dns_matchId.Value
           join f in U_XRM.dns_EmployeeSet on m.dns_MatchEmployee.Id equals f.dns_EmployeeId.Value
           select new
           {
          d.StateCode, d.StatusCode, d.SubmittedBy, d.Description, d.IsWorkflowCreated, d.ReadReceiptRequested,
              d.IsBilled, d.PriorityCode, d.DirectionCode, d.ActualEnd, d.TrackingToken, d.Sender, d.Subject,
              d.ToRecipients, d.DeliveryReceiptRequested, d.MessageId, d.DeliveryAttempts,  d.Notifications,
              d.TimeZoneRuleVersionNumber, d.ActivityId, f.dns_EmployeeNumber
           };

    //Console.WriteLine(uqry.ToList().Count);

    foreach (var uvt in uqry)
    {
    int dns_EmployeeNumber = uvt.dns_EmployeeNumber.Value;
    Guid batch_guid = new Guid("742A1234-B6C1-A214-B96E-123456789EE5");
   
        var pqry = (from m in P_XRM.dns_matchSet ..... ).Single();
        Guid p_match_guid = pqry.dns_matchId.Value;
      
        GCEntity.Email da = new GCEntity.Email();
        if (uvt.Sender == "noreply@dns.com")
        {
            GCEntity.ActivityParty fromParty = new GCEntity.ActivityParty
            {
            PartyId = new EntityReference(GCEntity.SystemUser.EntityLogicalName, new Guid("12345678-1234-1234-1234-0050568C2EE5"))
            };
            da.From = new GCEntity.ActivityParty[] { fromParty };

            string[] toadd = uvt.ToRecipients.Split(';');

            var pocqry = (from m in P_XRM.dns_ocontactSet
                  where m.EmailAddress == toadd[0]
                  select new { m.dns_ocontactId }).Single();
            //Console.WriteLine(pocqry.dns_ocontactId.Value);

            Guid p_offcont_guid = pocqry.dns_ocontactId.Value;

            GCEntity.ActivityParty toParty = new GCEntity.ActivityParty
            {
            PartyId = new EntityReference(GCEntity.dns_ocontact.EntityLogicalName, p_offcont_guid)
            };
            da.To = new GCEntity.ActivityParty[] { toParty };
        }
        else
        {
            GCEntity.ActivityParty fromParty = new GCEntity.ActivityParty();
            fromParty.AddressUsed = uvt.Sender;
            da.From = new GCEntity.ActivityParty[] { fromParty };

            GCEntity.ActivityParty toParty = new GCEntity.ActivityParty
            {
            PartyId = new EntityReference(GCEntity.SystemUser.EntityLogicalName, new Guid("12345678-1234-1234-1234-0050568C2EE5"))
            };
            da.To = new GCEntity.ActivityParty[] { toParty };
        }

        da.RegardingObjectId = new EntityReference("dns_match", p_match_guid);
        da.SubmittedBy = uvt.SubmittedBy;
        da.Description = uvt.Description;
        da.IsWorkflowCreated = uvt.IsWorkflowCreated;
        da.ReadReceiptRequested = uvt.ReadReceiptRequested;
        da.IsBilled = uvt.IsBilled;
        da.PriorityCode = uvt.PriorityCode;
        da.DirectionCode = uvt.DirectionCode;
        da.ActualEnd = uvt.ActualEnd;
        da.TrackingToken = uvt.TrackingToken;
        da.Subject = uvt.Subject;
        da.DeliveryReceiptRequested = uvt.DeliveryReceiptRequested;
        da.MessageId = uvt.MessageId;
        da.DeliveryAttempts = uvt.DeliveryAttempts;
        da.Notifications = uvt.Notifications;
        da.TimeZoneRuleVersionNumber = uvt.TimeZoneRuleVersionNumber;

        Guid emailId = P_CRMServiceProxy.Create(da);

        //Change the status
        Microsoft.Xrm.Sdk.EntityReference efEmail = new EntityReference()
        {
            LogicalName = "email",
            Id = emailId
        };

        Microsoft.Xrm.Sdk.OrganizationRequest req  = new Microsoft.Xrm.Sdk.OrganizationRequest() { RequestName = "SetState" };
        req["EntityMoniker"] = efEmail;
        OptionSetValue state = new OptionSetValue((int)uvt.StateCode.Value);
        OptionSetValue status = new OptionSetValue(uvt.StatusCode.Value);
        req["State"] = state;
        req["Status"] = status;

        P_CRMServiceProxy.Execute(req);
     }       
}

3)Email Attachment entity
private void ActivityAttachment()
{
    var uqry = from aa in U_XRM.ActivityMimeAttachmentSet
           join e in U_XRM.EmailSet on aa.ObjectId.Id equals e.ActivityId.Value
           join m in U_XRM.dns_matchSet on e.RegardingObjectId.Id equals m.dns_matchId.Value
           join f in U_XRM.dns_EmployeeSet on m.dns_MatchEmployee.Id equals f.dns_EmployeeId.Value
           select new
           {
           e.ActivityId, aa.ObjectId, aa.Body, aa.FileName, aa.FileSize, aa.MimeType, aa.Subject, aa.AttachmentId,
           f.dns_EmployeeNumber, aa.ObjectTypeCode
           };

    foreach (var uvt in uqry)
    {
    int dns_EmployeeNumber = uvt.dns_EmployeeNumber.Value;
    Guid batch_guid = new Guid("12345678-1234-1234-1234-0050568C2EE5");

        //StatusCode 3 is send and 4 is receive   
        var pqry = (from e in P_XRM.EmailSet
            join m in P_XRM.dns_matchSet on e.RegardingObjectId.Id equals m.dns_matchId.Value
            join f in P_XRM.dns_EmployeeSet on m.dns_MatchEmployee.Id equals f.dns_EmployeeId.Value
            where
              f.dns_EmployeeNumber == dns_EmployeeNumber
             where
                e.StatusCode == new OptionSetValue((int)3)
            select new
            {
                e.ActivityId
            }).Single();

        Guid prod_email_guid = pqry.ActivityId.Value;

        GCEntity.ActivityMimeAttachment anno = new GCEntity.ActivityMimeAttachment();
        anno.ObjectId = new EntityReference("email", prod_email_guid);
        anno.Body = uvt.Body;
        anno.FileName = uvt.FileName;
        anno.MimeType = uvt.MimeType;
        anno.Subject = uvt.Subject;
        anno.ObjectTypeCode = uvt.ObjectTypeCode;
        P_CRMServiceProxy.Create(anno);
    }
}

       
4)CC/BCC Email Address entity
private void ActivityParty()
{

    var uqry = from d in U_XRM.ActivityPartySet
           join e in U_XRM.EmailSet on  d.ActivityId.Id equals e.ActivityId.Value
           join m in U_XRM.dns_matchSet on e.RegardingObjectId.Id equals m.dns_matchId.Value
           join f in U_XRM.dns_EmployeeSet on m.dns_MatchEmployee.Id equals f.dns_EmployeeId.Value
           where
        d.ParticipationTypeMask.Value==4
           || d.ParticipationTypeMask.Value == 3
           select new
           {
           d.PartyId,d.ParticipationTypeMask,d.AddressUsed, d.ActivityId,f.dns_EmployeeNumber
           };

    foreach (var uvt in uqry)
    {
    int dns_EmployeeNumber = uvt.dns_EmployeeNumber.Value;
    Guid batch_guid = new Guid("742ADDBE-B6C1-E311-B96E-0050568C2EE5");

        var pqry = (from e in P_XRM.EmailSet
            join m in P_XRM.dns_matchSet on e.RegardingObjectId.Id equals m.dns_matchId.Value
            join f in P_XRM.dns_EmployeeSet on m.dns_MatchEmployee.Id equals f.dns_EmployeeId.Value
            where
              f.dns_EmployeeNumber == dns_EmployeeNumber
              && f.dns_BatchId.Id == batch_guid
            where
               e.StatusCode == new OptionSetValue((int)1)
               || e.StatusCode == new OptionSetValue((int)3)
            select new
            {
                e.ActivityId
            }).Single();
           
        //Open the email            
        Guid prod_email_guid = pqry.ActivityId.Value;
        Microsoft.Xrm.Sdk.EntityReference efEmail = new EntityReference()
        {
            LogicalName = "email",
            Id = prod_email_guid
        };

        Microsoft.Xrm.Sdk.OrganizationRequest req = new Microsoft.Xrm.Sdk.OrganizationRequest() { RequestName = "SetState" };
        req["EntityMoniker"] = efEmail;
        OptionSetValue state = new OptionSetValue(0);
        OptionSetValue status = new OptionSetValue(1);
        req["State"] = state;
        req["Status"] = status;

        P_CRMServiceProxy.Execute(req);

        //Update CC, BCC
        GCEntity.Email da = new GCEntity.Email();
        da.ActivityId = prod_email_guid;
        GCEntity.ActivityParty fromParty = new GCEntity.ActivityParty();
        fromParty.AddressUsed = uvt.AddressUsed;
        da.Cc = new GCEntity.ActivityParty[] { fromParty };
        P_CRMServiceProxy.Update(da);

        //Close the email
        state = new OptionSetValue(1);
        status = new OptionSetValue(3);
        req["State"] = state;
        req["Status"] = status;

        P_CRMServiceProxy.Execute(req);

    }
}
       

No comments:

Post a Comment