[玩转系统] 使用 PowerShell 在 SharePoint 中创建网站栏
作者:精品下载站 日期:2024-12-14 21:00:45 浏览:14 分类:玩电脑
使用 PowerShell 在 SharePoint 中创建网站栏
网站栏是可重复使用的字段,您可以将其添加到 SharePoint 网站,并且可以在任意数量的列表或库中使用它们!当您必须批量添加网站栏时,添加网站栏的 PowerShell 方法非常有用。假设,您可能在 DEV 环境中创建了某些网站栏,并希望将它们放在 STAGING 或 PROD 环境中。您会使用 SharePoint UI 来一一创建它们吗?不!那会很忙。我将在这篇文章中分享我的 PowerShell 脚本,用于在 SharePoint 中创建网站栏。
使用 PowerShell 将网站栏添加到 SharePoint
以下是使用 PowerShell 在 SharePoint 2016 中创建网站栏的简单示例:
#Get the Root Web
$RootWeb = Get-SPWeb "https://intranet.crescent.com"
#Create a text Field site column
$RootWeb.Fields.Add("ProjectCode","Text",$false)
$SalaryField = $RootWeb.Fields.Add("Crescent-Salary", "Currency", $false)
使用 PowerShell 创建网站栏 - 让我们批量创建!
现在,让我们批量添加网站栏。只需在数组中输入列名称: $SiteColumnsToAdd 并运行代码。
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
# List of Site Column Names to be Added
$SiteColumnsToAdd = @("Branch Name", "Branch Code", "Destination")
ForEach($SiteColumn in $SiteColumnsToAdd)
{
if (!$RootWeb.Fields.ContainsField($SiteColumn))
{
#Remove Space in the Field's Internal Name
$InternalName= $SiteColumn.Replace(" ","")
#Add new site column
$RootWeb.Fields.Add($InternalName, "Text", $false)
#Get the site column
$Addedcolumn = $RootWeb.Fields.GetField($InternalName)
#Set the Column Title and Group
$Addedcolumn.Group="Crescent Columns"
$Addedcolumn.Title=$SiteColumn
$Addedcolumn.update()
Write-host "Site column $($SiteColumn) has been Added Successfully!"
}
}
$RootWeb.Dispose()
SharePoint 2013 PowerShell 创建网站栏 - 选择字段:
以下是在 SharePoint 中创建网站栏的 PowerShell 的另一个示例:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Create a Choice Field site column
$RootWeb.Fields.Add("Departments","Choice",$false)
#Get the field by internal Name
$ChoiceField=$RootWeb.Fields.GetField("Departments")
#Set the Site column Group
$ChoiceField.Group="Crescent Columns"
#Add Choices
$ChoiceField.Choices.Add("Operations")
$ChoiceField.Choices.Add("Marketing")
$ChoiceField.Choices.Add("Sales")
$ChoiceField.Choices.Add("Finance")
#Set Title
$ChoiceField.Title="Department Selection"
#Default choice
$ChoiceField.DefaultValue="Sales"
#Allow fill in choices
$ChoiceField.FillInChoice=$true
#Apply changes to the column
$ChoiceField.Update()
Write-host "Site column $($SiteColumn) has been Added Successfully!"
$RootWeb.Dispose()
SharePoint 2016 使用 PowerShell 创建网站栏 - 使用字段架构 XML 方法
AddFieldAsXml 方法为我们提供了更大的灵活性:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Define the Site column XML
$FieldXML = '<Field Type="Number" DisplayName="Bpt-InvoiceNo" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Min="1" Max="10000" Group="Bpt Columns" ID="{b81c7da6-1317-46fa-a32b-9f446c30b6e9}" StaticName="Bpt-InvoiceNo" Name="Bpt-InvoiceNo"></Field>'
#Create site columns with powershell from XML
$RootWeb.Fields.AddFieldAsXml($FieldXML)
Write-host "Site column has been Added Successfully!"
请参阅字段元素 XML 来指定字段选项,例如“只读”、“组”等。
https://msdn.microsoft.com/en-us/library/office/ms437580.aspx
SharePoint 使用 PowerShell 从 CSV 文件添加网站栏
从 CSV 文件批量添加网站栏怎么样?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$RootWebURL ="https://intranet.crescent.com"
$CSVPath = "D:\SiteColumns.csv"
#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL
#Import from CSV File
$SiteColumnsCSV = Import-csv -Path $CSVPath #CSV has "Title" column Header
foreach($Column in $SiteColumnsCSV)
{
#Get the field title from CSV
$FieldTitle = $Column.Title
#Check if a site column exists already
if (!$RootWeb.Fields.ContainsField($FieldTitle))
{
#Frame FieldXMLString from the column Title
$FieldXMLString = [String]::Format('<Field Type="Text" Name="{0}" DisplayName="{1}" StaticName="{0}" Required="FALSE"></Field>',$fieldTitle.Replace(" ",""),$FieldTitle)
#create site column sharepoint 2010 powershell from XML string
$RootWeb.Fields.AddFieldAsXml($FieldXMLString)
Write-host "Site column '$($FieldTitle)' has been Added Successfully!"
}
}
$RootWeb.Dispose()
相关帖子:
- 使用 PowerShell 复制粘贴网站栏:使用 PowerShell 导出导入网站栏
- 创建基于功能的解决方案以在 SharePoint 中添加网站栏:构建基于功能的解决方案以部署 SharePoint 网站栏
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag