当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 将大型列表项目存档到另一个列表

作者:精品下载站 日期:2024-12-14 21:17:09 浏览:15 分类:玩电脑

SharePoint Online:使用 PowerShell 将大型列表项目存档到另一个列表


要求:通过将旧项目移动到新列表来归档大型列表以提高性能。

如何在 SharePoint Online 中存档列表项目?

在管理 SharePoint Online 列表时,您可能需要不时归档旧的或未使用的项目。我们如何在 SharePoint Online 中存档旧列表而不删除它们?幸运的是,使用 SharePoint 的内置功能可以轻松完成此过程。事实证明,PowerShell 还可以在 SharePoint Online 中存档列表。本指南将引导您完成在 SharePoint Online 中归档列表项所需的步骤。步骤如下:

  1. 从源列表中为存档创建新列表
  2. 将项目从源列表移动到存档列表

要创建存档列表,请将现有列表另存为不带内容的模板,然后创建新列表进行存档。您还可以通过转到 SharePoint Online 中的“网站内容>>新建>>列表>>来自现有列表”功能来克隆列表结构。

要使用 Web 浏览器界面从源列表中移动列表项,请使用“网站内容和结构”页面在 SharePoint Online 中移动列表项。您可以通过以下 URL 访问它:https://tenant.sharepoint.com/_layouts/15/sitemanager.aspx,例如 https://crescent.sharepoint.com/sites/marketing/_layouts/15/sitemanager .aspx

[玩转系统] SharePoint Online:使用 PowerShell 将大型列表项目存档到另一个列表

PowerShell 在 SharePoint Online 中存档列表项目:

在 SharePoint Online 中归档列表项是一个有用的过程,可帮助您保持网站井井有条并提高性能。通过将旧的或不常用的项目移动到存档位置,您可以帮助简化您的网站并减少混乱。让我们看看如何使用 PowerShell 在 SharePoint Online 中存档列表项。

假设您已准备好存档列表,此脚本会将 SharePoint Online 列表中 90 多天前创建的所有列表项移动到另一个列表。


#Function to copy attachments between list items
Function Copy-SPOAttachments($SourceItem, $TargetItem)
{
    Try {
        #Get All Attachments from Source
        $Attachments = Get-PnPProperty -ClientObject $SourceItem -Property "AttachmentFiles"
        $Attachments | ForEach-Object {
        #Download the Attachment to Temp
        $File  = Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $env:TEMP -AsFile -force

        #Add Attachment to Target List Item
        $FileStream = New-Object IO.FileStream(($env:TEMP+"\"+$_.FileName),[System.IO.FileMode]::Open)  
        $AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
        $AttachmentInfo.FileName = $_.FileName
        $AttachmentInfo.ContentStream = $FileStream
        $AttachFile = $TargetItem.AttachmentFiles.add($AttachmentInfo)
        $Context.ExecuteQuery()    
    
        #Delete the Temporary File
        Remove-Item -Path $env:TEMP$($_.FileName) -Force
        }
    }
    Catch {
        write-host -f Red "Error Copying Attachments:" $_.Exception.Message
    }
}

#Function to Archive items from one list to another
Function Archive-SPOListItems()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SourceListName,
        [Parameter(Mandatory=$true)] [string] $TargetListName
    )
    Try {
        #Get All Items from the Source List in batches 
        Write-Progress -Activity "Reading Source..." -Status "Getting Items from Source List. Please wait..."
        $SourceListItems = Get-PnPListItem -List $SourceListName -PageSize 500
         Write-host "Total Number of Items Found:"$SourceListItems.count

        #Filter List Items from the source - More than 90 days old!
        $TimeStamp = (Get-Date).AddDays(-90)
        $FilteredSourceListItems  = $SourceListItems | Where {$_["Created"] -Lt $TimeStamp}
        Write-Progress -Activity "Filtering List:" -Status "Filtering Items from Source List, Please wait..."
        $FilteredSourceListItemsCount= $FilteredSourceListItems.count
        Write-host "Total Number of Items After Filter:"$FilteredSourceListItemsCount

        #Get fields to Update from the Source List - Skip Read only, hidden fields, content type and attachments
        $SourceListFields = Get-PnPField -List $SourceListName | Where { (-Not ($_.ReadOnlyField)) -and (-Not ($_.Hidden)) -and ($_.InternalName -ne  "ContentType") -and ($_.InternalName -ne  "Attachments") }
    
        #Loop through each item in the source and Get column values, add them to target
        [int]$Counter = 1
        ForEach($SourceItem in $FilteredSourceListItems)
        {  
            $ItemValue = @{}
            #Map each field from source list to target list
            Foreach($SourceField in $SourceListFields)
            {
                #Check if the Field value is not Null
                If($SourceItem[$SourceField.InternalName] -ne $Null)
                {
                    #Handle Special Fields
                    $FieldType  = $SourceField.TypeAsString

                    If($FieldType -eq "User" -or $FieldType -eq "UserMulti" -or $FieldType -eq "Lookup" -or $FieldType -eq "LookupMulti") #People Picker or Lookup Field
                    {
                        $LookupIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.LookupID.ToString()}
                        $ItemValue.add($SourceField.InternalName,$LookupIDs)
                    }
                    ElseIf($FieldType -eq "URL") #Hyperlink
                    {
                        $URL = $SourceItem[$SourceField.InternalName].URL
                        $Description  = $SourceItem[$SourceField.InternalName].Description
                        $ItemValue.add($SourceField.InternalName,"$URL, $Description")
                    }
                    ElseIf($FieldType -eq "TaxonomyFieldType" -or $FieldType -eq "TaxonomyFieldTypeMulti") #MMS
                    {
                        $TermGUIDs = $SourceItem[$SourceField.InternalName] | ForEach-Object { $_.TermGuid.ToString()}                    
                        $ItemValue.add($SourceField.InternalName,$TermGUIDs)
                    }
                    Else
                    {
                        #Get Source Field Value and add to Hashtable
                        $ItemValue.add($SourceField.InternalName,$SourceItem[$SourceField.InternalName])
                    }
                }
            }

            #Copy Created by, Modified by, Created, Modified Metadata values
            $ItemValue.add("Created", $SourceItem["Created"])
            $ItemValue.add("Modified", $SourceItem["Modified"])
            $ItemValue.add("Author", $SourceItem["Author"].Email)
            $ItemValue.add("Editor", $SourceItem["Editor"].Email)

            Write-Progress -Activity "Copying List Items:" -Status "Copying Item ID '$($SourceItem.Id)' from Source List ($($Counter) of $($FilteredSourceListItemsCount))" -PercentComplete (($Counter / $FilteredSourceListItemsCount) * 100)
        
            #Copy column value from source to target
            $NewItem = Add-PnPListItem -List $TargetListName -Values $ItemValue

            #Copy Attachments
            Copy-SPOAttachments -SourceItem $SourceItem -TargetItem $NewItem

            #Delete the Item in the Source List
            Remove-PnPListItem -List $SourceListName -Identity ($SourceItem.Id) -Recycle -Force

            Write-Host "Archived Item '$($SourceItem.Id)' from Source to Target List ($($Counter) of $($FilteredSourceListItemsCount))"
            $Counter++
        }
    }
    Catch {
        Write-host -f Red "Error:" $_.Exception.Message 
    }
}

#Connect to PnP Online
Connect-PnPOnline -Url "https://crescent.sharepoint.com/sites/Projects/" -Interactive
$Context = Get-PnPContext

#Call the Function to Copy List Items between Lists
Archive-SPOListItems -SourceListName "Projects" -TargetListName "ProjectsArchive"

您可以按需运行脚本或将其作为计划任务的一部分运行。

总之,使用 PowerShell 在 SharePoint Online 中归档列表项是保持网站井井有条的快速有效方法。通过使用本教程中提供的脚本,您可以自动执行将旧的或不常用的项目移动到存档位置的过程,从而提高站点性能并减少混乱。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