当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 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 在 SharePoint 中创建网站栏

使用 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 网站栏

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