TransWikia.com

Listening to ItemAdded File Stream is NUll

SharePoint Asked on December 17, 2021

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();

One Answer

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);
        }

    }

Answered by Jerry on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP