使用 PowerShell 和 JSON 添加指向快速链接 Web 部件的链接
Add links to a Quick Links web part using PowerShell and JSON
我正在尝试将 link 添加到现代 SharePoint 网站上的快速链接 Web 部件,为此我正在使用 PowerShell 和 JSON。我已将 Web 部件作为 JSON 文件获取,并已使用 Get-Content
命令访问它。
快速链接 Web 部件的 JSON 如下所示:
{
"controlType": 3,
"id": "a9ed7796-5545-4623-a943-5be42762691d",
"position": {
"zoneIndex": 1,
"sectionIndex": 1,
"controlIndex": 1,
"layoutIndex": 1
},
"webPartId": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"webPartData": {
"id": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"instanceId": "a9ed7796-5545-4623-a943-5be42762691d",
"title": "Quick links",
"description": "Add links to important documents and pages.",
"serverProcessedContent": {
"htmlStrings": {},
"searchablePlainTexts": {
"items[0].title": "Yahoo",
"items[1].title": "Google"
},
"imageSources": {
"items[0].rawPreviewImageUrl": "https://s.yimg.com/cv/apiv2/social/images/yahoo_default_logo.png"
},
"links": {
"baseUrl": "https://bbpocoutlook.sharepoint.com/sites/tl23",
"items[0].sourceItem.url": "https://yahoo.com",
"items[1].sourceItem.url": "https://google.com"
},
"componentDependencies": {
"layoutComponentId": "706e33c8-af37-4e7b-9d22-6e5694d92a6f"
}
},
"dataVersion": "2.2",
"properties": {
"items": [
{
"sourceItem": {
"itemType": 2,
"fileExtension": "",
"progId": ""
},
"thumbnailType": 3,
"id": 2,
"description": "",
"altText": ""
},
{
"sourceItem": {
"itemType": 2,
"fileExtension": "",
"progId": ""
},
"thumbnailType": 3,
"id": 1,
"description": "",
"altText": ""
}
],
"isMigrated": true,
"layoutId": "List",
"shouldShowThumbnail": true,
"buttonLayoutOptions": {
"showDescription": false,
"buttonTreatment": 2,
"iconPositionType": 2,
"textAlignmentVertical": 2,
"textAlignmentHorizontal": 2,
"linesOfText": 2
},
"listLayoutOptions": {
"showDescription": false,
"showIcon": true
},
"waffleLayoutOptions": {
"iconSize": 1,
"onlyShowThumbnail": false
},
"hideWebPartWhenEmpty": true,
"dataProviderId": "QuickLinks",
"webId": "b5fdf80c-54ce-410f-a50d-910ea2e33250",
"siteId": "0c8f4c9a-71e6-4fc0-8355-9b52f0a7eb3a"
}
},
"emphasis": {},
"reservedHeight": 132,
"reservedWidth": 744,
"addedFromPersistedData": true
}
如何向 Web 部件添加新项目并向其添加 link?
例如,我如何通过 PowerShell 添加一个 link 到 Whosebug 到快速 link web 部件?
您可以读取 JSON 并使用 ConvertFrom-Json
将其转换为具有属性的对象。然后你可以添加一个快速 link 通过做:
# if the json is in a file:
$json = Get-Content -Path 'TheJsonFile.json' | ConvertFrom-Json
$plainTexts = $json.webPartData.serverProcessedContent.searchablePlainTexts
$plainTexts | Add-Member -MemberType NoteProperty -Name 'items[2].title' -Value 'Stack Overflow'
$links = $json.webPartData.serverProcessedContent.links
$links | Add-Member -MemberType NoteProperty -Name 'items[2].sourceItem.url' -Value 'https://whosebug.com/'
接下来,将更改后的对象转换回 JSON:
$json | ConvertTo-Json -Depth 5
并可能用 Set-Content
将其保存到磁盘
注意:PowerShell 不会创建 'pretty' json。缩进真的很可怕。如果你想让结果json看起来更好看,你可以使用我前段时间写的这个函数:
function Format-Json {
<#
.SYNOPSIS
Prettifies JSON output.
.DESCRIPTION
Reformats a JSON string so the output looks better than what ConvertTo-Json outputs.
.PARAMETER Json
Required: [string] The JSON text to prettify.
.PARAMETER Minify
Optional: Returns the json string compressed.
.PARAMETER Indentation
Optional: The number of spaces (1..1024) to use for indentation. Defaults to 2.
.PARAMETER AsArray
Optional: If set, the output will be in the form of a string array, otherwise a single string is output.
.EXAMPLE
$json | ConvertTo-Json | Format-Json -Indentation 4
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]$Json,
[ValidateRange(1, 1024)]
[int]$Indentation = 2,
[switch]$AsArray
)
# If the input JSON text has been created with ConvertTo-Json -Compress
# then we first need to reconvert it without compression
if ($Json -notmatch '\r?\n') {
$Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100
}
$indent = 0
$regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'
$result = $Json -split '\r?\n' |
ForEach-Object {
# If the line contains a ] or } character,
# we need to decrement the indentation level unless it is inside quotes.
if ($_ -match "[}\]]$regexUnlessQuoted") {
$indent = [Math]::Max($indent - $Indentation, 0)
}
# Replace all colon-space combinations by ": " unless it is inside quotes.
$line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')
# If the line contains a [ or { character,
# we need to increment the indentation level unless it is inside quotes.
if ($_ -match "[\{\[]$regexUnlessQuoted") {
$indent += $Indentation
}
# ConvertTo-Json returns all single-quote characters as Unicode Apostrophs \u0027
# see:
$line # -replace '\u0027', "'"
}
if ($AsArray) { return $result }
return $result -Join [Environment]::NewLine
}
然后像这样转换更改后的对象:
$json | ConvertTo-Json -Depth 5 | Format-Json
结果:
{
"controlType": 3,
"id": "a9ed7796-5545-4623-a943-5be42762691d",
"position": {
"zoneIndex": 1,
"sectionIndex": 1,
"controlIndex": 1,
"layoutIndex": 1
},
"webPartId": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"webPartData": {
"id": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"instanceId": "a9ed7796-5545-4623-a943-5be42762691d",
"title": "Quick links",
"description": "Add links to important documents and pages.",
"serverProcessedContent": {
"htmlStrings": {
},
"searchablePlainTexts": {
"items[0].title": "Yahoo",
"items[1].title": "Google",
"items[2].title": "Stack Overflow"
},
"imageSources": {
"items[0].rawPreviewImageUrl": "https://s.yimg.com/cv/apiv2/social/images/yahoo_default_logo.png"
},
"links": {
"baseUrl": "https://bbpocoutlook.sharepoint.com/sites/tl23",
"items[0].sourceItem.url": "https://yahoo.com",
"items[1].sourceItem.url": "https://google.com",
"items[2].sourceItem.url": "https://whosebug.com/"
},
"componentDependencies": {
"layoutComponentId": "706e33c8-af37-4e7b-9d22-6e5694d92a6f"
}
},
"dataVersion": "2.2",
"properties": {
"items": [
{
"sourceItem": "@{itemType=2; fileExtension=; progId=}",
"thumbnailType": 3,
"id": 2,
"description": "",
"altText": ""
},
{
"sourceItem": "@{itemType=2; fileExtension=; progId=}",
"thumbnailType": 3,
"id": 1,
"description": "",
"altText": ""
}
],
"isMigrated": true,
"layoutId": "List",
"shouldShowThumbnail": true,
"buttonLayoutOptions": {
"showDescription": false,
"buttonTreatment": 2,
"iconPositionType": 2,
"textAlignmentVertical": 2,
"textAlignmentHorizontal": 2,
"linesOfText": 2
},
"listLayoutOptions": {
"showDescription": false,
"showIcon": true
},
"waffleLayoutOptions": {
"iconSize": 1,
"onlyShowThumbnail": false
},
"hideWebPartWhenEmpty": true,
"dataProviderId": "QuickLinks",
"webId": "b5fdf80c-54ce-410f-a50d-910ea2e33250",
"siteId": "0c8f4c9a-71e6-4fc0-8355-9b52f0a7eb3a"
}
},
"emphasis": {
},
"reservedHeight": 132,
"reservedWidth": 744,
"addedFromPersistedData": true
}
我正在尝试将 link 添加到现代 SharePoint 网站上的快速链接 Web 部件,为此我正在使用 PowerShell 和 JSON。我已将 Web 部件作为 JSON 文件获取,并已使用 Get-Content
命令访问它。
快速链接 Web 部件的 JSON 如下所示:
{
"controlType": 3,
"id": "a9ed7796-5545-4623-a943-5be42762691d",
"position": {
"zoneIndex": 1,
"sectionIndex": 1,
"controlIndex": 1,
"layoutIndex": 1
},
"webPartId": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"webPartData": {
"id": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd",
"instanceId": "a9ed7796-5545-4623-a943-5be42762691d",
"title": "Quick links",
"description": "Add links to important documents and pages.",
"serverProcessedContent": {
"htmlStrings": {},
"searchablePlainTexts": {
"items[0].title": "Yahoo",
"items[1].title": "Google"
},
"imageSources": {
"items[0].rawPreviewImageUrl": "https://s.yimg.com/cv/apiv2/social/images/yahoo_default_logo.png"
},
"links": {
"baseUrl": "https://bbpocoutlook.sharepoint.com/sites/tl23",
"items[0].sourceItem.url": "https://yahoo.com",
"items[1].sourceItem.url": "https://google.com"
},
"componentDependencies": {
"layoutComponentId": "706e33c8-af37-4e7b-9d22-6e5694d92a6f"
}
},
"dataVersion": "2.2",
"properties": {
"items": [
{
"sourceItem": {
"itemType": 2,
"fileExtension": "",
"progId": ""
},
"thumbnailType": 3,
"id": 2,
"description": "",
"altText": ""
},
{
"sourceItem": {
"itemType": 2,
"fileExtension": "",
"progId": ""
},
"thumbnailType": 3,
"id": 1,
"description": "",
"altText": ""
}
],
"isMigrated": true,
"layoutId": "List",
"shouldShowThumbnail": true,
"buttonLayoutOptions": {
"showDescription": false,
"buttonTreatment": 2,
"iconPositionType": 2,
"textAlignmentVertical": 2,
"textAlignmentHorizontal": 2,
"linesOfText": 2
},
"listLayoutOptions": {
"showDescription": false,
"showIcon": true
},
"waffleLayoutOptions": {
"iconSize": 1,
"onlyShowThumbnail": false
},
"hideWebPartWhenEmpty": true,
"dataProviderId": "QuickLinks",
"webId": "b5fdf80c-54ce-410f-a50d-910ea2e33250",
"siteId": "0c8f4c9a-71e6-4fc0-8355-9b52f0a7eb3a"
}
},
"emphasis": {},
"reservedHeight": 132,
"reservedWidth": 744,
"addedFromPersistedData": true
}
如何向 Web 部件添加新项目并向其添加 link?
例如,我如何通过 PowerShell 添加一个 link 到 Whosebug 到快速 link web 部件?
您可以读取 JSON 并使用 ConvertFrom-Json
将其转换为具有属性的对象。然后你可以添加一个快速 link 通过做:
# if the json is in a file:
$json = Get-Content -Path 'TheJsonFile.json' | ConvertFrom-Json
$plainTexts = $json.webPartData.serverProcessedContent.searchablePlainTexts
$plainTexts | Add-Member -MemberType NoteProperty -Name 'items[2].title' -Value 'Stack Overflow'
$links = $json.webPartData.serverProcessedContent.links
$links | Add-Member -MemberType NoteProperty -Name 'items[2].sourceItem.url' -Value 'https://whosebug.com/'
接下来,将更改后的对象转换回 JSON:
$json | ConvertTo-Json -Depth 5
并可能用 Set-Content
注意:PowerShell 不会创建 'pretty' json。缩进真的很可怕。如果你想让结果json看起来更好看,你可以使用我前段时间写的这个函数:
function Format-Json {
<#
.SYNOPSIS
Prettifies JSON output.
.DESCRIPTION
Reformats a JSON string so the output looks better than what ConvertTo-Json outputs.
.PARAMETER Json
Required: [string] The JSON text to prettify.
.PARAMETER Minify
Optional: Returns the json string compressed.
.PARAMETER Indentation
Optional: The number of spaces (1..1024) to use for indentation. Defaults to 2.
.PARAMETER AsArray
Optional: If set, the output will be in the form of a string array, otherwise a single string is output.
.EXAMPLE
$json | ConvertTo-Json | Format-Json -Indentation 4
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[string]$Json,
[ValidateRange(1, 1024)]
[int]$Indentation = 2,
[switch]$AsArray
)
# If the input JSON text has been created with ConvertTo-Json -Compress
# then we first need to reconvert it without compression
if ($Json -notmatch '\r?\n') {
$Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100
}
$indent = 0
$regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'
$result = $Json -split '\r?\n' |
ForEach-Object {
# If the line contains a ] or } character,
# we need to decrement the indentation level unless it is inside quotes.
if ($_ -match "[}\]]$regexUnlessQuoted") {
$indent = [Math]::Max($indent - $Indentation, 0)
}
# Replace all colon-space combinations by ": " unless it is inside quotes.
$line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')
# If the line contains a [ or { character,
# we need to increment the indentation level unless it is inside quotes.
if ($_ -match "[\{\[]$regexUnlessQuoted") {
$indent += $Indentation
}
# ConvertTo-Json returns all single-quote characters as Unicode Apostrophs \u0027
# see:
$line # -replace '\u0027', "'"
}
if ($AsArray) { return $result }
return $result -Join [Environment]::NewLine
}
然后像这样转换更改后的对象:
$json | ConvertTo-Json -Depth 5 | Format-Json
结果:
{ "controlType": 3, "id": "a9ed7796-5545-4623-a943-5be42762691d", "position": { "zoneIndex": 1, "sectionIndex": 1, "controlIndex": 1, "layoutIndex": 1 }, "webPartId": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd", "webPartData": { "id": "c70391ea-0b10-4ee9-b2b4-006d3fcad0cd", "instanceId": "a9ed7796-5545-4623-a943-5be42762691d", "title": "Quick links", "description": "Add links to important documents and pages.", "serverProcessedContent": { "htmlStrings": { }, "searchablePlainTexts": { "items[0].title": "Yahoo", "items[1].title": "Google", "items[2].title": "Stack Overflow" }, "imageSources": { "items[0].rawPreviewImageUrl": "https://s.yimg.com/cv/apiv2/social/images/yahoo_default_logo.png" }, "links": { "baseUrl": "https://bbpocoutlook.sharepoint.com/sites/tl23", "items[0].sourceItem.url": "https://yahoo.com", "items[1].sourceItem.url": "https://google.com", "items[2].sourceItem.url": "https://whosebug.com/" }, "componentDependencies": { "layoutComponentId": "706e33c8-af37-4e7b-9d22-6e5694d92a6f" } }, "dataVersion": "2.2", "properties": { "items": [ { "sourceItem": "@{itemType=2; fileExtension=; progId=}", "thumbnailType": 3, "id": 2, "description": "", "altText": "" }, { "sourceItem": "@{itemType=2; fileExtension=; progId=}", "thumbnailType": 3, "id": 1, "description": "", "altText": "" } ], "isMigrated": true, "layoutId": "List", "shouldShowThumbnail": true, "buttonLayoutOptions": { "showDescription": false, "buttonTreatment": 2, "iconPositionType": 2, "textAlignmentVertical": 2, "textAlignmentHorizontal": 2, "linesOfText": 2 }, "listLayoutOptions": { "showDescription": false, "showIcon": true }, "waffleLayoutOptions": { "iconSize": 1, "onlyShowThumbnail": false }, "hideWebPartWhenEmpty": true, "dataProviderId": "QuickLinks", "webId": "b5fdf80c-54ce-410f-a50d-910ea2e33250", "siteId": "0c8f4c9a-71e6-4fc0-8355-9b52f0a7eb3a" } }, "emphasis": { }, "reservedHeight": 132, "reservedWidth": 744, "addedFromPersistedData": true }