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

[玩转系统] SharePoint Online:使用 PowerShell 创建多个文档库

作者:精品下载站 日期:2024-12-14 21:09:29 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 创建多个文档库


要求:使用 PowerShell 在 SharePoint Online 中创建多个文档库。

如何在SharePoint Online中创建多个文档库?

您是否希望在 SharePoint Online 中创建多个文档库?单个文档库可能并不总是足够的。也许您想为公司的每个部门创建一个库或单独的 SharePoint 库来存储您正在处理的不同项目的文件,或者您可能有大量需要组织的文档。无论出于何种原因,创建多个文档库都是管理文件、保证文件安全并充分利用 SharePoint 文档管理的最佳实践。在本指南中,我们将向您展示如何通过 PowerShell(一种用于自动化任务的脚本语言)在 SharePoint Online 中创建多个文档库!

有时,默认库“文档”可能不够,您可能需要向网站添加其他文档库。假设您拥有网站的编辑权限(或网站所有者或网站成员组的成员),则在 SharePoint Online 中创建多个文档库非常简单,只需几个简单的步骤即可完成:

  1. 登录您的 SharePoint Online 网站 >> 单击菜单栏中的“+ 新建”按钮 >> 从下拉菜单中选择“文档库”。

    [玩转系统] SharePoint Online:使用 PowerShell 创建多个文档库

  2. 输入文档库的名称和描述,然后单击“创建”。

    [玩转系统] SharePoint Online:使用 PowerShell 创建多个文档库

对您想要创建的每个其他库重复这些步骤。创建文档库后,您可以将新文件(几乎所有文件类型)上传到其中或创建新文件夹以帮助组织文档。您还可以为团队成员或员工设置权限,并控制谁有权访问您的文档库,并通过库设置添加字段、内容类型、设置权限继承、版本历史记录、工作流程等来进一步自定义库。

PowerShell 在 SharePoint Online 中创建多个文档库

Microsoft SharePoint 是一个多功能平台,可用于以各种方式管理和共享数据以进行协作。 SharePoint 团队网站附带一个名为“文档”的默认文档库。您是否经常定期创建文档库?使用 PowerShell,您可以立即创建多个文档库,这是一次批量配置文档库的好方法。

如何在SharePoint Online中创建多个文档库?我们可以通过将脚本包装到可重用函数中并使用要创建的文档库数组调用该函数来创建多个文档库。

SharePoint 中的文档库数量有限制吗?
一个 SharePoint 网站可以有多少个库?从技术上讲,每个网站集(包括根网站和任何子网站)可以组合 2000 个列表和库。由于 5000 的列表视图阈值限制会影响 SharePoint Online 中的性能和可用性,因此最好创建多个文档库。

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
     
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{

 [cmdletbinding()]

    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String[]] $LibraryNames,
        [Parameter(Mandatory=$False)] [String] $Description
    )    
    Try {
        #Create Each Document Library
        ForEach($LibraryName in $LibraryNames)
        {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
         
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()
 
            #Check if Library name doesn't exist already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Create a new Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $Web.Lists.Add($ListInfo) | Out-Null
                $Ctx.ExecuteQuery()
  
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
            }
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
 
#Get Credentials to connect
$Cred = Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
#Get the web from URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.executeQuery()
 
#Call the function to create document libraries
CreateSPO-DocumentLibrary -LibraryNames @("Document Library 1", "Document Library 2", "Document Library 3", "Document Library 4")    

使用 PowerShell 在 SharePoint Online 中创建多个文档库

如果您想向批量文档库脚本添加一些附加参数(例如“快速启动”)怎么办?这是我的 CSV 文件,其中包含文档库详细信息。

[玩转系统] SharePoint Online:使用 PowerShell 创建多个文档库

您可以在此处下载 CSV 模板:

让我们使用 PowerShell 从 CSV 文件创建单独的文档库:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{
[cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String] $LibraryName,
        [Parameter(Mandatory=$False)] [String] $Description,
        [Parameter(Mandatory=$False)] [Switch] $ShowOnQuickLaunch
    )    
    Try {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
        
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()

            #Check if Library name doesn't exist already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Set Quick Launch Option
                If($ShowOnQuickLaunch.IsPresent)
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::On
                }
                Else
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::Off
                }

                #Create Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $List = $Web.Lists.Add($ListInfo)
                $List.OnQuickLaunch = $QuickLaunchOption
                $List.Update()
                $Ctx.ExecuteQuery()
 
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exists!"
            }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

