module Shockout { export class SpApi { param { string } term param { Function } callback param { number = public static peopleSearch ( term , callback , take = 10 ) { var filter = "startswith(Name,'{0}')" . replace ( /\{0\}/g , term ) ; var select = null ; var orderby = "Name" ; var top = 10 ; var cache = true ; SpApi . getListItems ( 'UserInformationList' , fn , '/' , filter , select , orderby , top , cache ) ; function fn ( data , error ) { if ( ! ! error ) { callback ( null , error ) ; return ; } callback ( data , error ) ; } ; } param term param maxResults param callback public static searchUsers ( term , callback , select = 'Id,Account,Name,WorkEmail' , maxResults = 10 ) { var url = `/_vti_bin/listdata.svc/UserInformationList?
            $filter=startswith(WorkEmail, '${ term }') or startswith(Name, '${ term }') or startswith(LastName, '${ term }') and ContentType eq 'Person'
            &$orderby=Name
            &$top=${ maxResults }` ; SpApi . executeRestRequest ( url , fn , true , 'GET' ) ; function fn ( data , error ) { if ( ! ! error ) { callback ( data , error ) ; return ; } if ( ! ! data ) { if ( data . d ) { callback ( data . d ) ; } else { callback ( data ) ; } } } ; } param { number } id param { Function } callback public static getPersonById ( id , callback ) { if ( isNaN ( parseInt ( id + '' ) ) ) { return ; } var url = '/_vti_bin/listdata.svc/UserInformationList(' + id + ')' ; SpApi . executeRestRequest ( url , fn , true , 'GET' ) ; function fn ( data , error ) { if ( ! ! error ) { callback ( data , error ) ; return ; } if ( ! ! data ) { if ( data . d ) { callback ( data . d ) ; } else { callback ( data ) ; } } } ; } param { string } url param { JQueryPromiseCallback < any > } callback param { boolean = false param { string = public static executeRestRequest ( url , callback , cache = false , type = 'GET' ) { var $jqXhr = $ . ajax ( { url : url , type : type , cache : cache , dataType : 'json' , contentType : 'application/json; charset=utf-8' , headers : { 'Accept' : 'application/json;odata=verbose' } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { callback ( data ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { if ( ! ! status && parseInt ( status ) == 404 ) { var msg = status + ". The data may have been deleted by another user." ; } else { msg = status + ' ' + error ; } callback ( null , msg ) ; } ) ; } param { string } listName param { number } itemId param { Function } callback param { string = param { boolean = false param { string = null public static getListItem ( listName , itemId , callback , siteUrl = '/' , cache = false , expand = null ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; var url = siteUrl + '_vti_bin/listdata.svc/' + Utils . toCamelCase ( listName ) + '(' + itemId + ')?$expand=CreatedBy,ModifiedBy' + ( ! ! expand ? ',' + expand : '' ) ; SpApi . executeRestRequest ( url , fn , cache , 'GET' ) ; function fn ( data , error ) { if ( ! ! error ) { callback ( data , error ) ; return ; } if ( ! ! data ) { if ( data . d ) { callback ( data . d ) ; } else { callback ( data ) ; } } } ; } param { string } listName param { Function } callback param { string = param { string = null param { string = null param { string = null param { number = param { boolean = false public static getListItems ( listName , callback , siteUrl = '/' , filter = null , select = null , orderby = null , top = 10 , cache = false ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; var url = [ siteUrl + '_vti_bin/listdata.svc/' + Utils . toCamelCase ( listName ) ] ; if ( ! ! filter ) { url . push ( '$filter=' + filter ) ; } if ( ! ! select ) { url . push ( '$select=' + select ) ; } if ( ! ! orderby ) { url . push ( '$orderby=' + orderby ) ; } url . push ( '$top=' + top ) ; SpApi . executeRestRequest ( url . join ( '&' ) . replace ( /\&/ , '\?' ) , fn , cache , 'GET' ) ; function fn ( data , error ) { var data = ! ! data && 'd' in data ? data . d : data ; var results = null ; if ( ! ! data ) { results = 'results' in data ? data . results : data ; } callback ( results , error ) ; } ; } param { string } url param { Function } callback param { any = undefined public static insertListItem ( url , callback , data = undefined ) { var $jqXhr = $ . ajax ( { url : url , type : 'POST' , processData : false , contentType : 'application/json' , data : ! ! data ? JSON . stringify ( data ) : null , headers : { 'Accept' : 'application/json;odata=verbose' } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { callback ( data ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { callback ( null , status + ': ' + error ) ; } ) ; } param { ISpItem } item param { Function } callback param { any = undefined public static updateListItem ( item , callback , data = undefined ) { var $jqXhr = $ . ajax ( { url : item . __metadata . uri , type : 'POST' , processData : false , contentType : 'application/json' , data : data ? JSON . stringify ( data ) : null , beforeSend : function ( xhr ) { xhr . setRequestHeader ( 'X-HTTP-Method' , 'MERGE' ) ; xhr . setRequestHeader ( 'If-Match' , item . __metadata . etag ) ; } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { callback ( data ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { callback ( null , status + ': ' + error ) ; } ) ; } param { ISpItem } item param { JQueryPromiseCallback < any > } callback public static deleteListItem ( item , callback ) { var $jqXhr = $ . ajax ( { url : item . __metadata . uri , type : 'POST' , headers : { 'Accept' : 'application/json;odata=verbose' , 'X-Http-Method' : 'DELETE' , 'If-Match' : item . __metadata . etag } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) : void { callback ( data ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) : void { callback ( null , error ) ; } ) ; } param { ISpAttachment } att param { Function } callback public static deleteAttachment ( att , callback = undefined ) { const $jqXhr = $ . ajax ( { url : att . __metadata . uri , type : 'POST' , dataType : 'json' , contentType : "application/json" , headers : { 'Accept' : 'application/json;odata=verbose' , 'X-HTTP-Method' : 'DELETE' } , success : function ( data , status , jqXhr ) { if ( callback ) { callback ( { data : data , status : status , jqXhr : jqXhr } ) ; } } , error : function ( jqXhr , status , error ) { if ( callback ) { callback ( { status : status , jqXhr : jqXhr , error : error } ) ; } console . warn ( error ) ; } } ) ; return $jqXhr ; } } }