Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Web Api

Posted on by 850

Hi All,

I am trying to filter look up using fetchXML using Web Api  .

i can do it in normal way. But i am trying implement the same using web api.

Please help anyone have any idea.

Thank you.

*This post is locked for comments

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Web Api

    Hi,

    WebApi is not using "fetchXml" filtering syntax. Have a look at the examples here:

    https://msdn.microsoft.com/en-us/library/gg334767.aspx#BKMK_FilterNavProperties

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Web Api

    Hi,

    try to build it with the CRMRestBuilder: github.com/.../CRMRESTBuilder

  • naZir Profile Picture
    naZir 850 on at
    RE: Web Api

    i have already done this in same way.

    I am just curious is it possible to do using web api or not

  • Shahbaaz Ansari Profile Picture
    Shahbaaz Ansari 6,203 on at
    RE: Web Api

    Hi Nazir,

    below is my code i have used for filtering product lookup..hope this help

    function preFilterProductLookup() {

    // this is for filtering product on form

    Xrm.Page.getControl("productid").addPreSearch(function () {

    var address = Xrm.Page.getAttribute("new_residenceaddress").getValue()[0].id;

    var contact = Xrm.Page.getAttribute("responsiblecontactid").getValue()[0].id;

    //get all the related product from salesorder

    var fetchproduct ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+

    "<entity name='salesorder'>"+

    " <attribute name='new_productsg1' />"+    

    "<filter type='and'>"+

    " <condition attribute='new_residenceaddress' operator='eq' value= '" + address +"'/>"+

    " <condition attribute='new_primarycontact' operator='eq' value= '" + contact +"'/>"+

    "</filter>"+                                                                  

    "</entity>"+

    "</fetch>";

    var fetchproductdetail = XrmServiceToolkit.Soap.Fetch(fetchproduct);

    var fetchXml;

    if (fetchproductdetail.length > 0) {

    var fetchXml = "<filter type='and'><condition attribute='productid' operator='in'>";

    for(var i=0;i<fetchproductdetail.length; i++)

    {

    if(fetchproductdetail[i].attributes["new_productsg1"] != undefined)

    {

    fetchXml += "<value>" + fetchproductdetail[i].attributes["new_productsg1"].id +" </value>";

    }

    }

    fetchXml +=   "</condition></filter>";

    }

    Xrm.Page.getControl("productid").addCustomFilter(fetchXml);

    });

    Thanks,

    Shhabaaz

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Web Api

    Hi Nazir,

    Please try with the sample in the link.

  • naZir Profile Picture
    naZir 850 on at
    RE: Web Api

    No error.

    simply not filtering the records in look up.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Web Api

    Hi Nazir,

    Seems your fetchxml have some issue. What error your are getting ?

    Try to make simple fetchxml first to test.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
  • naZir Profile Picture
    naZir 850 on at
    RE: Web Api

    function FilteringLookUpField()

    {

       debugger;

       var LoggedInUserID=Xrm.Page.context.getUserId();

       var fecthXML = "<filter type='and'><condition attribute='ownerid' operator='eq' value='"+LoggedInUserID+"' /></filter>";

       var encodedFetchXml = encodeURI(fecthXML);

       var req = new XMLHttpRequest();

       req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?fetchXml="+encodedFetchXml, true);

       req.setRequestHeader("OData-MaxVersion", "4.0");

       req.setRequestHeader("OData-Version", "4.0");

       req.setRequestHeader("Accept", "application/json");

       req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

       req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

       req.onreadystatechange = function() {

           if (this.readyState === 4) {

               req.onreadystatechange = null;

               if (this.status === 200) {

                   var results = JSON.parse(this.response);

                   var recordCount = results["@odata.count"];

                   alert(recordCount);

                   //if(result.value.length==0)

                   //{

                   //    alert("There is no Account asscoiated to this owner");

                   //}

                   //else

                   //{

                   //    for(var i=0;i<result.value.length;i++)

                   //    {

                   //        accountId=res

                   //    }

                   //}

               } else {

                   Xrm.Utility.alertDialog(this.statusText);

               }

           }

       };

       req.send();

    }

    This is my code. and it is not working :(

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

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans