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

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

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

使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表


我的一位客户最近希望我将项目从一个 SharePoint Online 列表复制到另一个列表。 PnP PowerShell 是对此的最佳解决方案之一。在本教程中,我将向您展示如何使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

这是开发人员在处理 SharePoint Online 列表中的项目时非常常见的要求。没有人为此使用第三方工具,因为它很昂贵。因此,我们可以使用 PnP PowerShell。

在这里,我创建了一个 SharePoint Online 网站,其中有一个自定义列表,其中包含以下三列:

  • 标题
  • 描述
  • 数量

我还向 SharePoint 列表添加了批量项目。该列表包含 5000 多个项目。您可以看到该列表的样子:

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

我已在另一个 SharePoint Online 网站中创建了相同的列表,现在我们将从源列表复制到另一个 SharePoint 网站中的目标列表。

按照以下步骤使用 PnP PowerShell 将项目从 SharePoint Online 中的一个列表复制到另一个列表。

  • 连接到 SharePoint Online 网站:首先,您需要使用 PnP PowerShell 连接到 SharePoint Online 网站。使用以下命令进行连接:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive

此命令将提示您使用 SharePoint Online 凭据登录。

  • 从源列表中获取项目:接下来,从源列表中检索项目。将“SourceList”替换为源列表的名称:
$sourceListItems = Get-PnPListItem -List "SourceList"
  • 将项目复制到目标列表:现在,循环遍历源列表中的每个项目并将其添加到目标列表。将“DestinationList”替换为您的目的地列表的名称:
foreach ($item in $sourceListItems) {
    Add-PnPListItem -List "DestinationList" -Values @{
        Title = $item["Title"]
        Description = $item["Description"]
        Quantity = $item["Quantity"]
    }
}

在这里,您可以看到用于将项目从一个 SharePoint Online 列表复制到另一个的完整 PnP PowerShell 脚本。

# Connect to SharePoint Online
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

$sourceListItems = Get-PnPListItem -List "LargeInventoryList"
foreach ($item in $sourceListItems) {
    Add-PnPListItem -List "DestinationLargeInventoryList" -Values @{
        Title = $item["Title"]
        Description = $item["Description"]
        Quantity = $item["Quantity"]
    }
}

我执行了上面的 PowerShell 脚本,它将项目从源列表复制到目标列表。

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

您还可以查看包含复制项目的目标 SharePoint 列表。

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

这是使用 PnP PowerShell 将项目从一个列表复制到同一 SharePoint Online 站点中的另一个列表的方法。

使用 PnP PowerShell 将列表项从一个 SharePoint 站点复制到另一个 SharePoint 站点

在上面的示例中,源列表和目标列表都显示在同一个 SharePoint 网站上。

有时,您可能会需要将项目从一个 SharePoint 网站复制到另一 SharePoint Online 网站。对于这些情况,您只需修改脚本,如下所示:

# Connect to the Source SharePoint Site
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Retrieve Items from the Source List
$sourceListItems = Get-PnPListItem -List "LargeInventoryList"

# Connect to the Destination SharePoint Site
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerShellFAQs" -Interactive

# Copy Items to the Destination List
foreach ($item in $sourceListItems) {
    Add-PnPListItem -List "LargeInventoryList" -Values @{
        Title = $item["Title"]
        Description = $item["Description"]
        Quantity = $item["Quantity"]
    }
}

您可以看到下面的屏幕截图;执行上述脚本后,它将项目列表从源 SharePoint 网站复制到目标 SharePoint 网站。

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

使用 PnP PowerShell 将 SharePoint 项目从一个列表复制到另一个列表(批处理)

下面是一个 PnP PowerShell 脚本,用于使用 -PageSize 500 参数将列表项从一个 SharePoint 列表复制到另一个 SharePoint 列表,以有效处理较大的列表。

# Define variables
$sourceSiteUrl = "https://szg52.sharepoint.com/sites/PowerPlatformFAQs"
$targetSiteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
$sourceListName = "LargeInventoryList"
$targetListName = "LargeInventoryList"

# Connect to the source SharePoint site
Connect-PnPOnline -Url $sourceSiteUrl -Interactive

# Get list items from the source list with paging
$items = Get-PnPListItem -List $sourceListName -PageSize 500 -Fields "Title", "Description", "Quantity"

# Connect to the target SharePoint site
Connect-PnPOnline -Url $targetSiteUrl -Interactive

# Iterate through each item and add it to the target list
foreach ($item in $items) {
    $itemValues = @{
        "Title" = $item["Title"]
        "Description" = $item["Description"]
        "Quantity" = $item["Quantity"]
    }

    Add-PnPListItem -List $targetListName -Values $itemValues
}

此脚本通过以 500 为批次的项目分页来确保高效处理大型列表。

我使用 VS code 执行了上述 PowerShell 脚本;您可以在下面的屏幕截图中看到输出。

[玩转系统] 使用 PnP PowerShell 将项目从一个列表复制到 SharePoint Online 中的另一个列表

这是使用 PnP PowerShell 批处理将项目从一个大型 SharePoint 列表复制到另一个的方法。

结论

在本教程中,我解释了如何使用 PnP PowerShell 将项目从一个 SharePoint 列表复制到另一个 SharePoint 列表。我还解释了如何将列表项从一个 SharePoint 网站复制到另一个 SharePoint 网站。我们还了解了如何使用 PnP PowerShell 将项目从大型 SharePoint 列表复制到另一个列表。

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

取消回复欢迎 发表评论:

关灯