SharePoint Get Column Values in Few Lines
Here is the CSOM way to get the values of Selected columns from a List/Library. Change the name of the List/Library as per your needs.
For basic introduction to CSOM, check out my other blog in the CSOM section,
here
1. PowerShell CSOM
#Get Values of selected column like TypeDoc in a Document Library like SPDevBasics for a files #named SPDevBasicsJSOM
#Change the values based on your environment
$User = "admin@xxx.onmicrosoft.com"
$SiteURL = "https://xxx.sharepoint.com/sites/Assignments"
$docName="SPDevBasics"
$fileName="04.SPDevBasicsJSOM.pdf"
$fieldName="TypeDoc"
#Add references to SharePoint client assemblies
$spPath="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\"
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Search.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Taxonomy.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Publishing.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.UserProfiles.dll")
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds
$web=$Context.Web
$Context.Load($web)
$Context.ExecuteQuery()
$psLib=$context.Web.Lists.GetByTitle($docName)
$Context.Load($psLib)
$Context.ExecuteQuery()
#$psLib.Title
$files=$psLib.RootFolder.Files
$Context.Load($files)
$Context.ExecuteQuery()
#Find your File
foreach($file in $files){
if($file.Name -eq $fileName){
$allFields=$file.ListItemAllFields
$Context.Load($allFields)
$Context.ExecuteQuery()
$allFields["$($fieldName)"]
}
}
For basic introduction to CSOM, check out my other blog in the CSOM section,
here
1. PowerShell CSOM
#Get Values of selected column like TypeDoc in a Document Library like SPDevBasics for a files #named SPDevBasicsJSOM
$User = "admin@xxx.onmicrosoft.com"
$SiteURL = "https://xxx.sharepoint.com/sites/Assignments"
$docName="SPDevBasics"
$fileName="04.SPDevBasicsJSOM.pdf"
$fieldName="TypeDoc"
#Add references to SharePoint client assemblies
$spPath="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\"
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Search.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Taxonomy.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.Publishing.dll")
Add-Type -Path ($spPath+"Microsoft.SharePoint.Client.UserProfiles.dll")
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds
$web=$Context.Web
$Context.Load($web)
$Context.ExecuteQuery()
$psLib=$context.Web.Lists.GetByTitle($docName)
$Context.Load($psLib)
$Context.ExecuteQuery()
#$psLib.Title
$files=$psLib.RootFolder.Files
$Context.Load($files)
$Context.ExecuteQuery()
#Find your File
foreach($file in $files){
if($file.Name -eq $fileName){
$allFields=$file.ListItemAllFields
$Context.Load($allFields)
$Context.ExecuteQuery()
$allFields["$($fieldName)"]
}
}
Comments
Post a Comment