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

SharePoint Online用户评分列真实名称及REST获取方法咨询

问题描述

我的目标是显示用户是否已对列表中的项进行评分。需要从SharePoint Online中检索用户评分列,计划通过SPFx WebPart使用REST接口进行检索。

我编写的代码片段如下:

const columnsToRetrieve =
    `Id, Author/Id, Editor/Id, Editor/Title, Status, Title, Created, LikesCount, LikedBy/Title, LikedBy/Id, LikedBy/EMail, AverageRating`
do {
    if (!pagedItems) {
        pagedItems = await Web(siteUrl).using(SPFx(context)).lists
            .getByTitle(listName)
            .items
            .select(columnsToRetrieve)
            .expand("Author, Editor, LikedBy")
            .getPaged()

我尝试使用Rating、rating、Rates等字段名称,但均未被识别,不清楚该列的真实名称是什么,相关文档极少。曾查阅过针对SP2010的旧文档,但对SharePoint Online没有帮助。

解决方案

1. 确认评分列的内部名称

REST调用必须使用列的内部名称而非显示名称,获取方式:

  • 进入列表设置页面,找到目标评分列
  • 点击列名称进入编辑页面,查看浏览器地址栏中Field=后的参数值,该值即为列的内部名称(默认系统创建的评分列内部名称通常为Rating_x0020_0,手动添加的评分列会有类似命名规则)

2. 查询单个用户对项的评分状态

要判断当前用户是否已对某列表项评分,无法直接从列表项字段获取,需调用Social REST API查询:

// 获取当前用户对指定列表项的评分
async function getUserRatingForItem(siteUrl: string, context: any, listItemId: number, listGuid: string): Promise<number | null> {
    const itemUrl = `${siteUrl}/_layouts/15/listform.aspx?PageType=4&ListId=${listGuid}&ID=${listItemId}`;
    try {
        const response = await Web(siteUrl).using(SPFx(context)).social.ratings.getByUrl(itemUrl).get();
        return response.RatingValue || null; // 返回评分值,null表示未评分
    } catch (error) {
        // 404错误代表用户未对该项评分
        if ((error as any).status === 404) {
            return null;
        }
        throw error;
    }
}

3. 批量查询优化性能

如果需要同时查询多个项的用户评分,使用批量请求减少网络调用:

// 批量获取多个项的用户评分
async function getBatchUserRatings(siteUrl: string, context: any, listItemIds: number[], listGuid: string) {
    const web = Web(siteUrl).using(SPFx(context));
    const batch = web.createBatch();
    
    const ratingResults: {[key: number]: number | null} = {};
    
    listItemIds.forEach(id => {
        const itemUrl = `${siteUrl}/_layouts/15/listform.aspx?PageType=4&ListId=${listGuid}&ID=${id}`;
        web.social.ratings.getByUrl(itemUrl)
            .inBatch(batch)
            .get()
            .then(data => {
                ratingResults[id] = data.RatingValue;
            })
            .catch(error => {
                if ((error as any).status === 404) {
                    ratingResults[id] = null;
                } else {
                    ratingResults[id] = null;
                }
            });
    });
    
    await batch.execute();
    return ratingResults;
}

补充说明

  • 默认的AverageRating字段仅能获取项的平均评分,无法得知单个用户的评分状态
  • 调用Social API需要用户拥有至少查看对应列表项的权限
  • 列表项的URL必须使用指定格式,确保能被Social API正确识别

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 16:32:37