OfficeDev / Office-365-SDK-for-Android

Microsoft Services SDKs for Android produced by MS Open Tech.
https://dev.office.com/android
Other
224 stars 74 forks source link

Filter parameters not applied properly in ListClient queries #61

Closed TBag closed 9 years ago

TBag commented 9 years ago

The SDK generates incorrect URLs when filters are applied to a SharePoint list query. The following code demonstrates this issue.

public int getInspectionPhotoId(int incidentId,int roomId, int inspectionId,int top) throws ExecutionException, InterruptedException {
        String[] select = {"Id"};
        Query query = new Query().select(select)
                .parameter("sl_inspectionIDId",String.valueOf(inspectionId))
                .parameter("sl_incidentIDId",String.valueOf(incidentId))
                .parameter("sl_roomIDId",String.valueOf(roomId))
                .top(top)
                .orderBy("Modified", QueryOrder.Descending);
        List<SPListItem> items = mClient.getListItems(Constants.LIST_NAME_ROOMINSPECTIONPHOTOS, query).get();
        if(items != null && items.size() > 0){
            return items.get(0).getId();
        }

        return 0;
    }

The SDK generates this URL:

https://teeudev3.sharepoint.com/sites/SuiteLevelAppDemo/_api/web/lists/GetByTitle('Room%20Inspection%20Photos')/Items?$filter=&$top=1&$orderby=Modified+desc&sl_InspectionIDId=40&sl_incidentIDId=49&sl_roomIDId=1&$select=Id 

The problem is the filter parameters do not follow the $filter= parameter.

The correct URL should look like this:

https://teeudev3.sharepoint.com/sites/SuiteLevelAppDemo/_api/web/lists/GetByTitle('Room%20Inspection%20Photos')/Items?$filter= sl_inspectionIDId%20eq%2040%20and%20sl_incidentIDId%20eq%2049%20and%20sl_roomIDId%20eq%201&$top=1&$orderby=Modified+desc&$select=Id
marcote commented 9 years ago

Hi @TBag . I'm currently OOF, but I'll be back next Monday. Regards

TBag commented 9 years ago

OK, thanks for the heads up.

marcote commented 9 years ago

@TBag sorry about the delay, had to recover from a small surgery first.

I tried to reproduce the defect and if you want to use filter, you shouldn't use parameter. Use instead:

Query query = new Query().select(select)
.field("sl_inspectionIDId").eq(inspectionId)
.field("sl_incidentIDId").eq(incidentId)
.field("sl_roomIDId").eq(roomId)
.top(top)
.orderBy("Modified", QueryOrder.Descending);

and the result is the following one:

https://somehost.sharepoint.com/test/_api/web/lists/GetByTitle('somelistname')/Items?$filter=sl_inspectionIDId+eq+(10)+sl_incidentIDId+eq+(20)+sl_roomIDId+eq+(30)&$top=5&$orderby=Modified+desc&$select=Id

Let me know if this works for you. Thanks

TBag commented 9 years ago

Hi Marcos,

That code you suggested works well. Thanks.