4

I am able to traverse through my entire Picture Lib, however when I am trying to place into a byte array I am getting an it shows that the value of the OpenBinaryStream(); is empty. My is to go through the Picture Library and get the image and turn it into a base 64 string. I should be able to do it by simply placing those bytes into a byte array and then converting it to base 64 but the value is empty.

try
{
  var files = context.Web.GetFolderByServerRelativeUrl(uri).Files;
  context.Load(files);
  context.ExecuteQuery();

  foreach (var file in files)  << I am traversing my pic lib
  {
    var test = file.OpenBinaryStream(); << when testing the value it is empty
  }
}
catch (Exception exp)
{
            var buf = 0;
}
9

Felt my way around this, I found that context.Load(file) , context.ExecuteQuery(), was needed to actually load the file, and then I need to proceed into putting it in a stream which I later turned into a byte array which I was able to convert into base 64.

public static void GetImage(ClientContext context, string uri)
{
 try
 {
   var files = context.Web.GetFolderByServerRelativeUrl(uri).Files;
   context.Load(files);
   context.ExecuteQuery();
   foreach (Microsoft.SharePoint.Client.File file in files)
   {
      ClientResult<System.IO.Stream> data = file.OpenBinaryStream();
      context.Load(file);
      context.ExecuteQuery();
      using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
      {
        if (data != null)
        {
          data.Value.CopyTo(mStream);
          byte [] imageArray = mStream.ToArray();
          string b64String = Convert.ToBase64String(imageArray);                          
        }
      }
    }
  }
  catch (Exception exp)
  {

  }
}
  • Having same issue using PowerShell, but this answer doesn't work for me. – Randeep Singh Jul 20 '17 at 4:30

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.