SharePoint 2010 Powershell中两个版本之间的日期差异

问题描述 投票:0回答:1

我有一个具有批准工作流程的文档库,更改文档并创建次要版本,直到批准文档然后创建主要版本为止。

如果被拒绝,它将以次要版本继续。简而言之,我们可以在批准任务列表中有20个项目,与从版本1到版本2的说法相关。

所以我需要的是一个脚本,它将查询每个主要版本的另一个列表。

到目前为止,我将获得所有主要版本以及这些版本的创建时间。想法是获取一个文档的版本之间的日期时间差,并查询在两个日期之间创建的列表。

到目前为止,我有以下内容:

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

$WebURL="http://test/sites/PSMF"
$ModuleList = "Library"
#Get the Web and List
$Web = Get-SPWeb $WebURL

function GetMetadata {
    $list  = $web.Lists.TryGetList($ModuleList)
    $items = $list.items
     foreach($item in $items)
     {

        $versionHistory = $item.Versions

        foreach($version in $versionHistory)
        {
           #if major version 
           if($version.VersionLabel -match "[0-9]+.0")
           {
                $vNo = $version.VersionLabel
                $versioncreated = $version.created
                Write-Host " Version Created: $vNo" -ForegroundColor Green
                #get difference between two verisons?
                #pass two dates to fileter and return from GetApproval?
            }
        }
     }
 }


function GetApprovals($from, $To)
{
}

GetMetadata


powershell sharepoint sharepoint-2010
1个回答
0
投票

[注意,我没有改变您从“ ModuleList”中获取项目的方式,但是通常不安全的做法是直接在Items对象上直接访问SPList属性,除非您始终知道列表中会有一个很小的列表项目数。一旦列表增加到几百个项目,访问该属性的速度就会大大降低,并导致环境中其他操作的性能下降;一旦列表超过5000个项目,访问该属性实际上将引发错误。最好的做法是始终将Microsoft.SharePoint.SPQuerySPList.GetItems()一起使用,即使您试图获取所有项目,因为这允许您的代码以块(或页面)的形式获取项目。您可以在GetApprovals函数中看到我的使用方式,如果您想获取所有项目,则只需要带有Where子句的查询即可。

此脚本应该为您提供所需的内容:

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

$WebURL="http://test/sites/PSMF"
$ModuleList = "Library"
$ApprovalListName = "PutNameOfApprovalListHere"
#Get the Web and List
$Web = Get-SPWeb $WebURL

function GetMetadata {
    $list  = $web.Lists.TryGetList($ModuleList)
    $items = $list.items

    foreach($item in $items)
    {
        $versionHistory = $item.Versions
        $prevVersionDate = $null

        foreach($version in $versionHistory)
        {
           #if major version 
           if($version.VersionLabel -match "[0-9]+.0")
           {
                $vNo = $version.VersionLabel
                $versioncreated = $version.created
                Write-Host " Version Created: $vNo" -ForegroundColor Green

                if ($prevVersionDate -ne $null)
                {
                    $approvals = GetApprovals -from $prevVersionDate -to $versioncreated
                    # do something with the approvals we just found?
                }

                $prevVersionDate = $versioncreated
            }
        }
    }
 }


function GetApprovals($from, $To)
{
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $spQuery.ViewAttributes = "Scope='Recursive'";
    $spQuery.Query = '<Query><Where><And><Geq><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">' + $from + '</Value></Geq><Leq><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">' + $to + '</Value></Leq></And></Where></Query>'
    $spQuery.RowLimit = 200
    $approvalList = $web.Lists.TryGetList($ApprovalListName)
    $approvals = New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPListItem]

    do
    {
        $approvalItems = $approvalList.GetItems($spquery)
        $spQuery.ListItemCollectionPosition = $approvalItems.ListItemCollectionPosition
        foreach($approvalItem in $approvalItems)
        {
            $approvals.Add($approvalItem);
        }    
    }while ($spQuery.ListItemCollectionPosition -ne $null)

    return $approvals;
}

GetMetadata
© www.soinside.com 2019 - 2024. All rights reserved.