Requirement: I have a document library and list, the requirement is i need to take a document from document library and attach to list item in the list using CSOM only.
Followed technique: to achieve this requirement i followed below methodology
clientContext = SP.ClientContext.get_current();
webSite = clientContext.get_web();
webSite.AllowUnsafeUpdates =true;
var list = webSite.get_lists().getByTitle('Documents');
item = list.getItemById(3);
file = item.get_file();
file.checkOut('Comments',1);
file.copyTo("xxxxxxx/Lists/Student/Attachments/4/mahesh.docx","1");
file.checkIn('Comments',1);
clientContext.load(file );
clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
Issue: This functionality is absolutely working fine when list item contains at least one attachment, if list item doesn't have any attachments the code is failing. i tried web.AllowUnsafeUpdates also but not working.
Could anyone please help me on this.... :(
Thanks in Advance!
Regards,
Mahesh Yamana