ページサイズの大きい結果セット FetchXML

ページング クッキーを使用して、クエリの結果をページングできます。 FetchXML ページング Cookie は、アプリケーション内で大きなデータセットのページングを高速に実行するためのパフォーマンス機能です。 一連のレコードにクエリを実行すると、その結果にページング Cookie の値が含まれます。 より良いパフォーマンスを得ることを目的として、次の一連のレコードを取得するときにその値を渡すことができます。

FetchXML そして QueryExpression ページング クッキーに異なる形式を使用します。 FetchXmlToQueryExpressionRequest メッセージまたはQueryExpressionToFetchXmlRequest メッセージを使用して、クエリ形式を別の形式に変換すると、ページング Cookie の値は無視されます。 また、連続していないページを要求した場合も、ページング Cookie の値は無視されます。

FetchXML でページング クッキーを使用する場合は、クッキー値をXMLでエンコードする必要があります。 次の例は、 FetchXML でページング クッキーを使用した場合のXMLエンコードされたクッキーの外観を示しています。

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>";  

FetchXML ページングCookieの例

次の例は、 FetchXML クエリでページング クッキーを使用する方法を示しています。 完全なサンプル コードについては、「 サンプル: ページングCookieの使用 FetchXML 」 を参照してください。

// Define the fetch attributes.
// Set 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;
// Specify the current paging cookie. For retrieving the first page, 
// pagingCookie should be null.
string pagingCookie = null;

// Create the FetchXml string for retrieving all child accounts to a parent account.
// This fetch query is using 1 placeholder to specify the parent account id 
// for filtering out required accounts. Filter query is optional.
// Fetch query also includes optional order criteria that, 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("Retrieving data in pages\n"); 
Console.WriteLine("#\tAccount Name\t\t\tEmail Address");

while (true)
{
    // Build fetchXml string with the placeholders.
    string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);

    // Excute the fetch query and get the xml result.
    RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
    {
        Query = new FetchExpression(xml)
    };

    EntityCollection returnCollection = ((RetrieveMultipleResponse)_service.Execute(fetchRequest1)).EntityCollection;
    
    foreach (var c in returnCollection.Entities)
    {
        System.Console.WriteLine("{0}.\t{1}\t\t{2}", ++recordCount, c.Attributes["name"], c.Attributes["emailaddress1"] );
    }                        
    
    // Check for morerecords, if it returns 1.
    if (returnCollection.MoreRecords)
    {
        Console.WriteLine("\n****************\nPage number {0}\n****************", pageNumber);
        Console.WriteLine("#\tAccount Name\t\t\tEmail Address");
        
        // Increment the page number to retrieve the next page.
        pageNumber++;

        // Set the paging cookie to the paging cookie returned from current results.                            
        pagingCookie = returnCollection.PagingCookie;
    }
    else
    {
        // If no more records in the result nodes, exit the loop.
        break;
    }
}

このコードは、以下に示す静的な CreateXml メソッドに依存しています。

public static string CreateXml(string xml, string cookie, int page, int count)
{
    StringReader stringReader = new StringReader(xml);
    var reader = new XmlTextReader(stringReader);

    // Load document
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);

    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();
}

参照

ページング動作と並び順
サンプル: ページングCookieを使用する FetchXML
クエリの構築 FetchXML
会計年度の日付と日付時刻のクエリ演算子 FetchXML
使用 FetchXML
QueryExpression を使用して大きな結果セットをページングする

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。