How do I get the filesize from the Microsoft.SharePoint.Client.File object?
Load the File_x0020_Size
field information to get it.
This is what I do when I want to list all the files in a Sharepoint 2010 folder:
//folderPath is something like /yoursite/yourlist/yourfolder
Microsoft.SharePoint.Client.Folder spFolder = _ctx.Web.GetFolderByServerRelativeUrl(folderPath);
_ctx.Load(spFolder);
_ctx.ExecuteQuery();
FileCollection fileCol = spFolder.Files;
_ctx.Load(fileCol);
_ctx.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File spFile in fileCol)
{
//In here, specify all the fields you want retrieved, including the file size one...
_ctx.Load(spFile, file => file.Author, file => file.TimeLastModified, file=>file.TimeCreated,
file => file.Name, file => file.ServerRelativeUrl, file => file.ListItemAllFields["File_x0020_Size"]);
_ctx.ExecuteQuery();
int fileSize = int.Parse((string)spFile.ListItemAllFields["File_x0020_Size"]);
}
_ctx
is obviously the ClientContext
you have initiated.
Here's an extended list of all Sharepoint internal fields
user834964
Updated on June 27, 2022Comments
-
user834964 almost 2 years
I'm looking for a good way to get filesize from the
Microsoft.SharePoint.Client.File
object.The
Client
object does not have aLength
member.I tried this:
foreach (SP.File file in files) { string path = file.Path; path = path.Substring(this.getTeamSiteUrl().Length); FileInformation fileInformation = SP.File.OpenBinaryDirect(this.Context, path); using (MemoryStream memoryStream = new MemoryStream()) { CopyStream(fileInformation.Stream, memoryStream); file.Size = memoryStream.Length; } }
Which gave me a length through using the
MemoryStream
, but it's not good for performance. This file also does not belong to a document library. Since it's an attached file, I can't convert it to aListItem
object usingListItemAllFields
. If I could convert it to aListItem
, I could get its size using:ListItem["File_x0020_Size"]
How do I get the filesize of the
Client
object in SharePoint using C#? -
user834964 over 12 yearsThank you for sending a reply. I tried fileInformation.Stream.Length to get stream length. but,this property throws System.NotSupportedException. Code which copies a stream from such a reason. mmm...A solution is not found by me yet. regards
-
Steve Drake about 8 yearsI know this is and old Q, but this will not preform very well. SP2013 has file.length but in 2010 you can do what is suggested above.