I have to implement file copying, in some case i need to copy file using CSOM (for. ex. /_layouts/15/images/itgen.png - default small list icon) into my library. What i have tried is:

  • get by SP.Web.getFileByServerRelativeUrl()
  • XMLHttpRequest/jQuery.get - but IE 9 does not support Blobs.

Is there any another way to do that? Thanks for any help!


You can still copy it old school way: Read file content!

    getFile: function (filePath) {
    var deferred = jQuery.Deferred();
    var req = jQuery.ajax({
        url: filePath,
        type: "GET",
        cache: false
    }).done(function (fileContents) {
        if (fileContents !== undefined && fileContents.length > 0) {
        } else {
            deferred.reject('Failed to read file from web..');

    }).fail(function (jqXHR, textStatus) {
        deferred.reject("Request for web file failed: " + textStatus);

    return deferred.promise();
uploadFileToHost: function (filePath, destinationPath, fileName) {
    var deferred = jQuery.Deferred();
        function (contents) {
            var context = file.context
            var createInfo = new SP.FileCreationInformation();
            createInfo.set_content(new SP.Base64EncodedByteArray());
            for (var i = 0; i < contents.length; i++) {

            var files = context.get_web().getFolderByServerRelativeUrl(destinationPath).get_files();

                function () {
                function (sender, args) {
        function (err) {

    deferred.done(function () {
        Helper.logMessage("Uploaded to Host : " + file.destinationFileName);
    deferred.fail(function (error) {
        Helper.logMessage("Failed to uploaded to Host : '" + file.destinationFileName + "'. Error : " + error);

    return deferred.promise();
| improve this answer | |
  • thanks a lot for this solution! we had to support IE9 (I wrote about it above). My solution is pretty close to yours, but i use Image element to load image and then Canvas for drawind image on it and Canvas.toDataURL() method, which IE 9 supports (oh, at least something :)). – Romko Mar 24 '15 at 15:27

For those searching for the answer to the TITLE of this post, no, you cannot get an SPFile (or client equivalent SP.File) that represents a file in any virtual directory (/_layouts, /_images, /_vti_bin, etc).

The reason being is that an SPFile is tied to an SPListItem, which lives in an SPList. All of these objects live in the content database (witchcraft takes place for ghosted files, but they still live in a library).

| improve this answer | |

here are some samples:

! CSOM move the files one folder another folder

http://www.postseek.com/meta/a6c1a29023b78254f93e543ca769e5b0 http://zimmergren.net/technical/sp-2010-uploading-files-using-the-client-om-in-sharepoint-2010

| improve this answer | |
  • 4
    Please don't post link-only answers, instead add substantial part of the posts/blogs in your answer. – Arsalan Adam Khatri Mar 23 '15 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.