///
/// 实现分页查询
///
/// 页数
/// 每页大小
/// 是否成功
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();
}