SP File API

by dshilkret

JavaScript

RESTApiHelper.Items = function(SiteUrl,ListName){
    this.SiteUrl = SiteUrl;
    this.ListName = ListName;
    
    this.GetAllItems = function (doSuccess, doError) {
   	 jQuery.ajax({
   		 url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/items",
   		 type: "GET",
   		 headers: { "Accept": "application/json; odata=verbose" },
   		 success: doSuccess,
   		 error: doError
   	 });
    }

    this.GetItems = function (select, filter, doSuccess, doError) {
   	 var urlfilter = "";
   	 if (select != '') {
   		 urlfilter = "?$select=" + select;
   	 }
   	 if (filter != '') {
   		 if (urlfilter != '')
   			 urlfilter = urlfilter + "&$filter=" + filter;
   		 else
   			 urlfilter = "?$filter=" + filter;
   	 }

   	 jQuery.ajax({
   		 url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/items" + urlfilter,
   		 type: "GET",
   		 headers: { "Accept": "application/json; odata=verbose" },
   		 success: doSuccess,
   		 error: doError
   	 });
    }

    this.GetItemsByCaml = function (ViewXML, OnSuccess, OnError) {
   	 var serviceUrl = this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/getitems";

   	 var metadata = JSON.stringify({ 'query': { '__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml': ViewXML } });

   	 jQuery.ajax({
   		 url: serviceUrl,
   		 type: "POST",
   		 data: metadata,
   		 headers: {
   			 "X-RequestDigest": $("#__REQUESTDIGEST").val(),
   			 "Accept": "application/json; odata=verbose",
   			 "content-type": "application/json; odata=verbose",
   			 "content-length": metadata.length
   		 },
   		 success: OnSuccess,
   		 error: OnError
   	 });
    }

    this.GetItemByID = function (ItemId, doSuccess, doError) {
   	 jQuery.ajax({
   		 url: this.SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/getitembyid(" + ItemId + ")",
   		 type: "GET",
   		 headers: { "Accept": "application/json; odata=verbose" },
   		 success: doSuccess,
   		 error:...