module Shockout { export class SpApi15 { param { Function } callback param { boolean = false returns public static getCurrentUser ( callback , expandGroups = true , siteUrl = '' ) { let url = Utils . formatSubsiteUrl ( siteUrl ) ; let $jqXhr = $ . ajax ( { url : url + '_api/Web/CurrentUser' + ( expandGroups ? '?$expand=Groups' : '' ) , type : 'GET' , cache : true , dataType : 'json' , contentType : 'application/json; charset=utf-8' , headers : { 'Accept' : 'application/json;odata=verbose' } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { let user = data . d ; let account = user . LoginName . replace ( /^i\:0\#\.w\|/ , '' ) ; let currentUser = < ICurrentUser > { account : user . Id + ';#' + user . Title , department : null , email : user . Email , groups : [ ] , id : user . Id , jobtitle : null , login : account , title : user . Title } ; if ( expandGroups ) { let groups = data . d . Groups ; $ ( groups . results ) . each ( function ( i , group ) { var groupName = group . LoginName || group . Title ; currentUser . groups . push ( { id : group . Id , name : groupName } ) ; } ) ; } if ( ! ! callback ) { callback ( currentUser ) ; } } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { if ( ! ! callback ) { callback ( null , jqXhr . status ) ; } } ) ; return $jqXhr ; } param { number } userId param { JQueryPromiseCallback < any > } callback returns public static getUsersGroups ( userId , callback = undefined , siteUrl = '' ) { let url = `${ Utils . formatSubsiteUrl ( siteUrl ) }_api/Web/GetUserById(${ userId })/Groups` ; let $jqXhr = $ . ajax ( { url : url , type : 'GET' , cache : true , dataType : 'json' , contentType : 'application/json; charset=utf-8' , headers : { 'Accept' : 'application/json;odata=verbose' } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { let groups = [ ] ; for ( var i = 0 ; i < data . d . results . length ; i ++ ) { let group = data . d . results [ i ] ; groups . push ( { id : group . Id , name : group . Title } ) ; } if ( ! ! callback ) { callback ( groups ) ; } } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { if ( ! ! callback ) { callback ( null , error ) ; } } ) ; return $jqXhr ; } param siteUrl param from param to param body param subject returns public static SendEmail ( siteUrl , from , to , body , subject ) { let methodUrl = Utils . formatSubsiteUrl ( siteUrl ) + "_api/SP.Utilities.Utility.SendEmail" ; let data = { properties : { __metadata : { type : 'SP.Utilities.EmailProperties' } , From : from , To : { results : [ to ] } , Body : body , Subject : subject } } ; return SpApi15 . Post ( siteUrl , methodUrl , data ) ; } param siteUrl param methodUrl param data returns public static Post ( siteUrl , methodUrl , data , headers = undefined ) { const deferred = $ . Deferred ( ) , self = this ; const json = ! ! data ? JSON . stringify ( data ) : null ; SpApi15 . GetFormDigest ( siteUrl ) . then ( function ( digest ) { let hdrs = { 'Accept' : 'application/json;odata=verbose' , 'Content-Type' : 'application/json;odata=verbose' , 'X-RequestDigest' : digest . d . GetContextWebInformation . FormDigestValue , 'X-HTTP-Method' : 'POST' } ; if ( headers ) { for ( var p in headers ) { hdrs [ p ] = headers [ p ] ; } } $ . ajax ( { contentType : 'application/json;odata=verbose' , url : methodUrl , type : 'POST' , data : json , headers : hdrs , success : function ( data , status , jqXhr ) { if ( ! ! data ) { deferred . resolve ( data . d || data ) ; } else { deferred . resolve ( { status : status , jqXhr : jqXhr } ) ; } } , error : function ( jqXhr , status , error ) { deferred . reject ( { jqXhr : jqXhr , status : status , error : error } ) ; console . warn ( error ) ; } } ) ; } ) ; return deferred . promise ( ) ; } public static Get ( url , cache = false ) { const deferred = $ . Deferred ( ) , self = this ; let $jqXhr = $ . ajax ( { url : url , type : 'GET' , cache : cache , dataType : 'json' , contentType : 'application/json; charset=utf-8' , headers : { 'Content-Type' : 'application/json;odata=verbose' , 'Accept' : 'application/json;odata=verbose' } } ) ; $jqXhr . done ( function ( data , status , jqXhr ) { if ( ! ! data ) { deferred . resolve ( data . d || data ) ; } else { deferred . resolve ( { status : status , jqXhr : jqXhr } ) ; } } ) ; $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 ; } deferred . reject ( msg ) ; } ) ; return deferred . promise ( ) ; } public static GetListItem ( listName , itemId , siteUrl = '/' , cache = false , expand = undefined ) { const methodUrl = `${ Utils . formatSubsiteUrl ( siteUrl ) }_api/web/lists/GetByTitle('${ listName }')/items(${ itemId })${ ( ! ! expand ? '?$expand=' + expand : '' ) }` ; return SpApi15 . Get ( methodUrl , cache ) ; } param siteUrl returns public static GetFormDigest ( siteUrl ) { var opts = { 'url' : Utils . formatSubsiteUrl ( siteUrl ) + '_api/contextinfo' , 'method' : 'POST' , 'headers' : { 'Accept' : 'application/json;odata=verbose' } } ; return $ . ajax ( opts ) ; } param siteUrl param listName param data param itemId returns public static AddListItem ( siteUrl , listName , listItemType , data ) { const methodUrl = `${ Utils . formatSubsiteUrl ( siteUrl ) }_api/web/lists/GetByTitle('${ listName }')/items` ; let payload = { __metadata : { type : formatListItemType ( listItemType ) } } ; for ( const p in data ) { let o = data [ p ] ; if ( o == null || o == undefined ) { payload [ p ] = null ; } else if ( o . constructor === Array ) { payload [ p ] = getSpRestArrayTemplate ( o ) ; } else { payload [ p ] = o ; } } return SpApi15 . Post ( siteUrl , methodUrl , payload ) ; function getSpRestArrayTemplate ( array ) { return { "__metadata" : { "type" : "Collection(Edm.String)" } , "results" : array } ; } function formatListItemType ( type ) { let t = type . replace ( /\s/g , '_x0020_' ) ; let ptrn = `SP.Data.${ t }ListItem` ; let rx = new RegExp ( ptrn ) ; if ( rx . test ( t ) ) { return t ; } return ptrn ; } } public static UpdateListItem ( siteUrl , metadata , data ) { const methodUrl = metadata . uri ; const deferred = $ . Deferred ( ) , self = this ; let payload = { __metadata : { type : metadata . type } } ; for ( const p in data ) { let o = data [ p ] ; if ( o == null || o == undefined ) { payload [ p ] = null ; } else if ( o . constructor === Array ) { payload [ p ] = getSpRestArrayTemplate ( o ) ; } else { payload [ p ] = o ; } } const json = JSON . stringify ( payload ) ; SpApi15 . GetFormDigest ( siteUrl ) . then ( function ( digest ) { let hdrs = { 'Accept' : 'application/json;odata=verbose' , 'X-RequestDigest' : digest . d . GetContextWebInformation . FormDigestValue , 'X-HTTP-Method' : 'MERGE' , 'If-Match' : '*' } ; $ . ajax ( { contentType : 'application/json;odata=verbose' , url : methodUrl , type : 'POST' , data : json , headers : hdrs , success : function ( data , status , xhr ) { if ( ! ! data ) { deferred . resolve ( data . d || data ) ; } else { deferred . resolve ( { success : true } ) ; } } , error : function ( xhr , status , error ) { deferred . reject ( { xhr : xhr , status : status , error : error } ) ; console . warn ( error ) ; } } ) ; } ) ; return deferred . promise ( ) ; function getSpRestArrayTemplate ( array ) { return { "__metadata" : { "type" : "Collection(Edm.String)" } , "results" : array } ; } } public static GetUserById ( siteUrl , userId ) { let url = `${ Utils . formatSubsiteUrl ( siteUrl ) }_vti_bin/ListData.svc/UserInformationList(${ userId })` ; return SpApi15 . Get ( url , false ) ; } param { ISpAttachment } att param { Function } callback public static DeleteAttachment ( siteUrl , att , callback = undefined ) { const deferred = $ . Deferred ( ) , self = this ; SpApi15 . GetFormDigest ( siteUrl ) . then ( function ( digest ) { let hdrs = { 'Accept' : 'application/json;odata=verbose' , 'X-RequestDigest' : digest . d . GetContextWebInformation . FormDigestValue , 'X-HTTP-Method' : 'DELETE' , } ; $ . ajax ( { contentType : 'application/json;odata=verbose' , url : att . __metadata . uri , type : 'POST' , headers : hdrs , success : function ( data , status , xhr ) { if ( ! ! data ) { deferred . resolve ( data . d || data ) ; } else { deferred . resolve ( { success : true } ) ; } } , error : function ( xhr , status , error ) { deferred . reject ( { xhr : xhr , status : status , error : error } ) ; console . warn ( error ) ; } } ) ; } ) ; return deferred . promise ( ) ; } public static GetGenericPersonPng ( ) { return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHkAAAB4CAYAAADWpl3sAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAkmSURBVHhe7Z33bxNLEMc3oXcIvTeBaEIEJJqQQIi/ml8AAYpoEj0gmui995r3PivPk19EbOf2bm/Gno90SuLYvvV8d8ru3a77hoaGRp4+fRomT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