1. 程式人生 > >SharePoint 2013 Rest and SP.js Copyto and Moveto (Rest 和SP.js 基本用法之 複製和移動檔案)

SharePoint 2013 Rest and SP.js Copyto and Moveto (Rest 和SP.js 基本用法之 複製和移動檔案)

//copyto 
function Copyfiles() {
   //https://xxx.xxx.com.cn/itc/_api/web/folders/getbyurl(folderrelativeurl)/files/getbyurl('/Itstemplate/My Vacation.xlsx')/copyTo('/JSdocs/My',bOverWrite)
  var appweburl="https://xxx.xxx.com.cn/itc";
  var executor = new SP.RequestExecutor(appweburl); 
  executor.executeAsync(
  {
     url: appweburl + "/_api/web/folders/getbyurl('Itstemplate')/files/getbyurl('1.xlsx')/copyTo(strnewurl='JSdocs/1.xlsx',bOverWrite=false)",
     //由於不允許覆蓋,所以這裡bOverWrite=false。如果允許重新寫入 請使用 bOverWrite=false
     method: "POST",
     success: successHandler,
     error: errorHandler
    }
    );
}
//move to 
function movefiles() {
  var appweburl="https://xxx.xxx.com.cn/itc";
  //POST http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/files/getbyurl(filerelativeurl)/moveTo(newUrl,flags)
  var executor = new SP.RequestExecutor(appweburl); 
    executor.executeAsync({
    url: appweburl + "/_api/web/folders/getbyurl('Itstemplate')/files/getbyurl('1.xlsx')/moveTo(newurl='JSdocs/1.xlsx',flags=1)",
    method: "POST",
    success: successHandler,
    error: errorHandler
  });
}

function successHandler(){
alert('建立成功');
}
function errorHandler(){
alert('建立模版失敗,檢查此模版是否已經存在');
}

這裡要特別注意一個問題,開發的時候我是在本機做的,後來在伺服器操作,發現如果本地沒有office 好像檔案建立不成功,一直出錯。