module Shockout { export class SpSoap { param { Function } callback public static getCurrentUser ( callback ) { var user = < ICurrentUser > { } ; var query = '<Query><Where><Eq><FieldRef Name="ID" /><Value Type="Counter"><UserID /></Value></Eq></Where></Query>' ; var viewFields = '<ViewFields><FieldRef Name="ID" /><FieldRef Name="Name" /><FieldRef Name="EMail" /><FieldRef Name="Department" /><FieldRef Name="JobTitle" /><FieldRef Name="UserName" /><FieldRef Name="Office" /></ViewFields>' ; SpSoap . getListItems ( '' , 'User Information List' , viewFields , query , function ( xmlDoc , status , jqXhr ) { $ ( xmlDoc ) . find ( '*' ) . filter ( function ( ) { return Utils . isZrow ( this ) ; } ) . each ( function ( i , node ) { user . id = parseInt ( $ ( node ) . attr ( 'ows_ID' ) ) ; user . title = $ ( node ) . attr ( 'ows_Title' ) ; user . login = $ ( node ) . attr ( 'ows_Name' ) ; user . email = $ ( node ) . attr ( 'ows_EMail' ) ; user . jobtitle = $ ( node ) . attr ( 'ows_JobTitle' ) ; user . department = $ ( node ) . attr ( 'ows_Department' ) ; user . account = user . id + ';#' + user . title ; user . groups = [ ] ; } ) ; callback ( user ) ; } ) ; } param { string } loginName param { Function } callback public static getUsersGroups ( loginName , callback , siteUrl = '' ) { var packet = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<GetGroupCollectionFromUser xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">' + '<userLoginName>' + loginName + '</userLoginName>' + '</GetGroupCollectionFromUser>' + '</soap:Body>' + '</soap:Envelope>' ; var $jqXhr = $ . ajax ( { url : Utils . formatSubsiteUrl ( siteUrl ) + '_vti_bin/usergroup.asmx' , type : 'POST' , dataType : 'xml' , data : packet , contentType : 'text/xml; charset="utf-8"' } ) ; $jqXhr . done ( cb ) ; $jqXhr . fail ( cb ) ; function cb ( xmlDoc , status , jqXhr ) { var $errorText = $ ( xmlDoc ) . find ( 'errorstring' ) ; if ( ! ! $errorText && $errorText . text ( ) != "" ) { callback ( null , $errorText . text ( ) ) ; return ; } var groups = [ ] ; $ ( xmlDoc ) . find ( "Group" ) . each ( function ( i , el ) { groups . push ( { id : parseInt ( $ ( el ) . attr ( "ID" ) ) , name : $ ( el ) . attr ( "Name" ) } ) ; } ) ; callback ( groups ) ; } } param { string } siteUrl param { string } listName param { string } viewFields param { string } query param { Function } callback param { number = public static getListItems ( siteUrl , listName , viewFields , query , callback , rowLimit = 25 ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; if ( ! ! ! listName ) { Utils . logError ( "Parameter `listName` is null or undefined in method SpSoap.getListItems()" , SPForm . errorLogListName , SPForm . errorLogSiteUrl ) ; } var packet = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">' + '<listName>' + listName + '</listName>' + '<query>' + query + '</query>' + '<viewFields>' + viewFields + '</viewFields>' + '<rowLimit>' + rowLimit + '</rowLimit>' + '</GetListItems>' + '</soap:Body>' + '</soap:Envelope>' ; var $jqXhr = $ . ajax ( { url : siteUrl + '_vti_bin/lists.asmx' , type : 'POST' , dataType : 'xml' , data : packet , headers : { "SOAPAction" : "http://schemas.microsoft.com/sharepoint/soap/GetListItems" , "Content-Type" : "text/xml; charset=utf-8" } } ) ; $jqXhr . done ( function ( xmlDoc , status , error ) { callback ( xmlDoc ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { callback ( null , status + ': ' + error ) ; } ) ; } param { string } siteUrl param { string } listName param { Function } callback public static getList ( siteUrl , listName , callback ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; var packet = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/"><listName>{0}</listName></GetList></soap:Body></soap:Envelope>' . replace ( '{0}' , listName ) ; var $jqXhr = $ . ajax ( { url : siteUrl + '_vti_bin/lists.asmx' , type : 'POST' , cache : false , dataType : "xml" , data : packet , headers : { "SOAPAction" : "http://schemas.microsoft.com/sharepoint/soap/GetList" , "Content-Type" : "text/xml; charset=utf-8" } } ) ; $jqXhr . done ( function ( xmlDoc , status , jqXhr ) { callback ( xmlDoc ) ; } ) ; $jqXhr . fail ( function ( jqXhr , status , error ) { callback ( null , status + ': ' + error ) ; } ) ; } param { string } pageUrl param { string } checkinType param { Function } callback param { string = returns public static checkInFile ( pageUrl , checkinType , callback , comment = '' ) { var action = 'http://schemas.microsoft.com/sharepoint/soap/CheckInFile' ; var params = [ pageUrl , comment , checkinType ] ; var packet = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><CheckInFile xmlns="http://schemas.microsoft.com/sharepoint/soap/"><pageUrl>{0}</pageUrl><comment>{1}</comment><CheckinType>{2}</CheckinType></CheckInFile></soap:Body></soap:Envelope>' ; return this . executeSoapRequest ( action , packet , params , null , callback ) ; } param { string } pageUrl param { string } checkoutToLocal param { string } lastmodified param { Function } callback returns public static checkOutFile ( pageUrl , checkoutToLocal , lastmodified , callback ) { var action = 'http://schemas.microsoft.com/sharepoint/soap/CheckOutFile' ; var params = [ pageUrl , checkoutToLocal , lastmodified ] ; var packet = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><CheckOutFile xmlns="http://schemas.microsoft.com/sharepoint/soap/"><pageUrl>{0}</pageUrl><checkoutToLocal>{1}</checkoutToLocal><lastmodified>{2}</lastmodified></CheckOutFile></soap:Body></soap:Envelope>' ; return this . executeSoapRequest ( action , packet , params , null , callback ) ; } param { string } action param { string } packet param { Array < any > } params param { string = param { Function = undefined param { string = public static executeSoapRequest ( action , packet , params , siteUrl = '/' , callback = undefined , service = 'lists.asmx' ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; try { var serviceUrl = siteUrl + '_vti_bin/' + service ; if ( params != null ) { for ( var i = 0 ; i < params . length ; i ++ ) { packet = packet . replace ( '{' + i + '}' , ( params [ i ] == null ? '' : params [ i ] ) ) ; } } var $jqXhr = $ . ajax ( { url : serviceUrl , cache : false , type : 'POST' , data : packet , headers : { 'Content-Type' : 'text/xml; charset=utf-8' , 'SOAPAction' : action } } ) ; if ( callback ) { $jqXhr . done ( < JQueryPromiseCallback < any > > callback ) ; } $jqXhr . fail ( function ( jqXhr , status , error ) { var msg = 'Error in SpSoap.executeSoapRequest. ' + status + ': ' + error + ' ' ; if ( SPForm . enableErrorLog && ! SPForm . DEBUG ) { Utils . logError ( msg , SPForm . errorLogListName ) ; } if ( callback ) { callback ( jqXhr , status , error ) ; } } ) ; } catch ( e ) { Utils . logError ( 'Error in SpSoap.executeSoapRequest.' , JSON . stringify ( e ) , SPForm . errorLogListName ) ; console . warn ( e ) ; } } param { number } itemId param { string } listName param { Array < Array < any > > } fields param { boolean = true param { string = param { Function = undefined public static updateListItem ( itemId , listName , fields , isNew = true , siteUrl = '/' , callback = undefined ) { var action = 'http://schemas.microsoft.com/sharepoint/soap/UpdateListItems' ; var packet = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">' + '<listName>{0}</listName>' + '<updates>{1}</updates>' + '</UpdateListItems>' + '</soap:Body>' + '</soap:Envelope>' ; var command = isNew ? "New" : "Update" ; var params = [ listName ] ; var soapEnvelope = "<Batch OnError='Continue'><Method ID='1' Cmd='" + command + "'>" ; var itemArray = fields ; for ( var i = 0 ; i < fields . length ; i ++ ) { soapEnvelope += "<Field Name='" + fields [ i ] [ 0 ] + "'>" + Utils . escapeColumnValue ( fields [ i ] [ 1 ] ) + "</Field>" ; } if ( command !== "New" ) { soapEnvelope += "<Field Name='ID'>" + itemId + "</Field>" ; } soapEnvelope += "</Method></Batch>" ; params . push ( soapEnvelope ) ; SpSoap . executeSoapRequest ( action , packet , params , siteUrl , callback ) ; } param { string } term param { Function } callback param { number = param { string = public static searchPrincipals ( term , callback , maxResults = 10 , principalType = 'User' ) { var action = 'http://schemas.microsoft.com/sharepoint/soap/SearchPrincipals' ; var params = [ term , maxResults , principalType ] ; var packet = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<SearchPrincipals xmlns="http://schemas.microsoft.com/sharepoint/soap/">' + '<searchText>{0}</searchText>' + '<maxResults>{1}</maxResults>' + '<principalType>{2}</principalType>' + '</SearchPrincipals>' + '</soap:Body>' + '</soap:Envelope>' ; SpSoap . executeSoapRequest ( action , packet , params , '/' , cb , 'People.asmx' ) ; function cb ( xmlDoc , status , jqXhr ) { var results = [ ] ; $ ( xmlDoc ) . find ( 'PrincipalInfo' ) . each ( ( i , n ) : void => { var id = parseInt ( $ ( 'UserInfoID' , n ) . text ( ) ) ; if ( id > 0 ) { var account = $ ( 'AccountName' , n ) . text ( ) . replace ( /^i\:0\#\.w\|/ , '' ) ; results . push ( { AccountName : account , UserInfoID : id , DisplayName : $ ( 'DisplayName' , n ) . text ( ) , Email : $ ( 'Email' , n ) . text ( ) , Title : $ ( 'Title' , n ) . text ( ) , IsResolved : $ ( 'IsResolved' , n ) . text ( ) == 'true' ? ! 0 : ! 1 , PrincipalType : $ ( 'PrincipalType' , n ) . text ( ) } ) ; } } ) ; callback ( results ) ; } } param base64Data param fileName param listName param listItemId param siteUrl param callback public static addAttachment = function ( base64Data , fileName , listName , listItemId , siteUrl , callback ) { var strData = base64Data . indexOf ( ',' ) > - 1 ? base64Data . split ( ',' ) [ 1 ] : base64Data ; var action = 'http://schemas.microsoft.com/sharepoint/soap/AddAttachment' ; var packet = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<AddAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/">' + '<listName>{0}</listName>' + '<listItemID>{1}</listItemID>' + '<fileName>{2}</fileName>' + '<attachment>{3}</attachment>' + '</AddAttachment>' + '</soap:Body>' + '</soap:Envelope>' ; this . executeSoapRequest ( action , packet , [ listName , listItemId , fileName , strData ] , siteUrl , callback , 'lists.asmx' ) ; } ; } }