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

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

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

SharePoint Online:使用 PowerShell 创建文档库


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

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

文档库是 SharePoint Online 中最常用的库类型,用于存储和协作 Office 文档,例如 Word、Excel、PowerPoint 等。SharePoint Online 文档库附带默认元数据属性:“类型”、“名称”、“修改”和“修改者”,并且可以根据我们的要求进行扩展。在继续执行以下步骤之前,请确保您对要在其中创建文档库的网站具有“编辑”权限级别!让我们看看如何在 SharePoint Online 中创建新文档库。

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

文档库是 SharePoint Online 的重要组成部分,提供了一种收集文件并与同事共享文件的简单方法。要在 SharePoint Online 网站上创建新文档库,请执行以下步骤:

  1. 在 Web 浏览器中导航到 SharePoint Online 站点 >> 单击“设置”齿轮 >> 选择“站点内容”。
  2. 在“网站内容”页面上,单击“新建”菜单并选择“文档库”选项。 (或者在现代 SharePoint 网站的主页上,单击“新建”按钮,然后选择“文档库”)

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

  3. 提供库的名称和描述。根据您的要求设置“在站点导航中显示”复选框,然后单击“创建”按钮创建新的文档库。

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

这将创建一个新的 SharePoint 文档库,您的浏览器应该将您带到那里!让我们看看如何使用 PowerShell 在 SharePoint Online 中创建库。

SharePoint Online:PowerShell 使用 CSOM 创建新文档库

好吧,如何使用 PowerShell 在 SharePoint Online 中创建文档库?使用 PowerShell 在 SharePoint Online 中创建文档库的方法有两种。第一个是使用 SharePoint Online CSOM PowerShell,第二个是使用 PnP PowerShell。

要创建文档库,您首先需要使用 PowerShell 连接到 SharePoint Online 网站。连接后,您可以使用 PnP PowerShell 中的 New-PnPList cmdlet 或 CSOM 中的 Lists.Add() 方法来创建库。您需要为您的库指定名称和可选描述,并选择合适的模板。您还可以指定其他选项,例如文档库的快速启动、模板和 URL。


#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"

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sites/Sales"
$LoginName ="[email protected]"
$LoginPassword ="Password" 

#Get the Client Context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

#supply Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword -asplaintext -force  
$Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($LoginName,$SecurePWD)
$Context.Credentials = $Credential

#powershell create document library sharepoint online
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = "Project Docs"
$ListInfo.TemplateType = 101 #Document Library
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = "Repository to store project artifacts"
$List.Update()
$Context.ExecuteQuery()

write-host "New Document Library has been created!"

确保更改 $SiteURL 和与您的租户匹配的其他变量。让我们向上面的脚本添加一些错误处理,并使用 PowerShell 创建一个文档库 SharePoint Online。

如何使用 PowerShell 在 SharePoint Online 中创建文档库?

文档库是用于在 SharePoint Online 网站上存储任何类型文件的容器。下面是用于 SharePoint Online 的 PowerShell,用于创建添加了一些错误处理的文档库。


#Import SharePoint Online PowerShell Module
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
   
Function Create-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $DocLibraryName
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential

    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get All Lists from the web
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()
 
    #Check if Library name doesn't exists already and create document library
    if(!($Lists.Title -contains $DocLibraryName))
    { 
        #create document library in sharepoint online powershell
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $DocLibraryName
        $ListInfo.TemplateType = 101 #Document Library
        $List = $Ctx.Web.Lists.Add($ListInfo)
        $List.Update()
        $Ctx.ExecuteQuery()
  
        write-host  -f Green "New Document Library has been created!"
    }
    else
    {
        Write-Host -f Yellow "List or Library '$DocLibraryName' already exist!"
    }
}
Catch {
    write-host -f Red "Error Creating Document Library!" $_.Exception.Message
}
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Sales"
$DocLibraryName="Invoices"
 
#Call the function to create document library
Create-DocumentLibrary -siteURL $SiteURL -DocLibraryName $DocLibraryName
您还可以指定“URL”属性来设置文档库的 URL。例如。您可能需要从 URL 中删除 %20 个字符!

使用此 PowerShell 在 SharePoint Online 中创建列表:如何使用 PowerShell 在 SharePoint Online 中创建列表?

SharePoint Online:使用 PnP PowerShell 添加文档库

使用 New-PnPList cmdlet 创建新文档库。以下是如何使用 PnP PowerShell 脚本在 SharePoint Online 中创建文档库:


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Project Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online create document library powershell
New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch

文档库就位后,您可以开始向其中上传文件和文件夹:如何将整个文件夹上传到 SharePoint Online?

好吧,检查一个特定的文档库是否已经存在怎么样,如果不存在,创建一个新的文档库怎么样?好吧,我们可以将上面的脚本包装成一个可重用的函数,如下所示:


#Function to Ensure a SharePoint Online document library
Function EnsurePnP-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $LibraryName
    )    
    Try {
        Write-host -f Yellow "`nEnsuring Library '$LibraryName'"
         
        #Check if the Library exist already
        $List = Get-PnPList | Where {$_.Title -eq $LibraryName} 
        #Also works: $List = Get-PnPList -Identity $LibraryName

        If($List -eq $Null)
        {
            #Create Document Library
            $List = New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch  
            write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
        }
        Else
        {
            Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exist!"
        }
    }
    Catch {
        write-host -f Red "`tError Creating Document Library!" $_.Exception.Message
    }
}

#Connect to SharePoint Online
Connect-PnPOnline "https://crescent.sharepoint.com/Sites/Marketing" -Interactive

#Call the function to Ensure Document Library
EnsurePnP-DocumentLibrary -LibraryName "Branding"

一个 SharePoint 网站可以有多个库吗?当然!可以在一个 SharePoint 网站中出于不同目的创建多个文档库。如果要从 CSV 文件在 SharePoint Online 中创建多个文档库,请使用:使用 PowerShell 在 SharePoint Online 中创建多个文档库

经常问的问题:

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

您可以在 SharePoint 中拥有多个文档库。只需将文档库名称包装在一个数组中,然后调用该函数即可创建一个新库!例如,

$DocLibs=@(“Library1”,“Library2”,“Library3”)
ForEach($DocLibs 中的$Library)
{
#Call创建库的函数
EnsurePnP-DocumentLibrary -LibraryName $Library
}

我可以复制 SharePoint Online 中的现有文档库吗?

是的!您可以通过将任何现有文档库另存为列表模板解决方法来克隆它!如何使用 PowerShell 在 SharePoint Online 中复制文档库?

如何向 SharePoint Online 中的文档库授予权限?

浏览到库设置 >> 在列表设置页面上,单击“此文档库的权限”链接 >> 中断权限继承。现在,单击功能区中的“授予权限”以向用户和组提供访问权限。更多信息请参见:如何向 SharePoint Online 中的文档库授予权限?

如何在SharePoint Online中创建文档库模板?

要从 SharePoint Online 文档库创建模板,请导航到 SharePoint Online 库 >> 单击“设置”>>“文档库设置”>> 单击“权限和管理”组下的“将列表另存为模板”。
更多信息: 如何在SharePoint Online中从文档库创建模板?

如何将整个文件夹上传到 SharePoint Online?

要将整个文件夹上传到 SharePoint Online,请使用文档库页面上的“上传”按钮,然后从下拉菜单中选择“文件夹”。然后,选择要上传的文件夹,然后单击“打开”开始上传过程。或者,您可以使用 OneDrive 同步客户端将文件夹同步到您的计算机,然后将其拖放到 SharePoint Online 文档库中。

我可以在 SharePoint 页面上添加文档库吗?

是的。您可以在 SharePoint 页面上添加文档库。只需转到要添加库的页面,单击“编辑”按钮,然后从顶部菜单中选择“插入”即可。从那里,您可以选择“文档库”,然后选择要添加的库。
更多信息:如何将文档库添加到 SharePoint Online 中的页面?

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

取消回复欢迎 发表评论:

关灯