CRM 2011 fetchxml查询取超过5千笔

 protected void loadUnTaskInfo(IOrganizationService server)
    {
        string untaskXml = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                            <entity name='task'>
                                            <attribute name='activityid' />
                                            <attribute name='subject' />
                                            <attribute name='new_prioritylevel' />
                                            <attribute name='regardingobjectid' />
                                            <attribute name='scheduledstart' />
                                            <attribute name='scheduledend' />
                                            <attribute name='ownerid' />
                                            <order attribute='createdon' descending='true' />
                                            <filter type='and'>
                                                <condition attribute='statecode' operator='eq' value='0' />
                                            </filter>
                                            <link-entity name='systemuser' from='systemuserid' to='owninguser' visible='false' link-type='outer' alias='users'>
                                                <attribute name='businessunitid' />
                                            </link-entity>
                                            </entity>
                                        </fetch>");

        int fetchCount = 300; //定义每页查询的结果数
        int pageNumber = 1;
        string pagingCookie = null;

string  landname = “”;
        while (true)
        {
            string xml = CreateXml(untaskXml, pagingCookie, pageNumber, fetchCount);
            EntityCollection returnCollection = server.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(xml));

            foreach (var untask in returnCollection.Entities)
            {               landname = (untask.Contains("new_landname")) ? untask.Attributes["new_landname"].ToString() : "";
            }

            if (returnCollection.MoreRecords)
            {
                pageNumber++;
            }
            else
            {
                break;
            }
        }
}


    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();
原文地址:https://www.cnblogs.com/z1984/p/3161234.html