Created
June 11, 2014 15:42
-
-
Save Salvodif/a308a683f18155770483 to your computer and use it in GitHub Desktop.
How to get and save a file from a list to another list in another site by REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getParam ( name ) { | |
if ( name = ( new RegExp ( '[?&]' + encodeURIComponent ( name ) + '=([^&]*)' ) ).exec ( location.search ) ) | |
return decodeURIComponent ( name [ 1 ] ); | |
return ""; | |
} | |
function copySelectedProposal ( selectedProposal, dstParam, succeededCallback, failedCallback ) { | |
if ( selectedProposal == null ) | |
return; | |
var x = { | |
absUrl : _spPageContextInfo.siteAbsoluteUrl, | |
relUrl : _spPageContextInfo.webServerRelativeUrl, | |
dst : dstParam, | |
proposal : selectedProposal | |
}; | |
x.filename = selectedProposal.substring ( selectedProposal.lastIndexOf ( "/" ) + 1 ); | |
x.srcUrl = _.str.sprintf ( "%(absUrl)s/_api/web/getFileByServerRelativeUrl('%(proposal)s')/$value", x ); | |
x.dstUrl = _.str.sprintf ( "%(absUrl)s/%(dst)s/_api/web/getFolderByServerRelativeUrl('%(relUrl)s/%(dst)s/LibUploadedFiles')/Files/add(url='%(filename)s',overwrite=true)", x ); | |
console.debug ( x ); | |
$.getScript ( _spPageContextInfo.scriptbase + "/SP.RequestExecutor.js", function ( ) { | |
var executor = new SP.RequestExecutor ( x.absUrl ); | |
var info = { | |
url : x.srcUrl, | |
method : "GET", | |
binaryStringResponseBody : true, | |
success : function ( data ) { | |
var result = data.body; | |
var destInfo = { | |
url : x.dstUrl, | |
method : "POST", | |
headers : { | |
"Accept" : "application/json; odata=verbose", | |
"X-RequestDigest" : $ ( "#__REQUESTDIGEST" ).val ( ) | |
}, | |
contentType : "application/json;odata=verbose", | |
binaryStringRequestBody : true, | |
body : result, | |
success : function ( bla ) { | |
alert ( "Success! Your file was uploaded to SharePoint." ); | |
}, | |
error : function ( mah ) { | |
alert ( "Oooooops... it looks like something went wrong uploading your file." ); | |
} | |
}; | |
executor.executeAsync ( destInfo ); | |
}, | |
error : function ( err ) { | |
console.debug ( err ); | |
failedCallback ( ); | |
} | |
}; | |
executor.executeAsync ( info ); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment