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.