当前位置: 首页 > news >正文

PowerShell 新建 SharePoint Online 列表

  前言

  最近,在开发SharePoint Online项目中,发现有好多好多好多的表要创建。

  正文

  1.这是PowerShell 执行成功的页面,如下图:

image

  2.这是建好的SharePoint Online 列表,非常的丝滑,如下图:

image

  3.PowerShell 命令,大家有需要可以改一下就可以用,尤其是封装一下customFields这个变量,就能通用,如下图:

<#
.SYNOPSIS
PowerShell 7专用 - 创建SharePoint Online自定义列表并批量添加字段
.DESCRIPTION
使用PnP.PowerShell创建自定义列表,支持配置列表基础信息,批量添加文本/数字/日期/布尔/多行文本等字段,兼容MFA登录
#># -------------------------- 可配置变量(按需修改以下内容) --------------------------
# 站点和列表基础信息
$siteUrl = "https://linyus.sharepoint.com/sites/Develop"  # 替换为目标SPO站点地址
$listName = "产品管理列表"                                      # 自定义列表名称
$listDescription = "用于存储公司产品基础信息的自定义列表"       # 列表描述(可选)# 自定义字段配置数组(可增删/修改字段,支持常见类型)
# 支持的字段类型:Text(单行文本)、Number(数字)、DateTime(日期时间)、Boolean(是/否)、Note(多行文本)、Currency(货币)
$customFields = @(@{DisplayName = "产品编号"       # 字段显示名InternalName = "ProductCode"   # 字段内部名(无空格/特殊字符)Type = "Text"                  # 字段类型Required = $true               # 是否必填},@{DisplayName = "产品名称"InternalName = "ProductName"Type = "Text"Required = $true},@{DisplayName = "产品价格"InternalName = "ProductPrice"Type = "Currency"              # 货币类型Required = $trueMinimumValue = 0               # 最小值(数字/货币类型有效)},@{DisplayName = "库存数量"InternalName = "StockCount"Type = "Number"Required = $false},@{DisplayName = "是否上架"InternalName = "IsOnShelf"Type = "Boolean"Required = $falseDefaultValue = $false          # 默认值(布尔类型有效)},@{DisplayName = "发布日期"InternalName = "ReleaseDate"Type = "DateTime"Required = $falseFormat = "DateTime"            # 日期格式:DateTime(日期+时间)/Date(仅日期)},@{DisplayName = "产品描述"InternalName = "ProductDesc"Type = "Note"                  # 多行文本Required = $falseRichText = $true               # 是否支持富文本(仅Note类型有效)}
)# -------------------------- 核心执行逻辑 --------------------------
# 1. 检查并安装PnP.PowerShell模块(PowerShell 7兼容版)
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell)) {Write-Host "🔧 正在安装最新版PnP.PowerShell..." -ForegroundColor CyanSet-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction SilentlyContinueInstall-Module -Name PnP.PowerShell -Force -AllowClobber -Scope CurrentUser -ErrorAction Stop
}# 2. 导入模块
Import-Module PnP.PowerShell -DisableNameChecking -Force -ErrorAction Stop
Write-Host "✅ PnP.PowerShell模块加载成功`n" -ForegroundColor Green# 初始化连接状态
$isConnected = $falsetry {# 3. 连接SPO站点(兼容MFA,PowerShell 7稳定登录方式)Write-Host "🔗 正在连接站点: $siteUrl" -ForegroundColor CyanConnect-PnPOnline -Url $siteUrl -UseWebLogin -ErrorAction Stop$isConnected = $trueWrite-Host "✅ 站点连接成功`n" -ForegroundColor Green# 4. 检查列表是否已存在(避免重复创建)$existingList = Get-PnPList -Identity $listName -ErrorAction SilentlyContinueif ($existingList) {Write-Host "⚠️ 列表 '$listName' 已存在,跳过创建步骤" -ForegroundColor Yellow}else {# 5. 创建自定义列表(GenericList=自定义列表模板)Write-Host "📝 正在创建列表: $listName" -ForegroundColor CyanNew-PnPList -Title $listName -Template GenericList -ErrorAction StopWrite-Host "✅ 列表 '$listName' 创建成功`n" -ForegroundColor Green}# 6. 批量添加自定义字段Write-Host "➕ 开始添加自定义字段...`n" -ForegroundColor Cyanforeach ($field in $customFields) {# 检查字段是否已存在$fieldExists = Get-PnPField -List $listName -Identity $field.InternalName -ErrorAction SilentlyContinueif ($fieldExists) {Write-Host "⚠️ 字段 '$($field.DisplayName)' 已存在,跳过添加" -ForegroundColor Yellowcontinue}# 根据字段类型构建参数$fieldParams = @{List = $listNameDisplayName = $field.DisplayNameInternalName = $field.InternalNameType = $field.TypeRequired = $field.RequiredAddToDefaultView = $true  # 自动添加到默认视图ErrorAction = "Stop"}# 添加字段到列表Add-PnPField @fieldParamsWrite-Host "✅ 字段 '$($field.DisplayName)' 添加成功" -ForegroundColor Green}Write-Host "`n🎉 所有操作完成!" -ForegroundColor GreenWrite-Host "📌 站点地址: $siteUrl" -ForegroundColor GreenWrite-Host "📌 列表名称: $listName" -ForegroundColor Green
}
catch {Write-Host "`n❌ 执行出错: $($_.Exception.Message)" -ForegroundColor Redexit 1
}
finally {# 断开连接(仅在连接成功时执行)if ($isConnected) {try {Disconnect-PnPOnline -ErrorAction SilentlyContinueWrite-Host "`n🔌 已断开SPO站点连接" -ForegroundColor Cyan}catch {Write-Host "`n⚠️ 断开连接时的小问题(可忽略): $($_.Exception.Message)" -ForegroundColor Yellow}}
}

  结束语

  其实没什么想多说的,大家看着用吧。

