1

I am using Microsoft Graph API to retrieve the user profile image and I want to save the retrieved image as an attachment in the list item. So I am using following code.

var response = await httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/photo/$value");
var responsImage = response.Content.ReadAsStringAsync().Result;

Here, in responseImage I am able to get image content type. So I tried saving the image to my local drive using following code:

using (Stream responseStream = responsImage)
{
   using (FileStream fs = new FileStream(@"D:\image.jpg", FileMode.Create))
   {
       await responseStream.CopyToAsync(fs);
   }
}

After saving it to my local drive for testing the image, it gives me the proper image.

enter image description here

Now, I am trying to save the same image as an attachment as I am having an image in Stream object.

For saving image as an attachment I am using the code from here.

It saves the content as an attachment.

enter image description here

But when I open the attachment, it shows me the blank image instead of the correct image. As shown below:

enter image description here

I am not able to figure it out while saving its giving perfect image while using the same stream it saves the blank image.

What I am doing wrong?.

2

I have used this in PowrShell Scripts

May be it will help you

foreach($item in $Items)
{
   if($item.Attachments -ne $null)
 {


   try
   {
    foreach($attachment in $item.Attachments)
     {
      Write-Host "Downloading Attachement: " $attachment  -ForegroundColor Green
      $file= $w.GetFile($item.Attachments.UrlPrefix + $attachment)
      $bytes = $file.OpenBinary()
      $path = $destinationFolder + "\" + $attachment
      Write-Host ""
      Write "Saving to the loction  $path"
      $fs = New-Object System.IO.FileStream($path, "OpenOrCreate")
      $fs.Write($bytes,0,$bytes.Length)
      $fs.Close()

    } 
   }
   Catch
   {
    Write-Host " Error" -BackgroundColor DarkMagenta
   }
 }

}

Updated Answer

`

             List docs = web.Lists.GetByTitle("CustomList");
               ListItem item = docs.GetItemById(ID);
           var attInfo = new AttachmentCreationInformation();
        attInfo.FileName = mFile.Name;
        attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes("streamFile"));

        Attachment att = item.AttachmentFiles.Add(attInfo); //Add to File

        context.Load(att);
        context.ExecuteQuery();
        Console.WriteLine("done");`
  • Thanks for the suggestion, but I do not want any powershell script. I will keep as an alternative option. – Trimantra Software Solution May 29 '17 at 5:04
  • I have used CSOM in PowerShell script. You have to change the valuable deceleration only – Akshay Dattatray Nangare May 29 '17 at 5:16
  • The answer you provided is for saving image on local drive from list item attachment, But i want to save image as an attachment to the list item. – Trimantra Software Solution May 29 '17 at 6:37
  • Check updated answer – Akshay Dattatray Nangare May 29 '17 at 7:26
2

There 2 ways you can add attachments to list items.

Option 1 - .AttachmentFiles.Add(attachmentInfo) as below using FileStream:

var list = context.Web.Lists.GetByTitle("CustomList");
var listItem = list.GetItemById(7);
var path = @"C:\abcd.jpg";
var attachmentInfo = new AttachmentCreationInformation();
attachmentInfo.FileName = System.IO.Path.GetFileName(path);
using (var fs = new FileStream(path, FileMode.Open))
{
    attachmentInfo.ContentStream = fs;
    var attachment = listItem.AttachmentFiles.Add(attachmentInfo);
    context.ExecuteQuery();                    
}

Option 2 - using SaveBinaryDirect method. For that, you need to have ServerRelativeUrl of your SharePoint list. Try below code:

var list = context.Web.Lists.GetByTitle("CustomList");                
var attachFilePath = @"C:\abcd.jpg";
var listItem = list.GetItemById(4);
context.Load(listItem);
context.ExecuteQuery();
using (System.IO.FileStream fileStream = new System.IO.FileInfo(attachFilePath).Open(System.IO.FileMode.Open))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/sites/GautamTestSite/Lists/CustomList/Attachments/" + listItem.Id + "/" + System.IO.Path.GetFileName(attachFilePath), fileStream, true);
}

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.