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

SharePoint C# 如何通过图片库Title检索所有图片及对应名称

通过C# CSOM根据Title获取SharePoint图片库图片的实现方案

以下方案基于 SharePoint 客户端对象模型(CSOM)实现,是目前C#开发SharePoint相关功能最常用的方式,兼容SharePoint Online和本地部署的SharePoint Server版本。

前置准备

  • 对应版本CSOM包安装:
    • SharePoint Online 环境:NuGet安装 Microsoft.SharePointOnline.CSOM
    • SharePoint Server 本地环境:安装对应服务器版本的 Microsoft.SharePoint.Server.CSOM 包
  • 确保你使用的账号对目标图片库有至少读取权限

核心实现代码

完整示例(SharePoint Online 环境)

using Microsoft.SharePoint.Client;
using System.Security;

// 1. 初始化站点上下文
string siteUrl = "https://你的租户.sharepoint.com/sites/目标站点";
string userName = "你的账号@xxx.onmicrosoft.com";
string password = "你的账号密码";
string targetPicLibName = "你的图片库名称";
string targetPicTitle = "要查找的图片Title";

var securePassword = new SecureString();
foreach (char c in password) securePassword.AppendChar(c);

using (var context = new ClientContext(siteUrl))
{
    context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
    
    // 2. 获取目标图片库
    List picLib = context.Web.Lists.GetByTitle(targetPicLibName);
    
    // 3. 构建CAML查询过滤Title匹配的项
    CamlQuery query = new CamlQuery();
    query.ViewXml = @"<View Scope='RecursiveAll'>
                        <ViewFields>
                            <FieldRef Name='Title' />
                            <FieldRef Name='FileRef' />
                            <FieldRef Name='FileDirRef' />
                            <FieldRef Name='EncodedAbsUrl' />
                        </ViewFields>
                        <Query>
                            <Where>
                                <Eq>
                                    <FieldRef Name='Title' />
                                    <Value Type='Text'>" + targetPicTitle + @"</Value>
                                </Eq>
                            </Where>
                        </Query>
                      </View>";
    
    ListItemCollection items = picLib.GetItems(query);
    context.Load(items);
    context.ExecuteQuery();
    
    if (items.Count > 0)
    {
        ListItem targetItem = items[0]; // 若Title允许重复,需遍历所有匹配项
        // 需求1:获取图片的直接访问链接
        string picAbsoluteUrl = targetItem["EncodedAbsUrl"].ToString();
        Console.WriteLine($"图片访问地址:{picAbsoluteUrl}");
        
        // 需求2:下载图片二进制流
        File picFile = targetItem.File;
        context.Load(picFile);
        var fileStream = picFile.OpenBinaryStream();
        context.ExecuteQuery();
        
        // 可以将流保存到本地或者做其他处理
        using (var fs = System.IO.File.Create("本地保存路径/图片名.jpg"))
        {
            fileStream.Value.CopyTo(fs);
        }
    }
    else
    {
        Console.WriteLine("未找到对应Title的图片");
    }
}

SharePoint Server 本地环境差异点

仅身份验证部分和SPO不同,上下文初始化部分替换为以下代码即可:

using (var context = new ClientContext(siteUrl))
{
    // 使用当前Windows账号凭据
    context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    // 若需指定账号,替换为:
    // context.Credentials = new System.Net.NetworkCredential("用户名", "密码", "域");
    
    // 后续查询逻辑和上面SPO的代码完全一致
}

注意事项

  • 图片库的Title字段默认允许重复,若你的业务场景需要唯一匹配,建议提前做好Title唯一性校验,或者增加其他过滤条件缩小匹配范围
  • 如果需要获取图片的缩略图地址,可以直接在图片URL后拼接参数?width=xxx&height=xxx,SharePoint会自动返回对应尺寸的缩略图
  • 若图片库开启了内容审批,未审批的图片默认普通用户无法查询到,需要用有审批权限的账号查询才能返回结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 21:06:08