【问题标题】:I'm trying to pull a list of items from sharepoint online using powershell csom我正在尝试使用 powershell csom 从在线共享点中提取项目列表
【发布时间】:2021-11-10 18:24:09
【问题描述】:

我可以获得列表数据 - 但它是所有列表历史记录,而不仅仅是项目的最新版本。我没有看到用于过滤旧版本项目的版本 ID 字段。有没有办法用 csom 和 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 = "XYZ"
$ListName="ABC"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#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 {
    If ($_["Status"]  = "Returned") {
        #Get the Title field value
        write-host $_.ID
        write-host $_["Title"]
    }
}

【问题讨论】:

    标签: powershell sharepoint-online csom


    【解决方案1】:

    请以管理员身份运行以下 PowerShell CSOM 脚本:

    #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://****.sharepoint.com/sites/sitename"
    $ListName="ABC"
    
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
      
    #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
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Choice'>Returned</Value></Eq></Where></Query></View>"
    
    $ListItems = $List.GetItems($Query)
    $Context.Load($ListItems)
    $Context.ExecuteQuery()
     
    Write-host "Total Number of Items:"$ListItems.count
     
    #Loop through each List Item
    $ListItems | ForEach-Object {
         #Get the Title field value
         write-host $_.ID
         write-host $_["Title"]  
         Write-host " ************* "
    }
    

    ===========================更新答案 ============ ==============

    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View><ViewFields><FieldRef Name='ID'/><FieldRef Name='Title'/></ViewFields><Query><Where><Eq><FieldRef Name='Status'/><Value Type='Choice'>Returned</Value></Eq></Where></Query></View>"
    

    查询当前列表中[Status]列值为“Returned”的项目

    【讨论】:

    • 我会试一试 - 你能突出原始脚本的变化吗?是否为您的示例站点启用了历史记录,并且这些项目在历史记录中是否有多个条目?感谢您的帮助
    • 嗨,迈克。我在初始我的答案下更新了答案。希望这可以帮助您解决问题。祝你今天过得愉快!注意:[状态]是选择栏
    • EchoDU_MSFT - 感谢您抽出宝贵时间回复此信息。我能够在您的帮助下完成查询。再次感谢
    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 2022-11-03
    • 2017-07-23
    • 2016-11-24
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    相关资源
    最近更新 更多