通过fetchXml 实现分页查询

///
/// 实现分页查询
///
/// 页数
/// 每页大小
/// 是否成功
public string GetEntityList( IOrganizationService crmService, int page, int size)
{
String fetchXml = @”标准fetch查询,奇葩的是粘贴到这里之后竟然不能显示”;
//解析Xml并分页后返回Json
String retValue = GetDateByFetchXml(crmService, fetchXml, page, size);
return retValue;
}

以下方法是SDK中的标准方法:

#region 通过FetchXml查询数据
///
/// 通过FetchXml查询数据
///
/// OrgService
/// 查询FetchXml
/// 页码
/// 每页记录数
/// JsonString
public string GetDateByFetchXml(IOrganizationService _service, string fetchXml, int pageNumber, int pageSize)
{
// Specify the current paging cookie. For retrieving the first page,
// pagingCookie should be null.
string pagingCookie = null;

        // Build fetchXml string with the placeholders.
        string xml = CreateXml(fetchXml, pagingCookie, pageNumber, pageSize);
        // Excute the fetch query and get the xml result.
        RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
        {
            Query = new FetchExpression(xml)
        };

        EntityCollection returnCollection = ((RetrieveMultipleResponse)_service.Execute(fetchRequest1)).EntityCollection;


        string jsonString = JsonConvert.SerializeObject(returnCollection);

        return jsonString;

    }

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

    private 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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值