spFile.OpenBinary() error : file could not be opened

Are you facing this problem while opening the spfile using the url..?
SPFile spFile = web.GetFile("/Document Library Name/" + FileName+ ".jpg");
byte[] binFile = spFile.OpenBinary();  <----- error that file could not be opened
Here is the solution.
Get the SPListItem that contains your file.
Use CAML Query to get the list item by passing the column name and its value.
    SPList list= web.Lists["Document Library Name"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name=\"" + columnName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";
    SPListItemCollection itmColl = SPListItemCollection itmcol = list.GetItems(query);
    SPFile picture=null;
    if (itmcol.Count > 0)
     {
        picture = itmcol[0].File;
        //byte[] binFile = spFile.OpenBinary();
     }
     byte[] fileData = picture.OpenBinary();

Comments

Popular posts from this blog

SharePoint Online (O365) OAuth Authentication | Authorizing REST API calls against SharePoint Online Site | Get Access token from SharePoint Online | Set up OAuth for SharePoint Online Office 365

SharePoint 2013 REST API Reference

Simple Risk Assessment Matrix table with resultant risk calculation

Kendo UI (Core JavaScript) grid Server Paging, Server Sorting and Server Filtering with Dynamic SQL queries

Sharepoint- Using an Image From formatmap32x32.png in a Ribbon Control