So I have been trying for the last day or so to get code right to upload a file to a newly created document library based off a custom template. Based on my research I know I am getting all of the information correctly and sending the information correctly.

If the Document Library has not been created then I first create the library from a custom list template:

REST.prototype.CreateList = function (station, description) {
            var dfd = $.Deferred();
            var itms = [];
            this.GetListTemplates().then(function (data) {
                itms = data.data.d.results;
                // console.log(itms);
            }).then(function () {
                var ctx = SP.ClientContext.get_current();
                var web = ctx.get_web();
                for (var i = 0; i < itms.length; i++) {
                    // console.log(itms[i].Name);
                    if (itms[i].Name == "Station Library") {
                        var lci = new SP.ListCreationInformation();
                        var list = web.get_lists().add(lci);
                        ctx.executeQueryAsync(function () {
                            // console.log("Created: "+list.get_title());
                        }, function (sender, args) {
                            // console.log(args.get_message());
            return dfd.promise();

I don't know if the fact that the templated Document library only takes a content type based off of a document matters or not. I have wondered if I needed to get the content type first and upload the file as that content type( don't know if that's possible)

To get the file I am using this code:

FileManipulation.prototype.GetFileBuffer = function (fileInputId) {
            var dfd = $.Deferred();
            var elmnt = $("#" + fileInputId);
            var reader = new FileReader();
            reader.onload = function (e) {
            reader.onerror = function (e) {
            return dfd.promise();

After that I use the following code to add file to library

REST.prototype.AddFileToFolder = function (station, fileName, buffer) {
            var service = this;
            var _url = service.hostWebUrl + "/_api/web/lists/getByTitle(@TargetLibrary)/rootfolder/files/add(overwrite=true,url=@TargetFileName)?@TargetLibrary='" + station + "'&@TargetFileName='" + fileName + "'";
            return $.ajax({
                url: _url,
                method: "POST",
                data: buffer,
                processData: false,
                headers: {
                    "Accept": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val()

And this is where is get this error 500 (Internal Server Error) and also this message:

"{"error":{"code":"-2130575312, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The URL '<station>/<fileName>' is invalid.  It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web."}}}"

Your Answer

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

Browse other questions tagged or ask your own question.