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

[玩转系统] SharePoint Online:使用 PowerShell 检查列表是否存在

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

SharePoint Online:使用 PowerShell 检查列表是否存在


要求:使用 PowerShell CSOM 脚本检查给定 SharePoint Online 网站中是否存在特定列表。

[玩转系统] SharePoint Online:使用 PowerShell 检查列表是否存在

用于检查 SharePoint Online 中是否存在列表的 PowerShell 脚本:

在创建之前是否需要检查 SharePoint 列表或文档库是否存在? PowerShell 可以提供帮助!在这篇文章中,我们将向您展示如何使用 PowerShell 来确定列表是否存在。它将按名称搜索列表并返回一条消息,指示是否找到该列表。

以下是用于检查列表是否存在的 SharePoint Online PowerShell:


#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"
  
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-ListExists($SiteURL, $ListName, $Credentials)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    Try {
            $List = $Ctx.Web.Lists.GetByTitle($ListName)
            $Ctx.Load($List)
            $Ctx.ExecuteQuery()
            Return $True
        }
    Catch [Exception] {
      Write-host $_.Exception.Message -f Red
      Return $False
     }
}

#Set Variables for Site URL and List Name
$URL= "https://crescent.sharepoint.com/sites/sales/"
$List="Documents"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to Check List Exists in given web  
$ListExists = Check-ListExists -SiteURL $URL -ListName $List -Credentials $Cred

if($ListExists) {
    write-host "List Exists in Given Site!!" -f Green
    #Proceed with your script
 }
 else {
    write-host "List Doesn't Exists on given web!" -f Red
 }

如果您想要自动执行任务或构建需要在执行某些操作之前检查文件是否存在的工作流程,这可能会很有用。

方法 2:PowerShell 检查 SharePoint Online 中是否存在列表

让我们检查网站是否使用列表名称给出了列表,而不是“Try-Catch”方法。


#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 check if a list or document library exists
Function Check-SPOListExists([Microsoft.SharePoint.Client.ClientContext]$Ctx, [String]$ListName)
{
    Try {
        #Get All lists from the web
        $Lists = $Ctx.Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()

        #Check if the given List exists
        $List = $Lists | where {$_.Title -eq $ListName}
        If($List -ne $Null)
        {
            Return $True
        }
        Else
        {
            Return $False
        }
    }
    Catch {
      Write-host -f Red $_.Exception.Message 
      Return $False
    }
}
   
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Project Documents"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
   
#Call the function to Check List Exists in given web  
$ListExists = Check-SPOListExists -Ctx $Ctx -ListName $ListName

If($ListExists -eq $True)
{
    Write-Host -f Green "List '"$ListName"' exists!"

    #Do Something with the List 
    #$List.Recycle()
    #$Ctx.ExecuteQuery()
}
Else
{
    Write-Host -f Red "List '"$ListName"' does not exists!"
}

PnP PowerShell 检查 SharePoint Online 中是否存在列表

使用 PowerShell 检查 SharePoint Online 中是否存在列表的最简单方法之一是使用 Get-PnPList cmdlet。让我向您展示如何使用 PnP PowerShell 确定列表是否存在,然后在必要时创建它:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Tasks"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Try to Get the List or document library
$List = Get-PnPList -Identity $ListName -ErrorAction SilentlyContinue

If($List -ne $Null)
{  
    Write-Host -f Green "List Exist!"
}
Else
{
    Write-Host -f Yellow "List Does Not Exist!"
    #Create the list
}

如果列表存在,您将看到“列表存在”。如果列表不存在,您将看到“列表不存在”消息。

总之,有多种方法可以使用 PowerShell 检查 SharePoint Online 中是否存在列表。您可以使用 Get-PnPList cmdlet、CSOM PowerShell 脚本或使用 Try-Catch 方法来检查列表是否存在。通过使用这些脚本,您可以快速轻松地检查列表是否存在。

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

取消回复欢迎 发表评论:

关灯