public static void DataverseConnector()
{
try
{
string conn = @"AuthType=OAuth;Username=user@org.onmicrosoft.com;Password=password;Url=https://orgapp.crm.dynamics.com;TokenCacheStorePath=c:\MyTokenCache2;LoginPrompt=Auto";
using (ServiceClient serviceClient = new ServiceClient(conn))
{
if (serviceClient.IsReady)
{
RetriveRecords(serviceClient);
}
else
{
Console.WriteLine("A web service connection was not established.");
}
}
// Pause the console so it does not close.
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
private static void RetriveRecords(ServiceClient serviceClient)
{
int queryCount = 3;
int pageNumber = 1;
int recordCount = 0;
QueryExpression pagequery = convertFecthXMLToQueryExpression(serviceClient);
pagequery.PageInfo = new PagingInfo();
pagequery.PageInfo.Count = queryCount;
pagequery.PageInfo.PageNumber = pageNumber;
// The current paging cookie. When retrieving the first page,
// pagingCookie should be null.
pagequery.PageInfo.PagingCookie = null;
Console.WriteLine("Retrieving account records in pages...\n");
while (true)
{
// Retrieve the page.
EntityCollection results = serviceClient.RetrieveMultiple(pagequery);
if (results.Entities != null)
{
// Retrieve all records from the result set.
foreach (var acct in results.Entities)
{
Console.WriteLine("{0}.\t{1}", ++recordCount, acct["name"]);
}
}
// Check for more records, if it returns true.
if (results.MoreRecords)
{
Console.WriteLine("\n****************\nPage number {0}\n****************", pagequery.PageInfo.PageNumber);
// Increment the page number to retrieve the next page.
pagequery.PageInfo.PageNumber++;
// Set the paging cookie to the paging cookie returned from current results.
pagequery.PageInfo.PagingCookie = results.PagingCookie;
}
else
{
// If no more records are in the result nodes, exit the loop.
break;
}
}
}
private static QueryExpression convertFecthXMLToQueryExpression(ServiceClient serviceClient)
{
var query = @"<fetch mapping='logical'><entity name='account'><attribute name='accountid'/><attribute name='name'/><attribute name='numberofemployees'/></entity></fetch>";
FetchXmlToQueryExpressionRequest fetchToQueryExpRequest = new FetchXmlToQueryExpressionRequest();
fetchToQueryExpRequest.FetchXml = query;
FetchXmlToQueryExpressionResponse fetchXmlToQueryExpressionResponse = (FetchXmlToQueryExpressionResponse)serviceClient.Execute(fetchToQueryExpRequest);
QueryExpression myquery = fetchXmlToQueryExpressionResponse.Query;
return myquery;
}
other way to fetch item using FetchXML in paging
https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/page-large-result-sets-with-fetchxml
No comments:
Post a Comment