1 minute read

OData Requests

The REST API allows for OData requests to be made to each request. This will allow us to expand collections of the target object, to help reduce the number of requests made to the server.

Query Properties

  • Custom: string
    • Appended to the querystring
  • Expand: Array<string>
    • The properties to expand
  • Filter: string
    • The filter
  • GetAllItems: boolean
    • Flag to get all items (False by default)
    • Use this flag to get past the 5k limit
    • The result is still limited to the threshold defined for the web application
  • OrderBy: Array<string>
    • An array of properties to order by
    • Add “ desc” to the property, to reverse the order
  • Select: Array<string>
    • An array of properties to be selected
  • Skip: number
    • The number of results to skip
  • Top: number
    • The max number of results to return
    • 5000 is the max limit
    • Setting the “GetAllItems” flag will get past the 5k limit

Code Examples

JavaScript

Reference the library

var $REST = require("gd-sprest");

Get the web, expanding the field, lists and root folder

// Get the current web
$REST.Web()
    // Expand the fields, lists and root folder properties
    .query({
        Expand: ["Fields", "Lists", "RootFolder"]
    })
    // Execute the request
    .execute(function(web) {
        var fields = web.Fields.results;
        var lists = web.Lists.results;
        var rootFolder = web.RootFolder;
    });

Query for picture libraries, including their items

// Get the current web
$REST.Web()
    // Get the lists
    .Lists()
    // Query for the picture libraries, including the items
    .query({
        Expand: ["Items"],
        Filter: "BaseTemplate eq 109"
    })
    // Execute the request
    .execute(function(lists) {
        // Parse the lists
        for(var i=0; i<lists.results.length; i++;) {
            var list = lists.results[i];
            var items = list.Items.results;

            // Code goes here
        }
    });

TypeScript

Reference the library

import { Web } from "gd-sprest";

Get the web, expanding the field, lists and root folder

// Get the current web
Web()
    // Expand the fields, lists and root folder properties
    .query({
        Expand: ["Fields", "Lists", "RootFolder"]
    })
    // Execute the request
    .execute(web => {
        let fields = web.Fields.results;
        let lists = web.Lists.results;
        let rootFolder = web.RootFolder;
    });

Query for picture libraries, including their items

// Get the current web
Web()
    // Get the lists
    .Lists()
    // Query for the picture libraries, including the items
    .query({
        Expand: ["Items"],
        Filter: "BaseTemplate eq 109"
    })
    // Execute the request
    .execute(lists => {
        // Parse the lists
        for(let i=0; i<lists.results.length; i++;) {
            let list = lists.results[i];
            let items = list.Items.results;

            // Code goes here
        }
    });