2

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.

1 Answer 1

2

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
         });
     }
 });
}
3
  • Very nice! "CAML tools" ? Is there a way to implement this in javascript/jquery without the executequeryasync? I've only used CAML in regards to a program that had me write something in a similar format to what you've posted, so I'm not sure how to call it programmatically with js. Apr 2, 2014 at 22:05
  • I was using the BIWUG CAMLDesigner to generate that markup. It could be used in a SPServices jQuery function to fetch data, edited with example usage. Apr 2, 2014 at 22:11
  • Great. Thank you very much. I am not sure why this method is not preferred, but it works nicely for me. All others constantly implied that the only way was to use the ctx.ExecuteQueryAsync, so I'm glad CAML works just fine for what I need to do. Apr 3, 2014 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.