http://www.jsqmd.com/news/425434/

相关文章:

  • 基于springboot框架的火车票购票系统_33bx0nk0
  • 基于springboot框架的航班查询与推荐系统飞机订票系统设计与开发_d1b11p63
  • 有源电力滤波器Matlab仿真之旅
  • [vue3入门]HTML Learn Data Day 7
  • 重庆有哪些招聘平台?2026本地求职招工平台全攻略
  • 独立主格
  • ClawCon 2026:AI智能体从虚拟走向物理的里程碑
  • [vue3 入门]HTML Learn Data Day 7
  • Ubuntu server 24.04 LTS 初始配置记录(二、配置远程登录)
  • 超音速原理:从激波到尖端科技
  • 为什么谁先发送低电平谁就掌握对总线的控制权
  • 超声相控阵波束合成实战代码
  • 使用trae开发工具对某书屋项目进行接口自动化测试
  • 基于STM32DSP库与MATLAB的数字滤波器设计与实现
  • P1894 [USACO4.2] 完美的牛栏The Perfect Stall 题解
  • Bootstrap4 面包屑导航
  • G008 【模板】树的重心 带权重心 DFS P1670 P1395 P2986 洛谷
  • 行走人间・第二篇:生活
  • 基于springboot的健身服务管理系统
  • Web 词汇表
  • 3mm 厚层 CT 冠脉配准踩坑实录:从血管碎裂、空间漂移到 Elastix 完美对齐
  • 关于arduino 库文件的标准结构
  • 用ESP32打造动态网页仪表盘
  • flutter: 用getxservice管理状态
  • 感受一下谷歌的语义识别能力 和 古老的每个关键词单独做一个站的玩法
  • 2026年诚信的景观灯光护栏厂家优质推荐 - 品牌鉴赏师
  • 【claude】拒绝为美军提供“黑暗版”Claude,Anthropic成首个被五角大楼列入“供应链风险”的美国AI公司
  • 碎碎念
  • 正确理解C++中的值语义:move
  • 2026年防爆声级计制造厂推荐,防爆认证噪声监测专业厂商 - 品牌鉴赏师