【问题标题】:Specifying view for PS Script to Pull Sharepoint Enterprise List Data为 PS 脚本指定视图以提取 Sharepoint 企业列表数据
【发布时间】:2020-04-15 00:27:31
【问题描述】:

我正在尝试创建一个 PowerShell 脚本,以从我们的企业 Sharepoint 站点中提取大量报告文件的数据。 使用 getlistitems 我可以从我们的列表中提取相当一部分所需的数据,但它似乎只从每个列表的默认视图中返回项目。我想将它设置为从“所有项目”视图中提取,但我很难得到它。请参阅下面的代码。

$serverurl = "https:linkhiddenforprivacy/_vti_bin/Lists.asmx"
$xml = New-WebServiceProxy -Uri $serverurl -UseDefaultCredential
$dslist = $xml.GetListCollection()
$overalllist = $dslist.ChildNodes
foreach($listitem in $overalllist) {
    try {

        $xmlDoc = new-object System.Xml.XmlDocument
        $query = $xmlDoc.CreateElement("Query")
        $viewFields = $xmlDoc.CreateElement("ViewFields")
        $queryOptions = $xmlDoc.CreateElement("QueryOptions")
        $queryOptions.InnerXml = "<ViewAttributes Scope='RecursiveAll' IncludeMandatoryColumns='FALSE' />"
        $rowLimit = "10000"
        $view = $overalllist.views["All Items"]

        $listitems = $xml.getlistitems($listitem.id, $view ,$null, $null, $rowLimit, $queryOptions, $null).data.row 
        foreach($child in $listitems) {
            if($child.ows_DocIcon -eq "rdl"){
                $vs = $xml.GetVersionCollection($listitem.ID, $child.ows_ID, "_UIVersionString")
                $childlib = $child.ows_FileRef
                $childname = $child.ows_LinkFileName 
                [string]$childowner = $child.ows_ReportOwner
                $childcategory = $child.ows_ReportCategory
                $childdescription = $child.ows_ReportDescription
                $childstatus = $child.ows_ReportStatus
                [PSCustomObject]@{
                        'Doctype' = $child.ows_DocIcon
                        'Library' = $childlib.substring($childlib.indexof('#') + 1, $childlib.lastindexof('/') - $childlib.indexof('#') - 1)
                        'Report' = $childname
                        'Owner' = $childOwner.Substring($childowner.indexof('#') + 1, $childowner.Length - $childowner.IndexOf('#') - 1)
                        'Category' = $childCategory
                        'Description' = $childDescription
                        'Status' = $childStatus
                        'Version' = $vs.Version[0]._UIVersionString
                        'Childlib' = $childlib
                } | Export-Csv output.csv -Append -NoTypeInformation
            }

我已经尝试了一些变体,但没有任何成功。我是 Powershell 的新手,因此我们将不胜感激。

【问题讨论】:

    标签: powershell sharepoint view listitem


    【解决方案1】:

    在 PowerShell 中使用 CSOM,您可以轻松获取基于视图的项目。

    示例脚本:

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    
    $siteURL = "http://sp:12001"   
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
    
    try{  
        $lists = $ctx.web.Lists  
        $list = $lists.GetByTitle("MyList")  
        $view = $list.Views.GetByTitle("All Items")
        $ctx.load($view)        
        $ctx.executeQuery()
        $query=New-Object Microsoft.SharePoint.Client.CamlQuery
        $query.ViewXml = $view.ViewQuery;
        $listItems = $list.GetItems($query)  
        $ctx.load($listItems)        
        $ctx.executeQuery()  
        foreach($listItem in $listItems)  
        {  
            Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"]  
        }  
    }  
    catch{  
        write-host "$($_.Exception.Message)" -foregroundcolor red  
    }
    

    20132016online获取CSOM SDK

    如果您尝试在 SharePoint 中使用 PowerShell 搜索 CSOM,您可以获得大量演示。

    【讨论】:

    • 谢谢,很遗憾,由于组策略/安全问题,我无法安装其他软件。我只能使用示例中提供的代码之后的方法。
    • 您可以在本地安装 CSOM SDK,然后将 dll 复制到目标服务器并使用它。
    • 如果你想试试lists.asmx,可以查看c-sharpcorner.com/UploadFile/anavijai/…,现在更流行使用CSOM或pnp powershell。
    猜你喜欢
    • 2011-06-10
    • 2022-01-09
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    相关资源
    最近更新 更多