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

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 文件创建多个列表

作者:精品下载站 日期:2024-12-14 15:43:56 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 从 CSV 文件创建多个列表


要求:从 CSV 文件在 SharePoint Online 中批量创建列表或文档库。

如何从 CSV 文件创建多个列表?

PowerShell 可以轻松在 SharePoint Online 中创建多个列表!本文将向您展示如何使用 PowerShell 从 SharePoint Online 中的 CSV 文件创建多个列表。如果您需要使用 CSV 中的预定义配置快速创建多个列表,这会很有用。将解析 CSV 文件,并根据文件中的列标题(例如文件中的列表名称、说明、模板等)配置列表。

我们可以结合使用 CSV 和 PowerShell 在 SharePoint Online 中批量创建多个列表。这是我的 CSV 文件:

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 文件创建多个列表

请注意,此处的“模板”列定义了列表模板。它可以是自定义列表、任务、联系人、文档库等。您可以在此处下载此 CSV:

PowerShell 从 CSV 文件创建多个列表

使用此 PnP PowerShell 从 CSV 创建 SharePoint 列表。如何在SharePoint Online中创建多个文档库?只需相应设置 CSV 模板即可!


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$CSVFilePath = "C:\Documents\ListCreationTemplate.csv"
  
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the data from CSV file
$CSVFile = Import-CSV $CSVFilePath
  
#Read CSV file and create List
ForEach($Row in $CSVFile)
{
    Try {
        #Create List
        Write-host -f Yellow "Creating List:"$Row.ListName
        If($Row.OnQuickLaunch -eq "True")
        {
            New-PnPList -Title $Row.ListName -Template $Row.Template -OnQuickLaunch -ErrorAction Stop | Out-Null
        }
        else
        {
            New-PnPList -Title $Row.ListName -Template $Row.Template -ErrorAction Stop | Out-Null
        }
        Write-host -f Green "`tCreated List '$($Row.ListName)'"
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

此 CSV 文件具有 ListName、Description、Template、OnQuickLaunch 列。您可以添加任何必要的列作为创建列表的参数。

Tail:如何从枚举器获取所有值?

如果您想从枚举中获取所有值,请使用以下 PowerShell 脚本。就我而言,我必须检索所有列表模板类型。


#Get All Values from the Enum
Function Get-EnumValues([string]$Enumerator)
{
    $EnumValues = @{}
    [Enum]::GetValues([Type]$Enumerator) | ForEach-Object {
        $EnumValues.add($_, $_.value__)
    }
    $EnumValues
}
Get-EnumValues -Enumerator "Microsoft.SharePoint.Client.ListTemplateType"

您还可以使用枚举器的 GetEnumValues() 方法。例如。


[Microsoft.SharePoint.Client.ListTemplateType].GetEnumValues()

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

取消回复欢迎 发表评论:

关灯