Question

I have an external list with one wildcard filter named taskname. When I edit the filter directy in the view I get all the information i want to get.

The problem is when I try to set the filter for the taskname dynamically.

Is it possible that to get information using REST API? When I try to query the items and filter it. i dont get any results.

Is it possible to change the view with jQuery? For example, first I set a variable in the view for the filter, which can be updated at a later point with the filter name.

Thanks for every helpful answer =)

Était-ce utile?

La solution

today I found a REST approach to do that

function QueryList()
{
    var uniqueViewId = "MustHavAnIDToWork";
    var methodName = "NameOfTheReadMethodInExternalContentType";
    var taskValue = "YourTaskValueToSearch";
    var query = '<View Name="' + uniqueViewId + '"><Method Name="'+ methodName + '"><Filter Name="taskname" Value="' + taskValue + '" /></Method><Query></Query><ViewFields><FieldRef Name="Colum1" /><FieldRef Name="Column2" /><FieldRef Name="Column3" /> ...List ALL Columns of the External List of the Method you are Calling!!!...</ViewFields></View>';

    var listTitle = "YourListTitle"
    var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";

    var queryPayload = { 
       'query':{ 
           '__metadata': { 'type': 'SP.CamlQuery' },
           'ViewXml': queryViewXml
        } 
    }; 

    $.ajax({
      type: "POST", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
      }, 
      data: JSON.stringify(queryPayload),
      url: endpointUrl, 
      success: function(data) { /* do your success stuff */ },
      failure: function(error) { /* do your error stuff */ } 
   });
}

The code should be self explanatory (I hope) but some notes you should pay attention to:

  1. You need the Request Digest because I am doing a post request!
  2. Make sure that the method name is correct (when you create you ECT you propably made a "Read List" or "Read all Items" Method
  3. Make Sure your Data Source Filter Name ist correct
  4. To make this work, you need to name ALL columns that are return from the ECT method. If not you will get a 500 Error (Internal Server Error)
  5. Follow the request response with Fiddler or in the network cConsole fo your browser!
  6. I used jQuery for the AJAX call

If your follow that it should be fine. Because I generalized the code, I will post the exact same Code with an example I used in my enviroment. This code worked for my situation (it's tested). This is just for comparison. This will not work for you. Unless you have the exact same external list like I have. :-)

    function QueryList()
    {
        var uniqueViewId = "ReadSapMachineData";
        var methodName = "Read Data";
        var taskValue = "102456";
        var query = '<View Name="' + uniqueViewId + '"><Method Name="'+ methodName + '"><Filter Name="SerialNumber" Value="102456" /></Method><Query></Query><ViewFields><FieldRef Name="CHARACT" /><FieldRef Name="VALUE_CHAR" /><FieldRef Name="INHERITED" /><FieldRef Name="INSTANCE" /><FieldRef Name="VALUE_NEUTRAL" /><FieldRef Name="CHARACT_DESCR" /></ViewFields></View>';

        var listTitle = 'T_VALUE_CHAR List'
        var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";

        var queryPayload = { 
           'query':{ 
               '__metadata': { 'type': 'SP.CamlQuery' },
               'ViewXml': query
            } 
        }; 

        $.ajax({
          type: "POST", 
          headers: { 
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
          }, 
          data: JSON.stringify(queryPayload),
          url: endpointUrl, 
          success: function(data) { /* do your success stuff */ },
          failure: function(error) { /* do your error stuff */ } 
       });
    }

UPDATE: I got the idea from the following answere (the "ListViewXml" part) and thought that this could work for this problem.

How to get all items in a view using REST API

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top