0

I have designed a Form for Announcement list, I want to write a code on the Save button, which could save a file attached (browse/fileupload button) by user in the form (check screenshot). I want to save this attached file in a specific Library.

I am designing this in Sharepoint Designer 2010. How can I write a simple c# code for the same, I do not find code behind this form.

enter image description here

0

There is no code behind file associated with the custom form created using the designer. You should write JavaScript client object model code to perform the task. It can be written inside the designer. Here is a sample code to upload file to library:

"use strict";

var WingtipToys = window.WingtipToys || {};
WingtipToys.Jsom = WingtipToys.Jsom || {};

WingtipToys.Jsom.Libs = function () {

    var deferreds = new Array(),

    upload = function (serverRelativeUrl, filename, file) {
        deferreds[deferreds.length] = $.Deferred();

        getFileBuffer(file).then(
            function (buffer) {
                var bytes = new Uint8Array(buffer);
                var content = new SP.Base64EncodedByteArray(); //base64 encoding
                for (var b = 0; b < bytes.length; b++) {
                    content.append(bytes[b]);
                }
                var ctx = new SP.ClientContext.get_current();
                var createInfo = new SP.FileCreationInformation();
                createInfo.set_content(content); //setting the content of the new file
                createInfo.set_overwrite(true);
                createInfo.set_url(filename);
                this.files = ctx.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
                ctx.load(this.files);
                this.files.add(createInfo);
                ctx.executeQueryAsync(
                    Function.createDelegate(this,
                        function () { deferreds[deferreds.length - 1].resolve(this.files); }),
                    Function.createDelegate(this,
                        function (sender, args) { deferreds[deferreds.length - 1].reject(sender, args); }));

            },
            function (err) {
                deferreds[deferreds.length - 1].reject(err);
            }
         );

        return deferreds[deferreds.length - 1].promise();

    },

    getFileBuffer = function (file) {
        //See previous code
    };

    return {
        upload: upload,
    };

}();

Source: http://www.shillier.com/archive/2013/03/26/uploading-files-in-sharepoint-2013-using-csom-and-rest.aspx

When a Save button is clicked in a SharePoint form the OOB JavaScript function PreSaveAction is called. This can be overridden to add support for uploading the file. See here for more information about it: http://sharepointwings.blogspot.in/2012/06/javascript-presaveaction-in-sharepoint.html

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.