0

I am trying to add links to a Quick Links web part on a modern SharePoint site, to do this I am using PowerShell and JSON. I have obtained the web part as a JSON file and have accessed it using the Get-Contentcommand.

The JSON looks like this:

{
        "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
    }

How would I add a new item to the web part and add a link to it?

0

There are specific PnP PowerShell cmdlets for "ClientSide WebParts" (i.e. Modern WebParts). If you are trying to save modified json properties back to an existing WebPart, use Set-PnPClientSideWebPart, if you are creating a whole new webPart with those properties, use Add-PnPClientSideWebPart

If you are trying to manipulate the JSON first in PowerShell before saving it in SharePoint, adding objects to PowerShell is a bit of a pain, and made even more so since this properties you want to modify are essentially parsed text stored in the keys of the json. First, you will need to update the "serverProcessedContent". The schema here is odd, in that it almost resembles and array, but is actually just object properties, so you will need to parse out the "index" from the serverProcessedContent.links.keys -- if you are starting from scratch to create a new webpart, then just start with index=0. Then you can add a new property to serverProcessedContent.links with the name set to "items[yournewindexhere].sourceItem.url" and the value set to the new quicklink's URL. To set a display text for the url, use the same new index value and add "items[yournewindexhere].title" to the serverProcessedContent.searchablePlainTexts with the value set to the display text you want to be hyperlinked. Similarly, if you want to set an image for the quicklink, add "items[0].rawPreviewImageUrl" to the serverProcessedContent.imageSources.

Then finally, you can execute Set-PnPClientSideWebPart to save the updated json back to the webpart.

| improve this answer | |
  • Thank you for your response willman, I got it working eventually! – Liam Cosgrove Feb 6 at 16:29
0

If this can help, I finally found the right minimum data to use to have the expected result:

Add-PnPClientSideWebPart -Page $page -DefaultWebPartType "QuickLinks" -WebPartProperties @{title="QuickLinks 2";items=@(@{sourceItem=@{url="https://www.google.com";itemType=2;fileExtension="";progId=""};title="Google";thumbnailType=3;id=2};@{sourceItem=@{url="https://www.youtube.com";itemType=2;fileExtension="";progId=""};title="Youtube";thumbnailType=3;id=1});hideWebPartWhenEmpty="true"}  

Here the result :

enter image description here

This code can be improved to add and manage display options.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.