Download Microsoft CRM 2011 SDK - SDK voorpagina - CRM Performance - CRM Hosting
Heeft u vragen? Bel ons op 030 800 8008

In Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, you can use the paging cookie feature to make paging in an application faster for large datasets. The feature is available in both FetchXML and QueryExpression queries. When you use the paging cookie feature when querying a set of records, the result contains a value for the paging cookie. To improve system performance, you can then pass that value when you retrieve the next set of records.

FetchXML and QueryExpression use different formats for their paging cookies. If you convert from one query format to the other by using the FetchXmlToQueryExpressionRequest message or the QueryExpressionToFetchXmlRequest message, the paging cookie value is ignored.In addition, if you request nonconsecutive pages, the paging cookie value is ignored.

When you use the paging cookie withFetchXML, make sure that you use the correct encoding as shown in the following example:

C#
strQueryXML = "<fetch mapping='logical' paging-cookie='&lt;cookie page=&quot;1&quot;&gt;&lt;accountid last=&quot;{E062B974-7F8D-DC11-9048-0003FF27AC3B}&quot; first=&quot;{60B934EF-798D-DC11-9048-0003FF27AC3B}&quot;/&gt;&lt;/cookie&gt;' page='2' count='2'><entity name='account'><all-attributes/></entity></fetch>";

In This Section

Using a Paging Cookie with Query Expression

The following example shows how to use the paging cookie with a query expression:

C#
//  Query using the paging cookie.

// Define the paging attributes.
// The number of records per page to retrieve.
int fetchCount = 3;
// Initialize the page number.
int pageNumber = 1;
// Initialize the number of records.
int recordCount = 0;

// Define the condition expression for retrieving records.
ConditionExpression pagecondition = new ConditionExpression();
pagecondition.AttributeName = "address1_stateorprovince";
pagecondition.Operator = ConditionOperator.Equal;
pagecondition.Values.Add("WA");

// Define the order expression to retrieve the records.
OrderExpression order = new OrderExpression();
order.AttributeName = "name";
order.OrderType = OrderType.Ascending;

// Create the query expression and add condition.
QueryExpression pagequery = new QueryExpression();
pagequery.EntityName = "account";
pagequery.Criteria.AddCondition(pagecondition);
pagequery.Orders.Add(order);
pagequery.ColumnSet.AddColumns("name", "address1_stateorprovince", "emailaddress1", "accountid");

// Assign the pageinfo properties to the query expression.
pagequery.PageInfo = new PagingInfo();
pagequery.PageInfo.Count = fetchCount;
pagequery.PageInfo.PageNumber = pageNumber;
// The current paging cookie. When retrieving the first page, 
// pagingCookie should be null.
pagequery.PageInfo.PagingCookie = null;

Console.WriteLine("#\tAccount Name\t\t\tEmail Address");

while (true)
{
    // Retrieve the page.
    EntityCollection results = _serviceProxy.RetrieveMultiple(pagequery);

    if (results.Entities != null)
    {
        // Retrieve all records from the result set.
        foreach (Account acct in results.Entities)
        {
            Console.WriteLine(
           "{0}.\t{1}\t\t{2}",
           ++recordCount,
           acct.EMailAddress1,
           acct.Name);
        }
    }

    // Check for more records, if it returns true.
    if (results.MoreRecords)
    {
        // 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;
    }
}

Using a Paging Cookie with FetchXML

The following example shows how to use the paging cookie with FetchXML:

C#
// Define the FetchXML attributes.
// The number of records per page to retrieve.
int fetchCount = 3;
// Initialize the page number.
int pageNumber = 1;
// Initialize the number of records.
int recordCount = 0;
// The current paging cookie. For retrieving the first page, 
// pagingCookie should be null.
string pagingCookie = null;

// The parent account Id would typically be required in case you intend 
// to filter the number of account records by the associated parent account.
parentAccountId = new Guid("{832CF447-4E70-45db-A6BE-14F1AB343076}");

// Create the FetchXml string for retrieving all child accounts to a parent account.
// This FetchXml query is using 1 placeholder to specify the parent account id 
// for filtering out required accounts. Filter query is optional.
// The FetchXml query also includes optional order criteria, which, in this case, is used 
// to order the results in ascending order on the name data column.
string fetchXml = string.Format(@"<fetch version='1.0' 
    mapping='logical' 
    output-format='xml-platform'>
    <entity name='account'>
        <attribute name='name' />
        <attribute name='emailaddress1' />
        <order attribute='name' descending='false'/>
        <filter type='and'>
    <condition attribute='parentaccountid' 
operator='eq' value='{0}' uiname='' uitype='' />
        </filter>
    </entity>
</fetch>",
parentAccountId);

Console.WriteLine("#\tAccount Name\t\t\tEmail Address");

while (true)
{
    // Build the FetchXml string with the placeholders.
    string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
    
    // Execute the FetchXml query and get the XML result.
    string fetchResult = service.Fetch(xml);

    // Load the FetchXml result into XMLDocument to parse its contents.
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(fetchResult);

    // The morerecords attribute will return 1 if there are more records.
    string moreRecords = ExtractAttribute(doc, "morerecords");

    // The paging-cookie attribute holds the paging cookie to pass in the next query.
    pagingCookie = ExtractAttribute(doc, "paging-cookie");

    // Retrieve the result nodes.
    XmlNodeList resultNodes = doc.DocumentElement.SelectNodes("result");

    // Retrieve all records from the result set.
    foreach (XmlNode resultNode in resultNodes)
    {   
        Console.WriteLine(
           "{0}.\t{1}\t\t{2}",
           ++recordCount,
           ExtractNodeValue(resultNode, "name"),
           ExtractNodeValue(resultNode, "emailaddress1"));
    }

    // Check for morerecords, if it returns 1.
    if (moreRecords != null && moreRecords == "1")
    {
        // Increment the page number to retrieve the next page.
        pageNumber++;
    }
    else
    {
        // If no more records in the result nodes, exit the loop.
        break;
    }
}

See Also


© Resultondemand.nl   Hosting -  Performance -  Upgrade -  Nieuw -  Over ons -  Contact -  CRM 2011 proberen

2AT - Netricity - CRM boeken

Microsoft Partner? Bekijk hier ons partner aanbod