<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[ASP.Net - Alexander Development]]></title><description><![CDATA[ASP.Net - Alexander Development]]></description><link>https://alexanderdevelopment.net/</link><image><url>https://alexanderdevelopment.net/favicon.png</url><title>ASP.Net - Alexander Development</title><link>https://alexanderdevelopment.net/</link></image><generator>Ghost 1.20</generator><lastBuildDate>Fri, 24 Apr 2026 14:17:25 GMT</lastBuildDate><atom:link href="https://alexanderdevelopment.net/tag/asp-net/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Displaying Dynamics CRM FetchXML results in ASPX with XSLT]]></title><description><![CDATA[<div class="kg-card-markdown"><p>Last week I wrote a <a href="https://alexanderdevelopment.net/post/2013/02/21/Accessing-raw-SOAP-requests-responses-from-Dynamics-CRM-web-services-in-C.aspx">post</a> that showed how to retrive the raw SOAP response from a Dynamics CRM query in C#, but I didn't show how to do anything useful with it. In today's post I will show a practical example of how to execute a FetchXML request against</p></div>]]></description><link>https://alexanderdevelopment.net/post/2013/02/26/displaying-dynamics-crm-fetchxml-results-xslt/</link><guid isPermaLink="false">5a5837226636a30001b9771f</guid><category><![CDATA[Microsoft Dynamics CRM]]></category><category><![CDATA[CRM 2011]]></category><category><![CDATA[programming]]></category><category><![CDATA[C#]]></category><category><![CDATA[FetchXML]]></category><category><![CDATA[ASP.Net]]></category><dc:creator><![CDATA[Lucas Alexander]]></dc:creator><pubDate>Wed, 27 Feb 2013 00:00:00 GMT</pubDate><content:encoded><![CDATA[<div class="kg-card-markdown"><p>Last week I wrote a <a href="https://alexanderdevelopment.net/post/2013/02/21/Accessing-raw-SOAP-requests-responses-from-Dynamics-CRM-web-services-in-C.aspx">post</a> that showed how to retrive the raw SOAP response from a Dynamics CRM query in C#, but I didn't show how to do anything useful with it. In today's post I will show a practical example of how to execute a FetchXML request against a Dynamics CRM instance, capture the raw SOAP response and transform it with XSLT for display in an ASPX page. For demonstration purposes, I will be using the same query and XSL as I used in my <a href="https://alexanderdevelopment.net/post/2013/02/11/Displaying-FetchXML-results-with-XSLT-on-the-client-side-in-a-Dynamics-CRM-2011-web-resource.aspx">&quot;Displaying FetchXML results with XSLT on the client side in a Dynamics CRM 2011 web resource&quot;</a> post.</p>
<p>In this example I am using a WSDL-based proxy instead of the CRM SDK, but all of the code I am sharing can be easily modified to work with the SDK instead.</p>
<p>My ASPX page has five controls:</p>
<ol>
<li>xmlTextBox textbox to input the FetchXML query</li>
<li>xslTextBox textbox to input the XSL for the transformation</li>
<li>executeButton button to retrieve and transform the data</li>
<li>resultsTextBox textbox to show the SOAP response</li>
<li>resultsLiteral literal to show the transformed data</li>
</ol>
<p>When the executeButton button is clicked, the following steps take place:</p>
<ol>
<li>Set up a WCF client connection to the CRM OrganizationService (with client message inspector as described in my last post)</li>
<li>Retrieve the results for the supplied FetchXML query</li>
<li>Show the SOAP response</li>
<li>Transform the SOAP response using the supplied XSL</li>
<li>Show the transformed results</li>
</ol>
<p>Here's the code:</p>
<pre><code>/// &lt;summary&gt;  
/// executes retrieve and transform functionality  
/// &lt;/summary&gt;  
/// &lt;param name=&quot;sender&quot;&gt;&lt;/param&gt;  
/// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;  
protected void executeButton_Click(object sender, EventArgs e)  
{  
OrganizationServiceClient client = SetupClient();  
string results = RetrieveData(this.xmlTextBox.Text, client);  
  
string xsl = this.xslTextBox.Text;  
this.resultsTextBox.Text = results;  
this.resultsLiteral.Text = DoTransform(results, xsl);  
}  
  
