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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值