Could not able to fetch web object while getting list and listitem using Sharepoint CSOM Assemblies in Powershell

Hirdesh Baghel 45 Reputation points
2024-04-03T11:47:45.1866667+00:00

Hi
I am using below code for getting list and listitem using CSOM assemblies code but I am not able to get the required data as web property is coming as null.

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

#Get Credentials to connect
$Cred= Get-Credential
$SiteURL = "SiteURL"

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

#Get The Web
$Web =  $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Get Web Title
Write-host $Ctx.Web.Title

 

$SiteList="Sites"
$SList = $Ctx.Web.Lists.GetByTitle($SiteList)
$SitelistItem = $SList.GetItemById($ItemID)
Write-host $SitelistItem

Kindly help in this, Thanks in advance.

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2024-04-08T06:38:32.56+00:00

    Hi @Hirdesh Baghel,

    Per my test, you could use following script to get list item

    #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"
    $ListName="Projects"
     
    $UserName="******@crescent.com"
    $Password ="Password goes here"
      
    #Setup Credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
      
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
       
    #Get the List
    $List = $Context.web.Lists.GetByTitle($ListName)
     
    #sharepoint online get list items powershell
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
    $Context.Load($ListItems)
    $Context.ExecuteQuery()      
     
    write-host "Total Number of List Items found:"$ListItems.Count
     
    #Loop through each item
    $ListItems | ForEach-Object {
        #Get the Title field value
        write-host $_["Title"]
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Hirdesh Baghel 45 Reputation points
    2024-04-08T06:46:28.02+00:00

    Hi @RaytheonXie_MSFT
    Thanks for the response
    Yes I am using the same script only but I am unable to get the web property from the context, that is a major roadblock for executing this snippet to get list item.

    if you have any idea regarding the issue please let me know.

    Thanks and Regards,
    Hirdesh


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.