/// &lt;summary&gt;  
/// instantiates an OrganizationServiceClient object for the username/password/organization service specified as constants  
/// &lt;/summary&gt;  
/// &lt;returns&gt;&lt;/returns&gt;  
private OrganizationServiceClient SetupClient()  
{  
ClientCredentials credentials = new ClientCredentials();  
credentials.UserName.UserName = UserName; //UserName is a constant  
credentials.UserName.Password = UserPassword; //UserPassword is a constant  
  
OrganizationServiceClient client = new OrganizationServiceClient(&quot;CustomBinding_IOrganizationService&quot;,  
new EndpointAddress(OrganizationServiceUrl)); //OrganizationServiceUrl is a constant  
client.ClientCredentials.UserName.UserName = credentials.UserName.UserName;  
client.ClientCredentials.UserName.Password = credentials.UserName.Password;  
return client;  
}  
  
/// &lt;summary&gt;  
/// executes a fetchxml query with a given org service client and returns the SOAP response as a string  
/// &lt;/summary&gt;  
/// &lt;param name=&quot;fetchXml&quot;&gt;query&lt;/param&gt;  
/// &lt;param name=&quot;client&quot;&gt;client&lt;/param&gt;  
/// &lt;returns&gt;response&lt;/returns&gt;  
string RetrieveData(string fetchXml, OrganizationServiceClient client)  
{  
string response = &quot;&quot;;  
  
//set up a client message inspector for the query  
//see http://alexanderdevelopment.net/post/2013/02/21/Accessing-raw-SOAP-requests-responses-from-Dynamics-CRM-web-services-in-C.aspx  
MyInspector inspector = new MyInspector();  
client.Endpoint.Behaviors.Add(inspector);  
  
//build the fetchxml  
FetchExpression fetch = new FetchExpression();  
fetch.Query = fetchXml;  
EntityCollection entities = client.RetrieveMultiple(fetch);  
  
//if there is a captured response  
if (inspector.receivedMessages.Count == 1)  
{  
response = inspector.receivedMessages[0];  
}  
  
//return it  
return response;  
}  
  
/// &lt;summary&gt;  
/// transforms supplied results xml with supplied xsl  
/// &lt;/summary&gt;  
/// &lt;param name=&quot;results&quot;&gt;input xml&lt;/param&gt;  
/// &lt;param name=&quot;xsl&quot;&gt;xsl style sheet&lt;/param&gt;  
/// &lt;returns&gt;transformed output&lt;/returns&gt;  
string DoTransform(string results, string xsl)  
{  
//create an xmldocument from the input xml  
System.Xml.XmlDocument xmlDoc = new XmlDocument();  
xmlDoc.LoadXml(results);  
  
//create an xmldocument from the input xsl  
System.Xml.XmlDocument xslDoc = new XmlDocument();  
xslDoc.LoadXml(xsl);  
  
//load a compiled xsl transform object from the xsl document  
XslCompiledTransform transform = new XslCompiledTransform();  
transform.Load(xslDoc);  
  
//prepare stringwriter and xmlwriter get string output from transformation  
StringWriter sw = new StringWriter();  
XmlWriter xwriter = XmlWriter.Create(sw);  
  
//execute the transformation  
transform.Transform(xmlDoc, xwriter);  
  
//return the output  
string output = sw.ToString();  
return output;  
}
</code></pre>
<p>My .aspx and .aspx.cs files are attached here for reference.</p>
<ol>
<li><a href="https://alexanderdevelopment.net/content/images/2013/02/DisplayXml.aspx.txt">DisplayXml.aspx (3.16 kb)</a></li>
<li><a href="https://alexanderdevelopment.net/content/images/2013/02/DisplayXml.aspx.cs.txt">DisplayXml.aspx.cs (4.95 kb)</a></li>
</ol>
</div>]]></content:encoded></item></channel></rss>