SharePoint JavaScript API 根据文件路径删除文件

  最近,有这么个需求,然后写了几行代码,记录一下。有需要的可以参考一下。

  有几个需要注意的地方,就是文件URL要传相对地址,使用网站对象之前要Load一下。

  当然,如果你的网站不在根路径下,还可以用oWebsite.get_serverRelativeUrl()获取一下网站的相对路径,如果是跟站点,会返回一个斜杠。

<style type="text/css">
#filePath
{
    width:400px;
    height:40px;
}
#btnDelete
{
    width:100px;
    height:40px;
    background-color:gray;
    color:black;
}
</style>
<script type="text/javascript">
function runDelete()
{
    var filePath = document.getElementById("filePath").value;
    deleteFile(filePath);
}

//filePath format: /Document Shared/test.txt
function deleteFile(filePath)
{
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    clientContext.load(oWebsite);
    var fileToDelete = oWebsite.getFileByServerRelativeUrl(filePath);
    fileToDelete.deleteObject();
    clientContext.executeQueryAsync(
        Function.createDelegate(this,function(){alert("true")}),
        Function.createDelegate(this,function(){alert("false")})
    );
}
</script>
<input type="text" value="/Document Shared/test.txt" id="filePath"/><br/>
<input type="button" onclick="runDelete()" value="delete" id="btnDelete"/>
原文地址:https://www.cnblogs.com/jianyus/p/8927129.html