2

is there any way to automatically add an attachment stored on the SP server to the new list item? I enter the URL of the file and this file will be stored after adding an item in lists/list_name/attachments/itemID/.

I tried copyTo method, but the problem is, that copyTo can not copy into a non-existent folder and can not create this folder.

    <script type="text/javascript">

var itemId=23;
var nazevPozadavku = "test";
var nazevListu = "Pozadavky"
var zdrojovaSlozka = "files/upload/Pozadavky";
var cilovaSlozka = "https://myWeb_name/Lists/Pozadavky/Attachments/" + itemId + "/BC_" + itemId + "_" + nazevPozadavku + ".xls";


$(document).ready(function () {

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CopyFile);

});

function CopyFile() {
    _ctx = new SP.ClientContext.get_current();    
    this.web = _ctx.get_web();
    alert(this.web);
    _ctx.load(this.web);
    this.sourceList = this.web.get_lists().getByTitle(nazevListu);
    _ctx.load(this.sourceList); 
    this.currentItem = sourceList.getItemById(itemId);
    _ctx.load(this.currentItem);

     var attachmentFolder=this.web.getFolderByServerRelativeUrl(zdrojovaSlozka);          

    _sourceFile= attachmentFolder.get_files();
    _ctx.load(_sourceFile);

    _ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),
                           Function.createDelegate(this, this.onQueryFailed));
}

// Delegate that is called when the query completes successfully.
function onQuerySucceeded(sender, args) {
    if (_sourceFile != null) {

        _destinationlibUrl = cilovaSlozka;      
        _sourceFile.itemAt(0).copyTo(_destinationlibUrl, 1);



        notifyId = SP.UI.Notify.addNotification('Moving file ' + _sourceFile.itemAt(0).get_serverRelativeUrl() + ' to ' + _destinationlibUrl, true);


        _ctx.executeQueryAsync(
        function (sender, args) {
            SP.UI.Notify.removeNotification(notifyId);
            SP.UI.Notify.addNotification('File copied successfully', true);          
        },
        function (sender, args) {
             SP.UI.Notify.addNotification('Error copying file', false);
             SP.UI.Notify.removeNotification(notifyId);
             alert('Error occured: ' + args.get_message());
        }
    );

    }

}

// Delegate that is called when the destination file checkout completes successfully.


// Delegate that is called when server operation is completed with errors.
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

So either I need a script to automatically add an attachment or a script to create a folder in lists/list_name/attachments/.

And I can only use Javascript or SPD 2010, because I don't have permissions for Visual studio or Powershell.

  • I think a SharePoint designer workflow would be great option to your requirement – Shridhar J Joshi May 20 '18 at 8:43
0

You need to some how trap the new item created event. An this is not possible through JavaScript.

The best option you have is to create a custom form and custom save button. On the save button click use JavaScript object model to insert the list item and then use copyTo for creating the attachment.

  • But the problem is, that just new item does not create the folder in attachments. The folder is created only when some file is uploaded. – Ludvik Ctrnacty Nov 16 '14 at 12:57
  • You can't create the folder. The only option is to use List webservice as explained here – Amal Hashim Nov 16 '14 at 13:07

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.