You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用R提取SharePoint列表中文件的元数据?

使用R提取SharePoint列表文件的元数据

所需工具包

你需要两个核心R包来处理API请求和数据解析:

  • httr:发送HTTP请求并处理身份认证
  • jsonlite:解析SharePoint返回的JSON格式数据

安装并加载包:

install.packages(c("httr", "jsonlite"))
library(httr)
library(jsonlite)

认证配置

根据你的SharePoint部署类型选择对应认证方式:

本地部署SharePoint(NTLM认证)

针对企业内部本地站点,通常用NTLM认证:

# 配置站点、列表和账号信息
site_url <- "https://your-sharepoint-domain/sites/your-target-site"
list_title <- "你的文件列表名称"
domain_username <- "DOMAIN\\your-username"  # 注意域账号格式
password <- "your-password"

SharePoint Online(OAuth认证)

针对在线版SharePoint,推荐用AzureAuth包获取OAuth令牌:

install.packages("AzureAuth")
library(AzureAuth)

# 获取认证令牌
token <- get_azure_token(
  resource = "https://your-sharepoint-site.sharepoint.com",
  tenant = "your-tenant-id",
  app = "your-client-id",
  password = "your-client-secret",
  auth_type = "client_credentials"
)

调用REST API提取元数据

SharePoint提供REST API访问列表数据,通过指定字段和关联实体获取完整元数据:

本地部署示例

# 构建API请求URL,指定要提取的字段和关联实体
api_endpoint <- paste0(
  site_url, 
  "/_api/web/lists/getbytitle('", list_title, "')/items?",
  "$select=Title,FileLeafRef,Modified,Created,Author/Title,Editor/Title,FileSizeDisplay",
  "&$expand=Author,Editor"
)

# 发送认证请求
response <- GET(
  url = api_endpoint,
  authenticate(domain_username, password, type = "ntlm"),
  add_headers(Accept = "application/json;odata=verbose")
)

# 检查请求是否成功
stop_for_status(response)

# 解析JSON响应
raw_content <- content(response, "text", encoding = "UTF-8")
parsed_data <- fromJSON(raw_content)

# 转换为数据框
metadata_df <- as.data.frame(parsed_data$d$results)

SharePoint Online示例

api_endpoint <- paste0(
  "https://your-sharepoint-site.sharepoint.com/sites/your-target-site",
  "/_api/web/lists/getbytitle('", list_title, "')/items?",
  "$select=Title,FileLeafRef,Modified,Created,Author/Title,Editor/Title,FileSizeDisplay",
  "&$expand=Author,Editor"
)

response <- GET(
  url = api_endpoint,
  add_headers(Authorization = paste("Bearer", token$access_token)),
  add_headers(Accept = "application/json;odata=nometadata")
)

stop_for_status(response)
raw_content <- content(response, "text", encoding = "UTF-8")
parsed_data <- fromJSON(raw_content)
metadata_df <- as.data.frame(parsed_data$value)

整理元数据

提取的原始数据可能包含冗余字段,按需筛选和格式化:

# 选择需要的字段并重命名
clean_metadata <- metadata_df[, c("Title", "FileLeafRef", "Created", "Modified", "Author.Title", "Editor.Title", "FileSizeDisplay")]
colnames(clean_metadata) <- c("文件标题", "文件名", "创建时间", "修改时间", "创建者", "最后修改者", "文件大小(KB)")

# 格式化日期字段
clean_metadata$创建时间 <- as.POSIXct(clean_metadata$创建时间, format = "%Y-%m-%dT%H:%M:%SZ")
clean_metadata$修改时间 <- as.POSIXct(clean_metadata$修改时间, format = "%Y-%m-%dT%H:%M:%SZ")

# 查看结果
head(clean_metadata)

关键注意点

  • 确保账号拥有目标SharePoint列表的读取权限
  • 自定义字段需使用SharePoint内部字段名(可通过浏览器访问API端点查看完整字段列表)
  • SharePoint Online的API端点格式与本地部署略有差异,注意调整URL结构
  • 处理大列表时,可通过$top和$skip参数分页获取数据,避免请求超时

内容的提问来源于stack exchange,提问作者Emmanuel Hamel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:03:12