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

[玩转系统] 使用 Powershell 和 PnP 将 Sharepoint 列表项复制到另一个列表

作者:精品下载站 日期:2024-12-14 18:46:31 浏览:13 分类:玩电脑

使用 Powershell 和 PnP 将 Sharepoint 列表项复制到另一个列表


您可以使用 Powershell 和 PnP 轻松将 Sharepoint 列表项复制到另一个列表。

首先,使用 PnP-Online 连接到源 Sharepoint 站点:

#Create credential object
$cred = Get-Credential

#Import the Skype for Business Online PS session
Connect-PnPOnline -url $siteUrl -Credentials $cred

获取列表项

现在我们需要检索要复制的项目列表:

# Get source list items
$listItems = Get-PnPListItem -List 'ListName' -Fields "Project","Title","Description","Address"

将项目复制到目标列表

现在是时候复制数据了。首先,连接到目标站点(如果列表位于另一个 Sharepoint 站点)

#Connect to destination site using the connector script
Connect-PnPOnline -url 'http://conto.sharepoint.com/site'

foreach($item in $listItems) {
        #Create object
	$itemVal = @{
		'ProjectName' = $item['ProjectName']
		'Title' = $item['Title']
		'Desciption' = $item['Desciption']
		'Address' = $item['Address']
	}
	Add-PnPListItem -List 'newlist' -Values $itemVal -ContentType "Item"
}

一些重要的注意事项(我花了几个小时才弄清楚)

在 Sharepoint 中,列表列可以具有与打开 Sharepoint 时看到的不同的内部名称。要获取所有可用列表名称,请运行以下 cmdlet

# Get all the lists from the connected Sharepoint site
Get-pnplist

# Show fields from the selected list
Get-pnpfield -list 'listname'

如果列表中的一列包含超链接,那么您可以通过以下方式从中获取内容:

$url = $item['HyperlinkFieldName'].url
$desc = $item['HyperlinkFieldName'].description

$itemVal = @{
  'NewLinkField' = "$url, $desc"
  'Title' = $item['Title']
}

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

取消回复欢迎 发表评论:

关灯