Trying to upload attachment in list Item.?

Hello,

But getting this error:

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.Am I passing wrong parameter in SaveBinaryDirect.

try
            {
                AttachmentCreationInformation at = null;
                foreach (Attachment filename in itemSource.AttachmentFiles)
                {
                    Microsoft.SharePoint.Client.File file = itemSource.ParentList.ParentWeb.GetFileByServerRelativeUrl(filename.ServerRelativeUrl);
                    try
                    {
                        ctxSource.Load(file);
                        ctxSource.ExecuteQuery();
                    }
                    catch (Exception)
                    {
                        
                        //throw;
                    }
                    var imageData = file.OpenBinaryStream();

                    //StreamReader fsReader = new StreamReader(file.OpenBinaryStream());
                    string url = ctxDestination.Url + "Lists/Dokumente1/Attachments/" + lstItem.Id + "/" +file.Name; 
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctxDestination, url, file.OpenBinaryStream().Value, true);
                    //at = new AttachmentCreationInformation { FileName = file.Name, ContentStream = imageData.Value };
                    //lstItem.AttachmentFiles.Add(at);
                }
                lstItem.Update();
                ctxDestination.ExecuteQuery();
            }

August 21st, 2015 2:46am

Hi,

I hope the below attached thread is same thread what you are trying to discuss here.

https://social.msdn.microsoft.com/Forums/sharepoint/en-US/2fcd4172-366b-464a-b18d-0b583e526222/how-to-upload-attachment-in-sharepoint-list-item?forum=sharepointdevelopment

So you can discuss in the existing thread itself. 

Moderators, please merge these two into one.

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 3:02am

Yes, Can you please check the above code what mistake am doing.
August 21st, 2015 3:06am

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

Other recent topics Other recent topics