0

I have the following code to attach a file to a list item:-

   context.Load(context.Site);
   context.ExecuteQuery();
   Microsoft.SharePoint.Client.File fs = context.Site.RootWeb.GetFileByServerRelativeUrl(context.Site.ServerRelativeUrl + "/RiskAssessmentTemplate/Risk%20Assesment.xlsx");
   context.Load(fs);
   context.ExecuteQuery();
   var attInfo = new AttachmentCreationInformation();
   attInfo.FileName = fs.Name;
   var data = fs.OpenBinaryStream();
   context.Load(fs);
   context.ExecuteQuery();
   using (MemoryStream ms = new MemoryStream())
                            {
                            data.Value.CopyTo(ms);
                            attInfo.ContentStream = ms;
                            var att = item.AttachmentFiles.Add(attInfo);
                            context.Load(att);
                            context.ExecuteQuery();
    
                           }

now if i try to open the attachment which get attached using the above code i will get this error:-

enter image description here

while if i manually attach the same file i can open the attachment without any issue.. any advice please?

1 Answer 1

1

Hi SharePoint TestDev,

I tested your code in my SPO environment and am able to reproduce this error:

Open in desktop app:

enter image description here

Open in browser:

enter image description here

I Fixed it using below code:

Could you please have a try at your end?

BR

1
  • ok worked well..
    – John John
    Aug 19, 2020 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.