0

I need to upload a file to a new list item in Sharepoint Online from a clientapp. The list exists allready, but I create new list items and tried to upload file as attachements. I tried it over CSOM, but failed because the attachment folder for this item doesnt exist. It'll be created only if you manually upload something...

I think the same error happens if I try to upload the file over SOAP.

Upload to an existing list item with attachements is no problem at all.

Does anybody know how to solve this issue? Is there a workaround maybe?

2

Below proof of concept, create item and add attachment via client object model.

var list = Context.Web.Lists.GetById(listId);
Context.Load(list);
Context.ExecuteQuery();

var item = list.AddItem(new ListItemCreationInformation());
item["Title"] = "New Item with attachment";
item.Update()
context.ExecuteQuery();

var attInfo           = new AttachmentCreationInformation();
attInfo.FileName      = "Attachment name";
attInfo.ContentStream = new MemoryStream(attachmentData);

var att = item.AttachmentFiles.Add(attInfo);
Context.Load(att);
Context.ExecuteQuery();
0
                public static List<string> UploadDocuments<T>(ClientContext ctx,IList<HttpPostedFile> selectedFiles, T reqObj, int itemID)
                        {
                            List<Attachment> existingFiles = null;
                            List<string> processedFiles = null;
                            List<string> unProcessedFiles = null;
                            ListItem item = null;
                            FileStream sr = null;
                            AttachmentCollection attachments = null;
                            byte[] contents = null;
                            try
                            {
                                existingFiles = new List<Attachment>();
                                processedFiles = new List<string>();
                                unProcessedFiles = new List<string>();
            //Get the existing item
                                item = ctx.Web.Lists.GetByTitle(typeof(T).Name).GetItemById(itemID);
        //get the Existing attached attachments
                                attachments = item.AttachmentFiles;
                                ctx.Load(attachments);
                                ctx.ExecuteQuery();
        //adding into the new List
                                foreach (Attachment att in attachments)
                                    existingFiles.Add(att);
        //For each Files which user has selected
                                foreach (HttpPostedFile postedFile in selectedFiles)
                                {
                                    string fileName = Path.GetFileName(postedFile.FileName);
    //If selected file not exist in existing item attachment
                                    if (!existingFiles.Any(x => x.FileName == fileName))
                                    {
//Added to Process List
                                        processedFiles.Add(postedFile.FileName);
                                    }
                                    else
                                        unProcessedFiles.Add(fileName);
                                }
//Foreach process item add it as an attachment
                                foreach (string path in processedFiles)
                                {
                                    sr = new FileStream(path, FileMode.Open);
                                    contents = new byte[sr.Length];
                                    sr.Read(contents, 0, (int)sr.Length);
                                    var attInfo = new AttachmentCreationInformation();
                                    attInfo.FileName = Path.GetFileName(path);
                                    attInfo.ContentStream = sr;
                                    item.AttachmentFiles.Add(attInfo);
                                    item.Update();
                                }
                                ctx.ExecuteQuery();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                            finally
                            {
                                existingFiles = null;
                                processedFiles = null;
                                item = null;
                                sr = null;
                                attachments = null;
                                contents = null;
                                ctx = null;

                            }
                            return unProcessedFiles;
                        }

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.