web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

HOW-TO does "string Fetch(string fetchXml)" like (CRM 4 - CrmServiceWsdl) in CRM 2016?

(0) ShareShare
ReportReport
Posted on by

Now, I use CRM 2016 and VS 2013.

I want migrate older CRM Services (CRM 4 - CRM 2011) that uses CrmService (WebReference http://server:5555/MSCrmServices/2007/CrmServiceWsdl.aspx)

    public partial class CrmService : System.Web.Services.Protocols.SoapHttpClientProtocol {

 [System.Web.Services.Protocols.SoapHeaderAttribute("CorrelationTokenValue")]
        [System.Web.Services.Protocols.SoapHeaderAttribute("CrmAuthenticationTokenValue")]
        [System.Web.Services.Protocols.SoapHeaderAttribute("CallerOriginTokenValue")]
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://schemas.microsoft.com/crm/2007/WebServices/Fetch", RequestNamespace="http://schemas.microsoft.com/crm/2007/WebServices", ResponseNamespace="http://schemas.microsoft.com/crm/2007/WebServices", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string Fetch(string fetchXml) {
            object[] results = this.Invoke("Fetch", new object[] {
                        fetchXml});
            return ((string)(results[0]));
        }

       

Legacy code:                  

  string result = aCrm.Fetch(fetchQuery);


                    if (result.ToString() == "<resultset morerecords=\"0\" />")
                    {
                        return "<codigoerror>01</codigoerror><descripcion>Mediador sin campañas</descripcion>";
                    }
                    else
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml(result);
                        XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("result");

                        for (int i = 0; i < xmlNodes.Count; i++)
                        {
                            string fetchquery2 = string.Empty;
                            fetchquery2 = ConsultasFetch.MostrarInfoCampaña();
                            fetchquery2 = fetchquery2.Replace("MiVariable1", MediadorCodeCPC);
                            fetchquery2 = fetchquery2.Replace("MiVariable2", xmlNodes.Item(i).FirstChild.FirstChild.Value.ToString());
                            string result2 = aCrm.Fetch(fetchquery2);

                            XmlDocument xmlDoc2 = new XmlDocument();
                            xmlDoc2.LoadXml(result2);
                            XmlNodeList xmlNodes2 = xmlDoc2.GetElementsByTagName("result");

                            for (int j = 0; j < xmlNodes2.Item(0).ChildNodes.Count; j++)

                            {
                                if (xmlNodes2.Item(0).ChildNodes.Item(j).Name == "new_informada")
                                {
                                    XmlElement xmlelem1 = xmlDoc.CreateElement("Informada");
                                    xmlelem1.InnerText = xmlNodes2.Item(0).ChildNodes[j].InnerXml;
                                    xmlDoc.ChildNodes.Item(0).ChildNodes.Item(i).AppendChild(xmlelem1);
                                }
                                else if (xmlNodes2.Item(0).ChildNodes.Item(j).Name == "new_fechainformada")
                                {
                                    XmlElement xmlelem2 = xmlDoc.CreateElement("Fecha");
                                    xmlelem2.InnerText = xmlNodes2.Item(0).ChildNodes[j].Attributes["date"].Value;
                                    xmlDoc.ChildNodes.Item(0).ChildNodes.Item(i).AppendChild(xmlelem2);

                                }
                                else if (xmlNodes2.Item(0).ChildNodes.Item(j).Name == "new_interescompromiso")
                                {
                                    XmlElement xmlelem3 = xmlDoc.CreateElement("Interes");
                                    xmlelem3.InnerText = xmlNodes2.Item(0).ChildNodes[j].InnerXml;
                                    xmlDoc.ChildNodes.Item(0).ChildNodes.Item(i).AppendChild(xmlelem3);
                                }

                            }


                        }

 


Any suggestions or alternative?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    You can execute queries in Fetch XML in 2016 by using the ExecuteFetchRequest class:

    msdn.microsoft.com/.../microsoft.crm.sdk.messages.executefetchrequest.aspx

    That will return an ExecuteFetchResponse class which has the matching results.

    Alternate approach:

    msdn.microsoft.com/.../gg328117.aspx

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Great, ExecuteFetchRequest Deprecated

     [Obsolete("DEPRECATED. ExecuteFetchRequest Deprecated. Use the RetrieveMultipleRequest class. https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.executefetchrequest.aspx?f=255&MSPPError=-2147217396")]

           public string Fetch(string fetchXml)

           {

               // TODO: Dynamics CRM 2011 FetchXML result convert to XML file

               // prabirchoudhury.wordpress.com/.../dynamics-crm-2011-fetchxml-result-convert-to-xml-file

               // var ec = base.RetrieveMultiple(new FetchExpression(fetchXml));

               // TODO: ExecuteFetchRequest DEPRECATED

               var fetchResult = (ExecuteFetchResponse)base.Execute(new ExecuteFetchRequest() {

                   FetchXml = fetchXml,

               });

               //fetchResult.ResponseName

               //fetchResult.Results

               return fetchResult.FetchXmlResult;

           }

    Alternate approach:

    For me not valid use RetrieveMultipleRequest , we need FetchXml results as XML format.

    For EntityCollection , Foreach loop through the result and Build the XML node.

    It's harder and pain for legacy code.

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Did you look at the ExecuteFetchResponse class? There's a deprecated string property FetchXmlResult that, I think, will have the text results.

    I'd strongly suggest you start updating your code to use the proper query expressions though - if you're upgrading the code to use something that's deprecated you might as well put in a little more effort and do it in a way that won't break with the next updates.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans