Sharepoint REST API filter empty LookUp field

怎甘沉沦 提交于 2019-12-08 03:45:15

问题


I'd like to know how I can filter ListItems in SharePoint rest API (2013) on empty lookup. On my list items, there is a lookup (single value, not required) and i'd like to get all items where lookup is empty (blank).

_api/web/lists/getbytitle('MyList')/items?$select=Id&$expand=MyLookUp/Id&$filter=??

Does someone has a clue ?

Thx


回答1:


REST API in SharePoint 2013 does Not support null values for filtering on list item queries.

However you can combine CAML query with REST API to get the required data.

Please refer below code : (Note : please change your query/URL):

 function _rest_Post(rquest) {
        return $.ajax({
            method: rquest.method,
            url: rquest.url,
            contentType: rquest.contentType,
            headers: rquest.header,
            data: JSON.stringify(rquest.body),
        });

    }

function GetByCaml(serviceParams) {
    var req = {
        method: 'POST',
        url: url + "/_api/web/lists/getByTitle('" + serviceParams.create.lName + "')/getitems",
        header: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
        },
        body: serviceParams.create.body,
        contentType: "application/json;odata=verbose",

    };
    return _rest_Post(req);

}

function getData(){
    var queryViewXml = "<View><Query><Where><IsNull><FieldRef Name='Project_x0020_Manager'/></IsNull></Where></Query><ViewFields><FieldRef Name='Title'/></ViewFields></View>";
    var params = {};
    params.create= {};
    params.create.lName = "MyList";
    params.create.filter = "";
    params.create.body = {
        'query':{ 
            '__metadata': { 'type': 'SP.CamlQuery' },
            'ViewXml': queryViewXml
        }
    }
    return GetByCaml(params);
};


getData().then(function(data){
    //success handler
}, function(error){
    //failure handler
});



回答2:


OP, your answer is really close. The Id value for empty lookups should be -1. If you explore the raw data with something like SP CAML Query Helper you should see -1;# in the unselected lookups.

/items/?$select=Id,Title,Assigned_x0020_To/Id&$expand=Assigned_x0020_To&$filter=(Assigned_x0020_To eq -1) should work for you.




回答3:


I just found a solution that does work using REST, you simply filter the lookup ID to greater than 0. All lookup IDs are 1 or greater, but using not equal to 0 (MyLookup ne 0) fails, because null is not equal to 0. However, null is, apparently, not GREATER THAN 0, either. It might make the math geeks twitch, but it works.

_api/web/lists/getbytitle('abc')/items?$select=Id&$expand=MyLookUp/Id&$filter=MyLookup gt 0


来源:https://stackoverflow.com/questions/40959179/sharepoint-rest-api-filter-empty-lookup-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!