0

I am using SharePoint Office 365. I have tried to extrapolate how to do this from many other sources but I am stuck. I have the URL of a file (happens to be a OneNote Notebook) and I want to move the file to a new name.

This is my last iteration of my code. We already have the context in code above this segment

var onenote_file = target_person_folder + '/RenameMe';    
var File = context.get_web().getFileByServerRelativeUrl(onenote_file);
                    context.load(File);
                    console.log(File);
                    var onenote_file_target_name = target_person_folder + '/' + first_name + ' ' + last_name;
                    console.log(onenote_file_target_name);
                    SP.File.moveTo(context, onenote_file,onenote_file_target_name, 1);
                    context.executeQueryAsync(
                        function (sender, args) {
                            console('We renamed the file. YAY! ')
                        },
                        function (sender, args) {
                            console('BOO!, We did not move the file. .')
                        });

I think I am close but I would appreciate the help getting the rest of the way there.

Thanks!

2

As fox has answered using REST API, I would like to add via JSOM(way you have coded).

Pointing out issues in your code.

  • First you should load the onenote_file and run executequeryasync once to get your file object method then call moveTo method
  • Second is SP.File.moveTo which is static method, you have to use File(as your object name is File), Below is syntax for same

File.moveTo(newurl,overwriteflag)

Below code is tried and tested, you should able to use this.

var context = new SP.ClientContext.get_current();
var url = "/sites/mysc/Shared Documents/html.txt"
var myFile = context.get_web().getFileByServerRelativeUrl(url);


var neurl = "/sites/mysc/Shared Documents/html1.txt";

context.load(myFile);
 context.executeQueryAsync(function (sender, args) {
                    myFile.moveTo(neurl);
                         context.executeQueryAsync(
                        function (sender, args) {
                            console.log('We renamed the file. YAY! ')
                        },
                        function (sender, args) {
                            console.log('BOO!, We did not move the file. .')
                        });
                        },
                        function (sender, args) {

                        });
0

There is example:

function rename(webUrl,listTitle,itemId,fileName){
   var endpointUrl = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId +  ")";
   return executeJson(endpointUrl)
   .then(function(data){                 
      var itemPayload = {};
      itemPayload['__metadata'] = {'type': data.d['__metadata']['type']};
      itemPayload['Title'] = fileName;
      itemPayload['FileLeafRef'] = fileName;
      var itemUrl = data.d['__metadata']['uri'];
      var headers = {};
      headers["X-HTTP-Method"] = "MERGE";
      headers["If-Match"] =  "*";
      return executeJson(itemUrl,"POST",headers,itemPayload);
   });
}

var webUrl = _spPageContextInfo.webAbsoluteUrl; // web url
var listTitle = "Documents";  //list title
var itemId = 1;  //list item id
var fileName = "SP User Guide.docx"; //new file name

rename(webUrl,listTitle,itemId,fileName)
.done(function(item){
   console.log('Renamed');
})
.fail(function(error){
    console.log(error);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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