RestoreVersion does not seem to work

Mar 27, 2014 at 6:40 PM
Try as I might, I have not successfully been able to get the RestoreVersion to work. My GetListItems works perfectly, and returns everyhting as expected, it just does not seem to pass the info to the RestoreVersion (I do have the proper information on the document name with it's relative path, and the version wholenumber). I have tried using the RestoreVersion from within my GetListItems complete function and also by calling it from within the complete function, but niether end up doing anything after the rest of my complete function works as expected, and cycles through all items in my called list(as I can verify with my alerts), any insight would be greatful?
when It does try to pass access the other service I did get an error showing in the Explorer dev box, saying the following:

Object expected jquery.SPServices.js, line 1206 character 6
which was this line: opt.completefunc(cachedXML, status); // When the call is complete, do this
//----------------------Function to restore document first version
function RestoreOriginalVersion(){
var LibName=$("#Library").val(); //these values come from input boxes
var viewGUID=$("#vGUID").val(); //these values come from input boxes
var docVersion=$("#dVersion").val(); //these values come from input boxes
alert("Library: "+LibName);
alert("GUID: "+viewGUID);
alert("Version: "+docVersion);
    $().SPServices({
        operation: "GetListItems",
        webURL: "http://johnsandbox.moss.bns",
        async: false,
        listName: LibName,
        viewName: viewGUID,
        CAMLViewFields: "<ViewFields><FieldRef Name='BaseName' /><FieldRef Name='_UIVersionString' /></ViewFields>",
        CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='Recursive'/></QueryOptions>",
        cacheXML: true,
        completefunc: function (xData, Status) {
                                  $(xData.responseXML).SPFilterNode("z:row").each(function() { 
               var docName = $(this).attr("ows_BaseName");
               var docCurrentVersion = $(this).attr("ows__UIVersionString");
                                       RelativeDocName = ("\/"+LibName+"\/"+docName);
                                       alert ("RelativeDocName:"+RelativeDocName);
               //DoRestore(RelativeDocName,docVersion);
                                      alert("Document: "+ docName + "  Current Version: " + docCurrentVersion);
            $().SPServices({
                    operation: "RestoreVersion",
                    fileName:RelativeDocName,
                    fileVersion:docVersion
            }); //------ end of RestoreVersion SPServices function
           }); //---------closing of Data response function
        } //----------closing of complete function
     }); //------ end of capturing Client folder data and closing of SPServices function
} //----end of RestoreOriginalVersion
Mar 28, 2014 at 11:14 AM
I had something come to me last night as I slept. My problem may lie with not having the extension on the end of the filename, as the error shown in the IE dev tool was that it not have an object. I am going back and look into that aspect, and see if I can pull that information in my get list items before passing it to the RestoreVersion method, and let you know my findings.
Mar 28, 2014 at 11:43 AM
Edited Mar 28, 2014 at 11:45 AM
My assumption was correct once I changed the field in the getListItemitems from BaseName to LinkFilename and also the ows_BaseName to ows_LinkFilename, all worked as expected. Hopefully you will find this useful if trying to get the RestoreVersion to work.
Mar 28, 2014 at 1:14 PM
One other thing I also did in my code was use the ServerUrl and ows_ServerUrl to get my relative path, without having to build it as I was previosuly. I still get the orginal error in SPservices however( Object expected jquery.SPServices.js, line 1206 character 6 , which was this line: opt.completefunc(cachedXML, status); // When the call is complete, do this) and it may mean I still need to add somehting to my RestoreVersion method options to stop this, but the method does act on my call to it and restores the version. I might just add a commented line to a complete function, in the RestoreVerions method, to try and alleviate this one error.
Mar 28, 2014 at 2:23 PM
I now have it working with no errors. By placing a complete function in the RestoreVersion method it stopped the SPservices error I was getting. I just commented out the action I used to verify it fixes the error and left in the complete Function in the RestoreVersion method. I also call the Method from within the GetListItems.

Here is my finshed workign code, including my CEWP elements and button that enacts the whole thing:
<script language="javascript" type="text/javascript" src="/SiteAssets/jquery.js"></script>
<script language="javascript" type="text/javascript" src="/SiteAssets/jquery.SPServices.js"></script>
<script defer type="text/javascript"> 
$(document).ready(function(){
var RelativeDocName="";
//alert("jquery and SPservices loaded");
//--------------------Enable or disable Restore button --------------------------
$("#Library").change(enableRestoreBttn);
$("#vGUID").change(enableRestoreBttn);
$("#dVersion").change(enableRestoreBttn);
    if ($("#Library").val()=="" || $("#vGUID").val()=="" || $("#dVersion").val()==""){
        $("#RestoreBttn").attr('disabled', true);
    }
    if ($("#Library").val()!="" && $("#vGUID").val()!="" && $("#dVersion").val()!=""){
        $("#RestoreBttn").attr('disabled', false);
    }
    function enableRestoreBttn(){
        if ($("#Library").val()=="" || $("#vGUID").val()=="" || $("#dVersion").val()==""){
        $("#RestoreBttn").attr('disabled', true);
        } else if ($("#Library").val()!="" && $("#vGUID").val()!="" && $("#dVersion").val()!=""){
        $("#RestoreBttn").attr('disabled', false);
        }
    }
$("#RestoreBttn").click(RestoreOriginalVersion);
//----------------------Function to restore document first version------------------------
          function RestoreOriginalVersion(){
    var LibName=$("#Library").val();
    var viewGUID=$("#vGUID").val();
    var docVersion=$("#dVersion").val();
    //alert("Library: "+LibName);
    //alert("GUID: "+viewGUID);
    //alert("Version: "+docVersion);
           $().SPServices({
            operation: "GetListItems",
            webURL: "http://johnsandbox.moss.bns",
            async: false,
            listName: LibName,
            viewName: viewGUID,
            CAMLViewFields: "<ViewFields><FieldRef Name='LinkFilename' /><FieldRef Name='_UIVersionString' /><FieldRef Name='ServerUrl' /></ViewFields>",
            CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='Recursive'/></QueryOptions>",
            cacheXML: true,
            completefunc: function (xData, Status) {
                                  $(xData.responseXML).SPFilterNode("z:row").each(function() { 
               var docName = $(this).attr("ows_LinkFilename");
               var docCurrentVersion = $(this).attr("ows__UIVersionString");
               RelativeDocName = ("http:\/\/johnsandbox.moss.bns"+$(this).attr("ows_ServerUrl"));
                                   //alert("RelativeDocName: "+RelativeDocName);
               DoRestore(RelativeDocName,docVersion);
              }); //---------closing of Data response function
            } //----------closing of complete function
          }); //------ end of capturing Client folder data and closing of SPServices function
    alert("All documents in the view entered have been restored to the version requested");
    } //----end of RestoreOriginalVersion

//-----------------------call Restore
    function DoRestore(x,y){
            $().SPServices({
                    operation: "RestoreVersion",
                    fileName:x,
                    fileVersion:y,
                                                        completefunc: function(xData,Status){
                                                            //alert(x+" Has been restored to version "+y);
                                                         }
            }); //------ end of RestoreVersion SPServices function
    }
}); //------end of document ready
</script>