Monday, September 10, 2012

Testing of FetchXml

I have come across few instances where I want to test the custom written fetch xml query to test against the CRM. The reason being you cannot test aggregate queries in CRM and if you need to be sure to test the output before implementation, here you go with the solution. You can see the output so that it's easy before implementing the actual query.
Create a VS 2010 project (form based application).
Design the form by creating the following

Type
Name
Label
Button
buttonClearInput
Clear Input
Button
buttonClearOutput
Clear Output
Button
convert
Convert
TextBox
FetchXmlReq
Fetch Xml
TextBox
FetchXmlResp
Fetch Xml Response








        private void buttonClearInput_Click(object sender, EventArgs e)
        {
            FetchXmlReq.Text = "";
        }

        private void buttonClearOutput_Click(object sender, EventArgs e)
        {
            FetchXmlResp.Text = "";
        }

        private void convert_Click(object sender, EventArgs e)
        {
            string fetchXml = FetchXmlReq.Text;
            string fetchXmlResp =fetchXml;
            fetchXmlResp = testFetch(fetchXml);
            FetchXmlResp.Text = fetchXmlResp;
        }





private string testFetch(string fetchXml)
        {
            Uri organizationUri = new Uri("http://servername:port/SSBCapgemini/XRMServices/2011/Organization.svc");
            Uri homeRealmUri = null;
            ClientCredentials credentials = new ClientCredentials();

            credentials.Windows.ClientCredential.Domain = "domain";
            credentials.Windows.ClientCredential.UserName = "username";
            credentials.Windows.ClientCredential.Password = "Password";
            String fetchXmlresp = string.Empty;
            Guid _accountid = Guid.Empty;
            OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
            IOrganizationService _service = (IOrganizationService)orgProxy;
            try
            {
              
                /*
                string estimatedvalue_avg = @"  <fetch version='1.0' mapping='logical' distinct='false' aggregate='true'>
                                                  <entity name='ssb_stock'>
                                                    <attribute name='ssb_market_value' aggregate='SUM' alias='sum_market_value'/>
                                                    <attribute name='ssb_cost_value' aggregate='SUM' alias='sum_cost_value'/>
                                                    <filter>
                                                      <condition attribute='ssb_contact' operator='eq' value='{AE6A2F6E-13B9-8451-3775-0E328DF08230}' />
                                                    </filter>
                                                  </entity>
                                                </fetch>";
                //*/
                string estimatedvalue_avg = fetchXml;

                EntityCollection estimatedvalue_avg_result = _service.RetrieveMultiple(new FetchExpression(estimatedvalue_avg));

                foreach (var c in estimatedvalue_avg_result.Entities)
                {
                    decimal aggregate1 = ((Money)((AliasedValue)c.Attributes["sum_market_value"]).Value).Value;
                    decimal aggregate2 = ((Money)((AliasedValue)c.Attributes["sum_cost_value"]).Value).Value;
                    fetchXmlresp += "Sum of Market value: " + aggregate1 + Environment.NewLine;
                    fetchXmlresp += "Sum of Cost value: " + aggregate2;

                }

                /*
                // Display the results.
                fetchXmlresp += @"List all stocks matching specified parameters";
                fetchXmlresp += @"===============================================";
                foreach (var e in entityResults.Entities)
                {
                    fetchXmlresp += String.Format("Stock ID: {0}\n", e.Id;
                }


                fetchXmlresp += "<End of Listing>\n\n";
                 */
            }
            catch (Exception ex)
            {
               fetchXmlresp += ex.Message;
            }
           
            return fetchXmlresp;
        }

2 comments:

SirDagdlahan said...

How are you using CRM 2011 on VS 2010? I haven't seen an extension for it since they replaced BIDS in Sql 2012.

Unknown said...

Hi,

I did not understand your comments. Do you mean you are not able to use VS 2010 for loading data into CRM. If so yes, you cannot use VS 2010 and you need to use BIDS (VS2008) for the same. The drawback is BIDS 2008 uses .net framework 3.5 and so none of the dlls from crm sdk 2011 will not work.
Apart from this you can use VS 2010 with crm 2011.In fact there is a VS extension from microsoft. You can find it in the sdk tools