Created
October 17, 2019 20:47
-
-
Save Gennady-G/802a2c28cc4d0c01413f6e1462be6638 to your computer and use it in GitHub Desktop.
This file contains 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 copyMyFile() { | |
jQuery().SPServices({ // GET FILE CONTENT FOR COPYING: | |
operation: "GetItem", | |
Url: _spPageContextInfo.siteAbsoluteUrl + "/" + fileUrl, | |
completefunc: function (xData, Status) { | |
console.log("Got file stream and metadata, status: " + Status); | |
if (Status == "success") { | |
itemstream = jQuery(xData.responseXML).find("Stream").text(); | |
itemfields = ""; // document metadata (?) | |
// by the way - this is all logs as "undefined". I also tries to use array | |
jQuery(xData.responseXML).find("FieldInformation").each(function () { | |
itemfields += jQuery(this).get(0).xml; | |
}); | |
var destinationUrl = _spPageContextInfo.siteAbsoluteUrl + "/subsite/Documents/blabla.docx"; | |
jQuery().SPServices({ // COPY FILE | |
operation: "CopyIntoItems", | |
SourceUrl: _spPageContextInfo.siteAbsoluteUrl + "/" + fileUrl, | |
async: false, | |
DestinationUrls: [destinationUrl], | |
Stream: itemstream, | |
Fields: itemfields, // I can comment this, no matter (?) | |
completefunc: function (xData, Status) { | |
console.log("File copied! status: " + Status); | |
if (Status == "success") { | |
// Another call to get parent listitem | |
getListitemFromFileUrl(destinationUrl, | |
function (file) { | |
var addedItem = file.get_listItemAllFields(); | |
console.log("Updating item.."); | |
addedItem.set_item("_CopySource", ""); | |
addedItem.update(); | |
ctx.executeQueryAsync( | |
function () { | |
console.log("New doc details updated"); | |
}, | |
onFail | |
); | |
}, | |
function (sender, args) { | |
console.log(args.get_message()); | |
}); | |
} else { | |
handleSPSerror(xData); | |
} | |
} | |
}); | |
} else { | |
handleSPSerror(xData); | |
} | |
} | |
}); | |
} | |
// Get listItem from file | |
function getListitemFromFileUrl(url, success, error) { | |
var ctx = SP.ClientContext.get_current(); | |
var relUrl = url.replace(_spPageContextInfo.siteAbsoluteUrl, ''); // convert to relative url | |
var file = ctx.get_web().getFileByServerRelativeUrl(relUrl); // get file | |
ctx.load(file, 'ListItemAllFields'); | |
ctx.executeQueryAsync( | |
function () { | |
success(file); | |
}, | |
onFail | |
); | |
} | |
// Try handle error from SPServices | |
function handleSPSerror(xData) { | |
var errorCode = $(xData.responseXML).find('CopyResult').attr('ErrorCode'); | |
if (errorCode == "Unknown") { | |
var errorMessage = $(xData.responseXML).find('CopyResult').attr('ErrorMessage'); | |
console.log("%cRequest returned error: " + errorMessage, "background:red;color:white;"); | |
} | |
} | |
// handle jsom error | |
function onFail(sender, args) { | |
var errorMessage = args.get_message(); | |
var len = args.length, | |
i = 0; | |
for (i; i < len; i++) { | |
errorMessage += "<br/>" + args[i]; | |
} | |
console.log("%cError in sharepoint jsom: " + errorMessage, "background:red;color:white;"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment