ค้นหาเว็บไซต์

SharePoint Online: รับรายการและไลบรารีทั้งหมดโดยใช้ PowerShell


คุณกำลังมองหาวิธีรับรายการและไลบรารีทั้งหมดในไซต์ SharePoint Online ของคุณหรือไม่? ซึ่งจะมีประโยชน์สำหรับการตรวจสอบหรือเพียงแค่ดูภาพรวมของสิ่งที่อยู่บนไซต์ของคุณ ในบทความนี้ เราจะแสดงวิธีใช้ PowerShell เพื่อรับข้อมูลโดยละเอียดเกี่ยวกับรายการและไลบรารีทั้งหมด

SharePoint Online: PowerShell เพื่อรับรายการทั้งหมด

ในขณะที่ทำงานกับ SharePoint Online คุณอาจต้องวนซ้ำรายการทั้งหมดบนไซต์ นี่คือสคริปต์ PowerShell เพื่อรับรายการและไลบรารีทั้งหมดจากไซต์ SharePoint Online ที่ระบุ:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
$UserName="salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try { 
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials

    #sharepoint online powershell get all lists
    $Lists = $Context.web.Lists
    $Context.Load($Lists)
    $Context.ExecuteQuery()

    #Iterate through each list in a site   
    ForEach($List in $Lists)
    {
        #Get the List Name
        Write-host $List.Title
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PowerShell นี้รับชื่อรายการทั้งหมดจากไซต์ SharePoint Online ที่กำหนด

รับรายการทั้งหมดในไซต์ SharePoint Online และส่งออกเป็น CSV

มาเจาะลึกอีกหน่อยและรับคุณสมบัติรายการทั้งหมดแล้วส่งออกเป็นไฟล์ CSV:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Variable
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVPath = "C:\Temp\ListInventory.csv"

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

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
   
    #Get Lists from the site
    $Web = $Ctx.Web
    $Lists = $Web.Lists
    $Ctx.Load($Web)
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    $ListDataCollection = @()
    #Get List details
    ForEach ($List in $Lists)
    {
        $ListData = New-Object PSObject -Property ([Ordered] @{
        ListName = $List.Title
        Description = $List.Description
        ItemCount = $List.ItemCount
        BaseTemplateID = $List.BaseTemplate
        Created = $List.Created 
        BaseType = $List.BaseType
        ContentTypesEnabled = $List.ContentTypesEnabled
        Hidden = $List.Hidden
        ListId = $List.Id
        IsCatalog = $List.IsCatalog 
        LastItemDeletedDate = $List.LastItemDeletedDate 
        LastItemModifiedDate = $List.LastItemModifiedDate 
        ParentWebUrl = $List.ParentWebUrl 
        VersioningEnabled = $List.EnableVersioning    
        })
        $ListDataCollection += $ListData
    }
    $ListDataCollection

    #Export List data to CSV
    $ListDataCollection | Export-Csv -Path $CSVPath -Force -NoTypeInformation
    Write-host -f Green "List Statistics Exported to CSV!"
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

SharePoint Online: สคริปต์ PowerShell เพื่อวนซ้ำรายการและไลบรารีทั้งหมดในไซต์คอลเลกชัน

ตอนนี้ มาทำให้โค้ดนี้เป็นฟังก์ชันที่นำมาใช้ใหม่และรับรายการและไลบรารีจากไซต์และไซต์ย่อยทั้งหมดจากไซต์ที่ระบุได้


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
        
#Get Credentials to connect
$Cred= Get-Credential
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
    {
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Context.Load($Lists)
        $Context.ExecuteQuery()

        #Get all lists from the web   
        ForEach($List in $Lists)
        {
            #Get the List Name
            Write-host $List.Title
        }
    }

    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL) 
    {
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials

        $Web = $context.Web
        $Context.Load($web)
        #Get all immediate subsites of the site
        $Context.Load($web.Webs)  
        $Context.executeQuery()
 
        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web
 
        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Get-SPOWeb($SubWeb.URL)
        }
    }

    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

เอาต์พุตสคริปต์:

PnP PowerShell เพื่อรับรายการและไลบรารีทั้งหมดในไซต์คอลเลกชัน

มารับรายการและไลบรารีสินค้าคงคลังของไซต์ทั้งหมดในไซต์คอลเลกชันแล้วส่งออกเป็นไฟล์ CSV โดยใช้ PnP PowerShell


#Function to Get Lists and Libraries of a web
Function Get-SPOSiteInventory([Microsoft.SharePoint.Client.Web]$Web)
{
    Write-host -f Yellow "Getting Lists and Libraries from site:" $Web.URL

    #Get all lists and libraries
    $SiteInventory= @()
    $Lists= Get-PnPProperty -ClientObject $Web -Property Lists
    foreach ($List in $Lists)
    {
        $Data = new-object PSObject
        $Data | Add-member NoteProperty -Name "Site Name" -Value $Web.Title
        $Data | Add-member NoteProperty -Name "Site URL" -Value $Web.Url
        $Data | Add-member NoteProperty -Name "List Title" -Value $List.Title
        $Data | Add-member NoteProperty -Name "List URL" -Value $List.RootFolder.ServerRelativeUrl
        $Data | Add-member NoteProperty -Name "List Item Count" -Value $List.ItemCount
        $Data | Add-member NoteProperty -Name "Last Modified" -Value $List.LastItemModifiedDate
        $SiteInventory += $Data
    }

    #Get All Subwebs
    $SubWebs = Get-PnPProperty -ClientObject $Web -Property Webs
    Foreach ($Web in $SubWebs)
    {
        $SiteInventory+= Get-SPOSiteInventory -Web $Web
    }
    Return $SiteInventory
}

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\temp\SiteData.csv"

Try { 
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Root Web
    $Web = Get-PnPWeb

    #Call the function and export results to CSV file
    Get-SPOSiteInventory -Web $Web | Export-CSV $CSVFile -NoTypeInformation
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

เมื่อต้องการรับไลบรารีเอกสารทั้งหมดใน SharePoint Online โดยใช้ PowerShell ให้ใช้: SharePoint Online: PowerShell เพื่อแสดงรายการไลบรารีเอกสารทั้งหมด

บทความที่เกี่ยวข้อง