SharePoint REST API : List Items CRUD and Namespaces

The SharePoint REST API was Since 2013 introduced and offers Endpoints for almost all needed Operations on all levels(Sitecollection, Sites, Lists, Fields, Permissions, Document, Items…etc.)

In this article I am going to describe how to manage List Items(CReateUpdateDelete) using the REST API, AJAX…etc.

Endpoints

REST Webservices are based on 2 things :

URL

URLs are based on concatenation, on SharePoint we have

/_api/web/Lists/GetByTitle(‘Title’)/Items

Here it is clear what it is going on : we are trying to acces the items of the list with title “Title” under the current Web.

Methode

The Web Method tells which action we want to execute. The same Endpoint could make different changes depending on the Method :

POST /_api/web/Lists/GetByTitle(‘Test’)/Items

Will Create a new Item on the List Test

 

 

GET /_api/Web/Lists/GetByTitle(‘Test’)/Items

Will return the List Items

 

PATCH /_api/web/Lists/GetByTitle(‘Test’)/Items(1)

Updates the Item with ID 1

 

DELETE /_api/web/Lists/GetByTitle(‘Test’)/Items(1)

Deletes the Item with ID 1

 

List Items CRUD

The complete JS File can be downloaded here

You can also find a JS Sample which uses Namespaces, I tried to implement a proper way to load and manage items easily : here

Load List Items

function GetSPListItems(listName)
{
var siteurl = _spPageContextInfo.webAbsoluteUrl;

var req = $.ajax({
url: siteurl + “/_api/web/lists/getbytitle(‘” + listName + “‘)/items”,
method: “GET”,
headers: { “Accept”: “application/json; odata=verbose” }
});

return req;
}

Load List Items with filtering

REST API still offer the possiblity to use CAML Queries, I find building XML Queries sometimes very complicated, that’s why i propose to use $filter.

$filter=Title eq ‘My custom title’

Sample :

 

Update List Item

In order to update a list item, we need a body request in addition of the Endpoint and the Webmethod.

The Request Body is a Json composed from:

  • Type : which is always SP.Data.{YourListTitle}ListITem
    • PS : If you once changed the List title, you should use the original name
    • Spaces and special chars are encoded
  • Values : which is a key:value with fields Internalnames

With my sp_crud.js you need just to pass the itemID, list’s title and an array of {Key, Value} where key is the internal name and value is the value.

In all write operations of the REST api it is required to send the Digest Value

“X-RequestDigest”: $(“#__REQUESTDIGEST”).val()

 

 

Sample :

Entities

I have a SharePoint List called “Varianten”, on this list simple Data are stored(Title and ID) and would be used to populate a dropdown box.

We have a Namespace “Varianten”, which has an Entity Manager that includes an observale array(KnockoutJS) and a method “LoadVarianten”.

And now it is quire easy to load variante from SharePoint. In my sanple i load the list items and bind them to an autocomplete field.

var varianteManager = new Varianten.VariantenManager();
varianteManager.LoadVarianten().then(
function(){…..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s