开发者

SharePoint Client Object Model (COM) File Version Information

开发者 https://www.devze.com 2023-01-14 12:55 出处:网络
I am trying to get some basic file version information using the new SharePoint Client Object Model (COM) with SharePoint 2010. I have successfully loaded and queried for my ListItem, File, and FileVe

I am trying to get some basic file version information using the new SharePoint Client Object Model (COM) with SharePoint 2010. I have successfully loaded and queried for my ListItem, File, and FileVersionCollection like this:

using (ClientContext context = new ClientContext(site)) {
   context.Load(context.Web);
   List docs = context.Web.Lists.GetByTitle("Docs");
   context.Load(docs);
   //query that returns the ListItems I want
   CamlQuery query = new CamlQuery { ViewXml = ".."};

   ListItemCollection docItems = docs.GetItems(query);
   context.Load(docItems);
   context.ExecuteQuery();

   //load the FileVersionCollection
   foreach (ListItem listItem in docItems) {
      context.Load(listItem);
      context.Load(listItem.File);
      context.Load(listItem.File.Versions);
   }
   context.ExecuteQuery();

At this point, I can iterate through the listItem.File.Versions collection and get the VersionLabel and Url. However, I need to get the number of bytes of the version and the FileVersion object is lacking a Size or Length property.

I decided I could just read the version off the server and throw away the bytes (not efficient, I know, but it should work) like so:

foreach (FileVersion version in item.File.Versions) {
   FileInformation info = File.OpenBinaryDirect(context, version.Url);

   long filesize = 0;

   Stream stream = info.Stream;
   byte[] buffer = new byte[4096];
   int read = 0;
   while ((read = stream.Read(buffer, 0, 4096)) > 0) {
      filesize += read;
   }

   //use the filesize
}

But every time I execute File.OpenBinaryDirect I get this error:

Specified argument was out of the range of valid values.
Parameter name: serverRelativeUrl

If I take the value of version.Url and put it into my browser, the file opens.

Any suggestions on how to get the file size? I would prefer not to open an HTTP stream and read the file, but if it comes to that, then I will.

BTW, I tri开发者_运维知识库ed creating a new tag sharepoint-com but I don't have enough reputation. If someone with enough points thinks that tag is worthwhile, please create it :)


SPFile.Length Gets the size of the file in bytes, excluding the size of any Web Parts that are used in the file.


Apparently you cannot access content of previous versions by File.OpenBinaryDirect. You can use WebClient to download it via HTTP/S directly instead.

Web web = ...;
FileVersion version = ...;
using (var input = new WebClient() { UseDefaultCredentials = true }) {
    string url = web.Url + "/" + version.Url;
    byte[] content = input.DownloadData(url);
}

See this forum thread about it.

0

精彩评论

暂无评论...
验证码 换一张
取 消