unable to Attach file for list item using SP.File CopyTo Method

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

February 22nd, 2015 11:41am

Hi

Check the below post

https://social.msdn.microsoft.com/Forums/office/en-US/b6d02ce3-5f6f-47e8-9a93-d11c803d33ce/attachment-upload-for-list-items-using-client-object-model-sharepoint-2010?forum=sharepointgeneralprevious

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 2:02am

Hi SK,

Thank you for your reply!

We are not allowed to use Visual Studio and the same thing we can use in JavaScript, but file stream takes path as one parameter which is physical location of your file system. When you use it some times users might not have access to the file system then our code fails.

Regards,

Mahesh Yamana

February 23rd, 2015 3:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics