none
How to check for undefined field in SharePoint/REST/OData RRS feed

  • Question

  • I'm writing Javascript pages querying lists via OData GETs. 

    In some cases a field is not filled in and the field is missing from the data.d.results record. I want to query for such cases but there doesn't seem to be an OData way to do it. If there were a "fieldexists('fieldName')" that would do it for me.

    My idea for a hack/workaround is a real stinker: query all records, iterate through them, check for the existence of the field with data.d.results[x].hasOwnProperty('fieldName'), slice the array item out if it exists and decrement length. Please tell me I don't have to do this.


    Larry J Seltzer larry@larryseltzer.com @lseltzer

    Monday, February 27, 2017 7:22 PM

All replies

  • Hi,

    You could create custom function to check whether the field exists, for example:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script type="text/javascript">
            function myFunction() {
                var listTitle = $('#txt_List').val();
                var fieldTitle = $('#txt_Field').val();
                var check = IsFieldExist(listTitle, fieldTitle);
                alert(check);
            }
            function IsFieldExist(listTitle, FieldName) {
                var result;
                var requestUri = _spPageContextInfo.webAbsoluteUrl +
                        "/_api/web/lists/getByTitle('" + listTitle + "')/fields/getByTitle('" + FieldName + "')";
    
                // execute AJAX request
                $.ajax({
                    url: requestUri,
                    type: "GET",
                    async: false,
                    headers: { "ACCEPT": "application/json;odata=verbose" },
                    success: function (data) {
                        //console.log(data);                    
                        result=true;
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        if (jqXHR.status=='400') {
                            result=false;
                        }
                        //console.log(textStatus);
                    }
                });
                return result;
            }
        </script>
        <input type="button" onclick="myFunction()" value="click" title="click" />
        <input id="txt_List" type="text" />
        <input id="txt_Field" type="text" />

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, February 28, 2017 2:52 AM
  • Thank you Lee, but I'm not sure this actually helps. The field exists in some records and not in others. I want an OData query to return the records where it does not exist. 

    Larry J Seltzer larry@larryseltzer.com @lseltzer

    Tuesday, February 28, 2017 1:18 PM
  • Hi,

    You may check the function hasField(fieldName) provided by jsontableschema-js would help.

    https://github.com/frictionlessdata/jsontableschema-js

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 1, 2017 7:09 AM
  • Thank you, but that's only going to work after the query and is basically the same as data.d.results[x].hasOwnProperty('fieldName'), I want to be able to query only the records I need. I'm pretty sure now it can't be done. 

    Larry J Seltzer larry@larryseltzer.com @lseltzer

    Wednesday, March 1, 2017 1:23 PM
  • Hi,

    Filtering data/list items by a filed which not exists doesn’t make sense, so you have to process after querying.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 2, 2017 1:29 AM