#Set Parameters
$CSVFilePath = "C:\Temp\DocLibs.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath 

#Read CSV file and create a document library
ForEach($Line in $CSVFile)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Line.SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()
    
    #Get the ShowOnQuickLaunch option
    $ShowOnQuickLaunch = [System.Convert]::ToBoolean($Line.ShowOnQuickLaunch)

    #Call the function to create document library
    If($ShowOnQuickLaunch -eq $True)
    {
        CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description -ShowOnQuickLaunch
    }
    Else
    {
        CreateSPO-DocumentLibrary -LibraryName $Line.LibraryName -Description $Line.Description
    }
}    

“TemplateType”参数定义库的类型。使用“[Microsoft.SharePoint.Client.ListTemplateType].GetEnumNames()”获取可在 SharePoint Online 中创建的所有可用列表和库。例如,图片库、公告等。

使用 PnP PowerShell 批量创建文档库

这次,我必须从一个 CSV 文件创建多个文档库,并设置文档库的特定属性,例如设置版本控制(主要版本-次要版本)、列出经验和权限!该 CSV 文件有两列:LoginID 和 DisplayName。

[玩转系统] SharePoint Online:使用 PowerShell 创建多个文档库

使用此 PowerShell 从 CSV 文件输入批量创建文档库。


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Shared"
$CSVFilePath = "C:\Users\salaudeen\Documents\Users.csv"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath
 
#Read CSV file and create new library
ForEach($Line in $CSVFile)
{
    Try {
        #Create document library
        Write-host -f Yellow "Creating Document Library:"$Line.DisplayName
        New-PnPList -Title $Line.DisplayName -Template DocumentLibrary -Url $Line.LoginID -ErrorAction Stop

        #Set Library Properties
        Set-PnPList -Identity $Line.DisplayName -EnableVersioning $True -EnableMinorVersions $True -CopyRoleAssignments:$False -BreakRoleInheritance -ListExperience NewExperience
        $Library  = Get-PnPList -Identity $Line.DisplayName

        #Exclude from Search Results
        $Library.NoCrawl = $True
        $Library.Update()

        #Grant permission on List to User
        $UserID = $Line.LoginID+"@crescent.com"
        Set-PnPListPermission -Identity $Line.DisplayName -AddRole "Full Control" -User $UserID

        Write-host -f Green "`tCreated Document Library:"$Line.DisplayName
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

您也可以在 OneDrive 网站上使用这些脚本。

文件夹仍然比元数据更广泛使用。要在 SharePoint 文档库中创建多个文件夹,请使用:如何将文件夹批量添加到 SharePoint Online?

结论

总之,在 SharePoint Online 中创建多个文档库是一个简单明了的过程,可以通过 PowerShell 执行。通过使用这种方法,您可以有效地管理和组织您的SharePoint内容和信息,保证业务需求,例如每个部门或团队都有一个单独的库来存储文件和文档。借助触手可及的 SharePoint Online 和 PowerShell 的强大功能,您可以简化内容管理任务,并确保您的网站井井有条并可根据需要进行访问。

经常问的问题:

如何使用 PowerShell 获取 SharePoint Online 中的文档库?

使用:Get-PnPList -Identity“DocumentLibraryName”从 SharePoint Online 网站获取任何文档库。您还可以使用 CSOM PowerShell 脚本 $Ctx.Web.Lists.GetByTitle(“DocumentLibraryName”) 来获取库。
详细信息:使用 PowerShell 在 SharePoint Online 中获取文档库

如何使用 PowerShell 获取 SharePoint Online 文档库中的文件列表?

您可以使用 CSOM 或 PnP PowerShell 脚本获取 SharePoint Online 文档库中所有文件的列表,并将清单导出到 CSV 文件。
详细信息:如何获取 SharePoint Online 文档中的文件列表图书馆?

如何更改SharePoint在线文档库URL?

若要更改文档库的 URL,您可以使用文件浏览视图、SharePoint Designer 或 PowerShell 方法。
详细信息:在 SharePoint Online 中重命名文档库 URL

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

取消回复欢迎 发表评论:

关灯