Iteration through SharePoint lists do not work in PowerShell or LinqPad

oscarogge 15 Reputation points
2023-02-13T08:00:51.3966667+00:00

PowerShellIterate

PowershellIterate2

Hello, I am experiencing issues when trying to iterate through a list in SharePoint. It is not about an incorrect disposal of the web object as most online searches would suggest. I've found a few results that seem to describe the same problem, but no relevant answers.

This issue occurs in both PowerShell and LinqPad, on all servers.

You can call list/items via the API.

You can still use "list.GetItemById()". But not if you have called "list.Items" on the same list object first.

If you fetch the list and call e.g. "list.Fields" you get results. However, if you call "list.Items" first and then "list.Fields", you get null on fields.

If you try to print the list in PowerShell (with just "$list"), you see that almost all properties printed after "items" are null. This is also true for example of SchemaXml

It has been tested to delete 4 views from the list which looked to be corrupt, but this has not made any difference. I have also gone through each item in the list using a script (iterated in a normal for-loop with "getItemById"), but could not find any element that seems corrupt or abnormal.

Has anyone else experienced this issue before?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,671 Reputation points
    2023-02-13T16:57:49.25+00:00

    Hello there,

    I would suggest you raise this to the Microsft team for further investigation. You can also open a ticket.

    Get a dump and share it through the feedback hub.

    You can raise feedback to the Microsoft team. The Feedback Hub app lets you tell Microsoft about any problems you run into https://support.microsoft.com/en-us/windows/send-feedback-to-microsoft-with-the-feedback-hub-app-f59187f8-8739-22d6-ba93-f66612949332

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  2. RaytheonXie_MSFT 40,096 Reputation points Microsoft External Staff
    2023-02-16T07:29:32.07+00:00

    Hi @oscarogge ,

    You need to load items when you are using csom. Please refer to following script

    #Variables for Processing
    $SiteUrl = "https://crescent.sharepoint.com/sites/xxx"
    $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.


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.