Question

With JavaScript, is it possible to get its version number if I know the ID of an item?

I have a function that needs to do something with the selected file, but even if I select the file, it is retrieving its regular value __.docx, so this doesn't help me.

I'm hoping I can perform a caml query in SPServices or somehow (the ctx.ExecuteQueryAsync option was ruled out by my leader) to just pop the ID.

The information below is just additional info about what I need it for in case anyone is interested.


Older versions of files are stored as 512 * whole number value of version + decimal, so if I try to perform an action on the selected file that has a version number of 1.x where x > 0, I'm not actually doing anything with that specific file.

Basically, if the version of file Hey.docx is 2.2, I need to calculate (512*2)+1= such that: https://mysite.com/sitecol/Library/Hey.docx <-- most recent file https://mysite.com/sitecol/_vti_history/1026/Library/Hey.docx <-- file I need.

Was it helpful?

Solution

Using some CAML tools pointed at a local library with versioning enabled, I was able to get back the field ows__UIVersionString of a file. It correctly shows the version is 9.0 which is reflected in the UI.

<Where>
      <Eq>
         <FieldRef Name='ID' />
         <Value Type='Counter'>1</Value>
      </Eq>
   </Where>
   <ViewFields>
      <FieldRef Name='_UIVersionString' />
   </ViewFields>

Example usage using SPServices:

function GetVersionHistory(id){
$().SPServices({
     operation: "GetListItems",
     async: false,
     listName: "My List Name",
     CAMLViewFields: "<ViewFields><FieldRef Name='_UIVersionString' /></ViewFields>",
     CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+id+"</Value></Eq></Where></Query>",
     completefunc: function (xData, Status) {
         $(xData.responseXML).SPFilterNode("z:row").each(function() {
             // do something
         });
     }
 });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top