使用JSOM检查文件或文件夹是否存在

本文介绍如何使用SharePoint JavaScript Object Model (JSOM)来判断文件或文件夹是否存在。通过示例代码展示了如何加载文件夹对象并检查其是否存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to Check with SharePoint JSOM if File or Folder Exists

Here’s a code snippet showing how to use SharePoint’s JavaScript Object Model (JSOM) to determine whether a file or folder exists:

var ctx = SP.ClientContext.get_current();
 
// Could also call getFileByServerRelativeUrl() here. Doesn't matter.
// The way this works is identical for files and folders.
var folder = ctx.get_web().getFolderByServerRelativeUrl("/path/to/folder");
 
ctx.load(folder, "Exists", "Name");
 
ctx.executeQueryAsync(
   function() {
      if (folder.get_exists()) {
         // Folder exists and isn't hidden from us. Print its name.
         console.log(folder.get_name());
      }
      else {
         console.log("Folder exists but is hidden (security-trimmed) for us.");
      }
   },
   function(s, args) {
      if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {
         // Folder doesn't exist at all.
         console.log("Folder does not exist.");
      }
      else {
         // An unexpected error occurred.
         console.log("Error: " + args.get_message());
      }
   }
);

The behavior of the client-side object model is slightly different depending whether the file or folder truly doesn’t exist or is just being hidden from us due to security-trimming.

The code snippet above takes this distinction into account and shows you how to check for both conditions.

You can also issue a CAML query against a document library (if that’s where your file/folder lives) to see if an item representing your file/folder exists, but the above snippet is intended to demonstrate the procedure for actual SP.File and SP.Folder objects.

 

原文地址:https://yieldreturnpost.wordpress.com/2015/08/25/how-to-check-with-sharepoint-jsom-if-file-or-folder-exists/

转载于:https://www.cnblogs.com/bjdc/p/6256680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值