使用CSOM复制列表:将people列表字段迁移至persons新列表
嘿,我来帮你搞定用CSOM复制SharePoint列表字段这件事!你已经写了一部分代码,我把完整的实现和细节说明给你,这样就能把people列表的所有自定义和默认字段都复制到新的persons列表里啦:
核心思路
要实现这个需求,我们需要分四步走:
- 连接到目标SharePoint网站,加载源列表(
people)及其字段集合 - 创建新列表(
persons)的基础结构 - 遍历源列表的字段,过滤掉系统自动生成的内置字段(避免重复添加报错)
- 将源字段完整复制到新列表中
完整代码实现
using Microsoft.SharePoint.Client; using System; class ListFieldCopier { static void Main(string[] args) { // 替换成你的SharePoint网站URL string siteUrl = "https://yourtenant.sharepoint.com/sites/yourSite"; // 初始化CSOM上下文 using (ClientContext oClientContext = new ClientContext(siteUrl)) { // 如果是SharePoint Online,需要添加身份验证(比如用户名密码或者应用权限) // var securePassword = new System.Security.SecureString(); // foreach (char c in "yourPassword") securePassword.AppendChar(c); // oClientContext.Credentials = new SharePointOnlineCredentials("username@tenant.onmicrosoft.com", securePassword); // 加载网站对象 Web oWeb = oClientContext.Web; oClientContext.Load(oWeb); // 获取源列表并加载其字段集合(包含字段的SchemaXml等关键属性) List sourceList = oWeb.Lists.GetByTitle("people"); oClientContext.Load(sourceList.Fields, fields => fields.Include( field => field.SchemaXml, field => field.InternalName, field => field.TypeAsString)); oClientContext.ExecuteQuery(); // 配置新列表的创建信息 ListCreationInformation creationInfo = new ListCreationInformation(); creationInfo.Title = "persons"; creationInfo.Description = "new list created using VS 2013 &CSOM..."; // 设置列表模板类型,和源列表保持一致(这里用通用列表模板,可根据实际修改) creationInfo.TemplateType = (int)ListTemplateType.GenericList; List targetList = oWeb.Lists.Add(creationInfo); oClientContext.Load(targetList); oClientContext.ExecuteQuery(); // 遍历源字段,复制到新列表 foreach (Field sourceField in sourceList.Fields) { // 跳过隐藏、只读的系统字段,避免重复添加 if (!sourceField.Hidden && !sourceField.ReadOnlyField && !IsSystemField(sourceField.InternalName)) { try { // 通过SchemaXml完整复制字段的所有属性 Field newField = targetList.Fields.AddFieldAsXml(sourceField.SchemaXml, true, AddFieldOptions.AddFieldInternalNameHint); oClientContext.Load(newField); oClientContext.ExecuteQuery(); Console.WriteLine($"已成功复制字段: {sourceField.Title}"); } catch (Exception ex) { Console.WriteLine($"复制字段{sourceField.Title}失败: {ex.Message}"); } } } Console.WriteLine("\n新列表创建并完成字段复制!"); } } // 辅助方法:判断是否是系统内置的无需手动添加的字段 private static bool IsSystemField(string internalName) { // 这些字段是SharePoint列表默认自带的,新列表创建时会自动生成 string[] systemFields = { "ID", "Created", "Modified", "Author", "Editor", "Attachments", "ContentType", "Title" }; return Array.Exists(systemFields, field => field.Equals(internalName, StringComparison.OrdinalIgnoreCase)); } }
关键细节说明
- 引用程序集:确保你的项目引用了
Microsoft.SharePoint.Client和Microsoft.SharePoint.Client.Runtime,可以通过NuGet安装Microsoft.SharePointOnline.CSOM包(对应SPO)或者本地CSOM包(对应SharePoint Server) - 身份验证:如果是SharePoint Online,需要添加用户名密码或者应用权限的验证代码(代码里已经注释了示例);如果是本地SharePoint,用
NetworkCredential即可 - 字段过滤:
IsSystemField方法里的字段可以根据你的需求调整,比如如果Title字段是源列表自定义过的,你可以移除它,手动复制自定义配置 - 字段复制准确性:使用
AddFieldAsXml方法可以完整保留源字段的所有属性(比如类型、最大长度、默认值、验证规则等),比手动构建字段信息更可靠
内容的提问来源于stack exchange,提问作者Beginner
相关产品推荐
相关产品推荐

