Tìm kiếm trang web

SharePoint Online: Nhận tất cả các trường danh sách bằng PowerShell


Yêu cầu: SharePoint Online PowerShell để lấy tất cả các trường danh sách.

Làm cách nào để lấy trường danh sách trong SharePoint Online?

Bạn có bao giờ cần lấy các cột danh sách từ Danh sách SharePoint Online không? Có thể bạn muốn biết tên nội bộ của cột SharePoint Online hoặc một số thuộc tính khác? Bài đăng trên blog này sẽ chỉ cho bạn cách sử dụng PowerShell và giao diện trình duyệt web để lấy tên nội bộ của cột danh sách SharePoint Online.

Bạn có thể lấy các cột danh sách trong SharePoint Online bằng cách điều hướng đến cài đặt danh sách trong phần “Cột” và bạn sẽ tìm thấy tất cả các trường của danh sách cụ thể.

SharePoint Online: PowerShell để lấy trường danh sách

Đây là SharePoint Online PowerShell để lấy các cột danh sách: Hãy sử dụng PowerShell để lấy các thuộc tính trường danh sách và xuất sang tệp CSV, chẳng hạn như:

  • Liệt kê tên trường
  • Loại lĩnh vực
  • ID trường
  • Tên nội bộ của trường
  • XML lược đồ trường
  • và Mô tả trường

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

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

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

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
        
#Get the List and list fields
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)

#sharepoint online powershell get list columns
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
        
#Iterate through each field in the list
Foreach ($Field in $List.Fields)
{   
    #Skip System Fields
    if(($Field.ReadOnlyField -eq $False) -and ($Field.Hidden -eq $False)) 
    {
       #get internal name of sharepoint online list column powershell 
       Write-Host $Field.Title : $Field.InternalName
    }
}

SharePoint Online: Nhận tất cả các trường của danh sách và xuất sang CSV

Hãy lấy tất cả các trường danh sách từ danh sách SharePoint Online và xuất chúng sang tệp 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"

#function to Get all fields from a SharePoint Online list or library
Function Get-SPOListFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $Ctx.Load($List.Fields)
        $Ctx.ExecuteQuery()
        
        #Array to hold result
        $FieldData = @()
        #Iterate through each field in the list
        Foreach ($Field in $List.Fields)
        {   
            Write-Host $Field.Title `t $Field.Description `t $Field.InternalName `t $Field.Id `t $Field.TypeDisplayName

            #Send Data to object array
            $FieldData += New-Object PSObject -Property @{
                    'Field Title' = $Field.Title
                    'Field Description' = $Field.Description
                    'Field ID' = $Field.Id 
                    'Internal Name' = $Field.InternalName
                    'Type' = $Field.TypeDisplayName
                    'Schema' = $Field.SchemaXML
                    }
        }
        Return $FieldData
    }
    Catch {
        write-host -f Red "Error Getting Fields from List!" $_.Exception.Message
    } 
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CSVLocation ="C:\Temp\ListFields.csv"

#Call the function to get all list fields
Get-SPOListFields -SiteURL $SiteURL -ListName $ListName | Export-Csv $CSVLocation -NoTypeInformation

PnP PowerShell để lấy tất cả các trường và tên nội bộ từ danh sách

Bạn có cần biết tên nội bộ của cột trong danh sách SharePoint Online không? Nếu vậy, đây là PowerShell để lấy tất cả các trường cùng với tên trường, Tên nội bộ và GUID từ danh sách SharePoint Online.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Get All Columns from List
    Get-PnPField -List $ListName
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Xuất tất cả các trường từ tất cả danh sách trong trang web SharePoint Online

Vì mục đích sao lưu hoặc kiểm tra, bạn muốn trích xuất tất cả thông tin trường từ mọi danh sách và thư viện trong một trang web? Chắc chắn! Đây là cách PnP PowerShell để làm điều đó!


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail"
$CSVFile = "C:\Temp\ListFields.csv"
$ListFields = @()

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Get All lists from the site - Exclude Hidden
    $Lists = Get-PnPList | Where {$_.Hidden -eq $false}
    
    ForEach($List in $Lists)
    {
        Write-host -f Yellow "Processing List:"$List.Title
        
        #Get All Fields from the List
        $Fields = Get-PnPField -List $List | Where {$_.Hidden -eq $false}
        ForEach($Field in $Fields)
        {
            $ListFields += [PSCustomObject][ordered]@{
                ListName     = $List.Title
                FieldTitle   = $Field.Title
                InternalName = $Field.InternalName
                ID           = $Field.ID
                SchemaXml    = $Field.SchemaXml
                Required     = $Field.Required
                TypeAsString = $Field.TypeAsString
        }
    }
    }
    #Export data to CSV
    $ListFields | Export-Csv -Path $CSVFile -NoTypeInformation
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Đây là đầu ra mẫu từ tập lệnh trên:

Bằng cách truy xuất tất cả các trường, bạn có thể lấy thông tin về các cột danh sách SharePoint Online của mình và thực hiện nhiều hành động khác nhau, chẳng hạn như thêm hoặc xóa trường.

Bài viết liên quan