0

I'm currently listening to the ItemAdded event using a RER for a DocumentLibrary, this so far works perfectly. However I have an issue where when I try to access the file stream it returns as null. I feel like I have this correct but I also can't seem to find any create documentation on it.

List docs = clientContext.Web.Lists.GetById(listId);
ListItem item = docs.GetItemById(listItemId);
clientContext.Load(item);
clientContext.ExecuteQuery();
var file = item.File.OpenBinaryStream();
2
  • Are you able to debug it? Why don't you use SPFx or PowerApps? Jul 24, 2020 at 7:28
  • @UtkarshDubey I am not using Power Apps because when using OneDrive sync the file uploads/creation is not detected. I have tested Power Apps extensively for my goal in many different ways with no success. At least with a RER I am getting the events to fire properly.
    – Matt
    Jul 24, 2020 at 14:40

1 Answer 1

0

Try the code snippet below, use FileInformation class to get the file stream:

    using (var clientContext = new ClientContext("https://zheguo.sharepoint.com/"))
    {
        clientContext.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);
        Web web = clientContext.Web;
        clientContext.Load(web);
        clientContext.ExecuteQuery();

        List docList = web.Lists.GetByTitle("Documents");
        ListItem docItem = docList.GetItemById(1);
        clientContext.Load(docItem, a => a.File);
        clientContext.ExecuteQuery();
        FileInformation fi = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, docItem.File.ServerRelativeUrl.ToString());
        var fileStream = fi.Stream;
        using (FileStream outputFileStream = new FileStream("C:\\Test.jpg", FileMode.Create))
        {
            fileStream.CopyTo(outputFileStream);
        }

    }

Your Answer

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

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