Skip to content

Instantly share code, notes, and snippets.

@rappen
Last active January 16, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rappen/11a89553ff26c400c0c100db43c442ff to your computer and use it in GitHub Desktop.
Save rappen/11a89553ff26c400c0c100db43c442ff to your computer and use it in GitHub Desktop.
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Xml;
namespace Rappen.Xrm.Extensions
{
public static class ServiceExtensions
{
/// <summary>
/// Converts from QueryBase to FetchXML, removing invalid attribute useraworderby to validate with fetch.xsd
/// </summary>
/// <param name="organizationService"></param>
/// <param name="query"></param>
/// <returns></returns>
public static string QueryExpressionToFetchXml(this IOrganizationService organizationService, QueryBase query)
{
QueryExpressionToFetchXmlRequest request = new QueryExpressionToFetchXmlRequest()
{
Query = query
};
QueryExpressionToFetchXmlResponse response = (QueryExpressionToFetchXmlResponse)organizationService.Execute(request);
var doc = new XmlDocument();
doc.LoadXml(response.FetchXml);
var fetchnode = doc.SelectSingleNode("fetch");
if (fetchnode != null && fetchnode.Attributes["useraworderby"] != null)
{
fetchnode.Attributes.RemoveNamedItem("useraworderby");
}
return doc.OuterXml;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment