using System; using System.Net; using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Channels; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Xml.Xsl; using System.Text; using System.IO; using LucasDemoCrmWeb.CrmSdk; namespace LucasDemoCrmWeb { public partial class DisplayXml : System.Web.UI.Page { #region Constants /// /// User Name /// private const string UserName = "USERNAME@DOMAIN"; /// /// Password /// private const string UserPassword = "PASSWORD"; /// /// Organization service endpoint /// private const string OrganizationServiceUrl = "https://CRMURL/XRMServices/2011/Organization.svc"; #endregion protected void Page_Load(object sender, EventArgs e) { } /// /// executes retrieve and transform functionality /// /// /// 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); } /// /// instantiates an OrganizationServiceClient object for the username/password/organization service specified as constants /// /// 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("CustomBinding_IOrganizationService", new EndpointAddress(OrganizationServiceUrl)); //OrganizationServiceUrl is a constant client.ClientCredentials.UserName.UserName = credentials.UserName.UserName; client.ClientCredentials.UserName.Password = credentials.UserName.Password; return client; } /// /// executes a fetchxml query with a given org service client and returns the SOAP response as a string /// /// query /// client /// response string RetrieveData(string fetchXml, OrganizationServiceClient client) { string response = ""; //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; } /// /// transforms supplied results xml with supplied xsl /// /// input xml /// xsl style sheet /// transformed output 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; } } }