SharePoint REST API获取组关联站点标题仍返回旧值,如何强制获取更新后标题?
问题详情
尝试使用SharePoint REST API获取SharePoint Online站点的实时标题,但标题通过SharePoint UI更新3-4小时后,API仍返回旧标题。
当前代码
export const getLiveSiteTitle = async (): Promise<string> => { try { const token = cache.get('accessToken') as string; if (!token) throw new Error('Failed to retrieve access token from cache'); const response = await fetch( `${process.env.SHAREPOINT_BASE_PATH}/sites/acc-alifornia-project/_api/web?$select=Title`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json;odata.metadata=minimal', 'odata-version': '4.0', }, cache: 'no-store', } ); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); const data = await response.json(); console.log('Raw response:', data); return data.Title; } catch (error) { console.error("Error fetching live site title:", error); throw error; } };
补充细节
- 标题已通过SharePoint UI更新3-4小时;
- 使用
_api/web?$select=Title仍返回旧标题; - 设置
cache: 'no-store'无效果; - 希望在不使用Microsoft Graph API的前提下获取最新标题。
核心问题
是否有办法强制SharePoint REST API返回更新后的标题?
解决方案
可以尝试以下几种方法绕过缓存,获取最新的站点标题:
- 添加缓存破坏参数
在请求URL末尾追加随机参数(如时间戳),让服务器识别为全新请求,避免返回缓存数据:
const response = await fetch( `${process.env.SHAREPOINT_BASE_PATH}/sites/acc-alifornia-project/_api/web?$select=Title&nocache=${Date.now()}`, // 保持原有请求配置 );
- 强化缓存控制请求头
除了cache: 'no-store',添加更严格的缓存控制头,明确禁止使用缓存:
headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json;odata.metadata=minimal', 'odata-version': '4.0', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' },
- 调用站点属性端点
尝试使用_api/web/Properties端点获取标题,该端点的缓存策略可能与_api/web不同,站点标题通常存储在vti_title属性中:
const response = await fetch( `${process.env.SHAREPOINT_BASE_PATH}/sites/acc-alifornia-project/_api/web/Properties?$select=vti_title`, { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, 'Accept': 'application/json;odata.metadata=minimal', 'Cache-Control': 'no-cache, no-store, must-revalidate', }, } ); const data = await response.json(); return data.vti_title;
- 验证权限与请求路径
确认当前access token对应的账号拥有站点设置的查看权限,同时检查请求的站点路径是否完全正确,避免请求到错误站点的缓存数据。
内容的提问来源于stack exchange,提问作者Mike Bartels
相关产品推荐
相关产品推荐

