private void TestExample()
{
getCRMService();
XrmServiceContext _context = new XrmServiceContext(service);
int Fetchcount = 3;
int PageNumber = 1;
int RecordCount = 0;
string pagingCookie = null;
string FetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
</entity>
</fetch>";
while (true)
{
string xml = CreateXml(FetchXML, pagingCookie, PageNumber, Fetchcount);
RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
EntityCollection entity = ((RetrieveMultipleResponse)service.Execute(fetchRequest)).EntityCollection;
if (entity.MoreRecords)
{
PageNumber++;
}
else
{
BulkUpdate(service, entity);
break;
}
BulkUpdate(service, entity);
}
}
public static void BulkUpdate(IOrganizationService service, EntityCollection entities)
{
ExecuteMultipleRequest req = new ExecuteMultipleRequest();
req.Requests = new OrganizationRequestCollection();
req.Settings = new ExecuteMultipleSettings();
req.Settings.ContinueOnError = true;
req.Settings.ReturnResponses = true;
foreach (var entity in entities.Entities)
{
entity.Attributes.Remove("telephone1");
entity.Attributes["telephone1"] ="121454";
UpdateRequest updateRequest = new UpdateRequest { Target = entity };
req.Requests.Add(updateRequest);
}
ExecuteMultipleResponse responseWithNoResults =
(ExecuteMultipleResponse)_serviceProxy.Execute(req);
if (responseWithNoResults.Responses.Count > 0)
{
foreach (var responseItem in responseWithNoResults.Responses)
{
if (responseItem.Fault != null)
{
}
}
}
else
{
Console.WriteLine("All account records have been updated successfully.");
}
}
public string CreateXml(string xml, string cookie, int page, int count)
{
StringReader stringReader = new StringReader(xml);
XmlTextReader reader = new XmlTextReader(stringReader);
// Load document
XmlDocument doc = new XmlDocument();
doc.Load(reader);
return CreateXml(doc, cookie, page, count);
}
public string CreateXml(XmlDocument doc, string cookie, int page, int count)
{
XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
if (cookie != null)
{
XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
pagingAttr.Value = cookie;
attrs.Append(pagingAttr);
}
XmlAttribute pageAttr = doc.CreateAttribute("page");
pageAttr.Value = System.Convert.ToString(page);
attrs.Append(pageAttr);
XmlAttribute countAttr = doc.CreateAttribute("count");
countAttr.Value = System.Convert.ToString(count);
attrs.Append(countAttr);
StringBuilder sb = new StringBuilder(1024);
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(stringWriter);
doc.WriteTo(writer);
writer.Close();
return sb.ToString();
}
{
getCRMService();
XrmServiceContext _context = new XrmServiceContext(service);
int Fetchcount = 3;
int PageNumber = 1;
int RecordCount = 0;
string pagingCookie = null;
string FetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
</entity>
</fetch>";
while (true)
{
string xml = CreateXml(FetchXML, pagingCookie, PageNumber, Fetchcount);
RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
EntityCollection entity = ((RetrieveMultipleResponse)service.Execute(fetchRequest)).EntityCollection;
if (entity.MoreRecords)
{
PageNumber++;
}
else
{
BulkUpdate(service, entity);
break;
}
BulkUpdate(service, entity);
}
}
public static void BulkUpdate(IOrganizationService service, EntityCollection entities)
{
ExecuteMultipleRequest req = new ExecuteMultipleRequest();
req.Requests = new OrganizationRequestCollection();
req.Settings = new ExecuteMultipleSettings();
req.Settings.ContinueOnError = true;
req.Settings.ReturnResponses = true;
foreach (var entity in entities.Entities)
{
entity.Attributes.Remove("telephone1");
entity.Attributes["telephone1"] ="121454";
UpdateRequest updateRequest = new UpdateRequest { Target = entity };
req.Requests.Add(updateRequest);
}
ExecuteMultipleResponse responseWithNoResults =
(ExecuteMultipleResponse)_serviceProxy.Execute(req);
if (responseWithNoResults.Responses.Count > 0)
{
foreach (var responseItem in responseWithNoResults.Responses)
{
if (responseItem.Fault != null)
{
}
}
}
else
{
Console.WriteLine("All account records have been updated successfully.");
}
}
public string CreateXml(string xml, string cookie, int page, int count)
{
StringReader stringReader = new StringReader(xml);
XmlTextReader reader = new XmlTextReader(stringReader);
// Load document
XmlDocument doc = new XmlDocument();
doc.Load(reader);
return CreateXml(doc, cookie, page, count);
}
public string CreateXml(XmlDocument doc, string cookie, int page, int count)
{
XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
if (cookie != null)
{
XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
pagingAttr.Value = cookie;
attrs.Append(pagingAttr);
}
XmlAttribute pageAttr = doc.CreateAttribute("page");
pageAttr.Value = System.Convert.ToString(page);
attrs.Append(pageAttr);
XmlAttribute countAttr = doc.CreateAttribute("count");
countAttr.Value = System.Convert.ToString(count);
attrs.Append(countAttr);
StringBuilder sb = new StringBuilder(1024);
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(stringWriter);
doc.WriteTo(writer);
writer.Close();
return sb.ToString();
}