Wednesday, October 02, 2013

Code snippet to copy a particular record from test to prod or vice versa


Use the code template from here

In the example below I am copying a particular task and related notes/attachments.
Code:
 Entity entity = _service.Retrieve("task", new Guid("F6213B56-61A7-E211-A560-0050568E0699"), new ColumnSet(true));
                    organizationUri = new Uri(prod);
                    OrganizationServiceProxy _prodProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
                    _prodProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    IOrganizationService _Prodservice = (IOrganizationService)_prodProxy;

                    _prodProxy.Create(entity);
         
                //ConditionExpression con_ce1 = new ConditionExpression("isdocument", ConditionOperator.Equal, true);
                ConditionExpression con_ce2 = new ConditionExpression("objectid", ConditionOperator.Equal, new Guid("F6213B56-61A7-E211-A560-0050568E0699"));
                FilterExpression con_fe = new FilterExpression(LogicalOperator.And);
                //con_fe.AddCondition(con_ce1);
                con_fe.AddCondition(con_ce2);
                QueryExpression con_qe = new QueryExpression();
                con_qe.EntityName = "annotation";
                con_qe.Criteria = con_fe;
                con_qe.ColumnSet = new ColumnSet("annotationid");
                con_qe.PageInfo.ReturnTotalRecordCount = true;
                EntityCollection con_coll = _service.RetrieveMultiple(con_qe);
                MessageBox.Show("Record Count: " + con_coll.Entities.Count);

                foreach(Entity note in con_coll.Entities)
                {

                    Entity entity = _service.Retrieve(note.LogicalName,note.Id, new ColumnSet(true));
                    organizationUri = new Uri(prod);
                    OrganizationServiceProxy _prodProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
                    _prodProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    IOrganizationService _Prodservice = (IOrganizationService)_prodProxy;

                    _prodProxy.Create(entity);
                }
         

No comments: