Adding an image web part to modern page using PNP CSOM

Iron Contributor

Hi All,

 

Trying to programmatically add an image web part to a modern page using PnP CSOM as per: https://dev.office.com/blogs/programatically-creating-and-updating-modern-pages-in-sharepoint-online.

 

To do this I have added an image web part to a workbench page and configured it to display the image desired, then clicked Web Part Data, and then inspected that popup with developer tools to be able to copy and paste the settings:

[
    {
        "controlType": 3,
        "displayMode": 2,
        "id": "dd0b35fd-c04f-4c9c-9254-ca12dc803b19",
        "position": {
            "controlIndex": 1,
            "sectionIndex": 1,
            "zoneIndex": 1
        },
        "webPartData": {
            "dataVersion": "1.8",
            "description": "Show an image on your page.",
            "id": "d1d91016-032f-456d-98a4-721247c305e8",
            "instanceId": "dd0b35fd-c04f-4c9c-9254-ca12dc803b19",
            "properties": {
                "altText": "",
                "fixAspectRatio": false,
                "imageSourceType": 2,
                "listId": "{A4514897-0699-40F7-8E33-C3A7D2A8FAC4}",
                "siteId": "b3e3a3d3-73d0-4cec-b62f-999537700bbb",
                "uniqueId": "4674d419-c4dd-4bcf-a8e5-9f9890a3fc95",
                "webId": "bcd0fe30-de28-4a0a-a5cd-0bd7b91e87d8"
            },
            "serverProcessedContent": {
                "htmlStrings": {},
                "imageSources": {
                    "imageSource": "/sites/sandpit_dev/News/OnPremArticleImages/sharepoint_for_business%20ARTICLE.jpg"
                },
                "links": {},
                "searchablePlainTexts": {}
            },
            "title": "Image"
        },
        "webPartId": "d1d91016-032f-456d-98a4-721247c305e8"
    }
]

From this, I made the following code:

var imageWebPart = p.InstantiateDefaultWebPart(DefaultClientSideWebParts.Image);
imageWebPart.Properties["imageSourceType"] = 2;
imageWebPart.Properties["imageSource"] = "";
imageWebPart.Properties["captionText"] = "This is a test";
imageWebPart.Properties["fileName"] = "";
imageWebPart.Properties["siteId"] = "b3e3a3d3-73d0-4cec-b62f-999537700bbb";
imageWebPart.Properties["webId"] = "bcd0fe30-de28-4a0a-a5cd-0bd7b91e87d8";
imageWebPart.Properties["listId"] = "{A4514897-0699-40F7-8E33-C3A7D2A8FAC4}";
imageWebPart.Properties["uniqueId"] = "4674d419-c4dd-4bcf-a8e5-9f9890a3fc95";
imageWebPart.Properties["imgWidth"] = "1002";
imageWebPart.Properties["imgHeight"] = "469";
imageWebPart.Properties["fixAspectRatio"] = false;
p.AddControl(imageWebPart, -1);

However all I end up with is an image web part that has no image configured - however it is displaying the "This is a test" caption.

 

I have also tried by removing the { } from the list id value, but no joy.

 

Am I missing something here?

 

Thanks,

 

Nigel

 

3 Replies

Ok - so (rather obviously) setting the "imageSource" property to the URL of the image resolved the issue, but what I dont understand was that the PnP webcast did not seem to need to set this - instead just setting the siteId, webId and uniqueId was enough?

 

For anyone interested, here is the code:

ClientSidePage p = ClientSidePage.Load(cc, "Templatepage.aspx");
        p.LayoutType = ClientSidePageLayoutType.Home;

        var imageWebPart = p.InstantiateDefaultWebPart(DefaultClientSideWebParts.Image);
        imageWebPart.Properties["imageSourceType"] = 2;
        imageWebPart.Properties["imageSource"] = "/sites/sandpit_dev/News/OnPremArticleImages/sharepoint_for_business%20ARTICLE.jpg";
        imageWebPart.Properties["captionText"] = "This is a test";
        imageWebPart.Properties["fileName"] = "";
        imageWebPart.Properties["siteId"] = "b3e3a3d3-73d0-4cec-b62f-999537700bbb";
        imageWebPart.Properties["webId"] = "bcd0fe30-de28-4a0a-a5cd-0bd7b91e87d8";
        imageWebPart.Properties["listId"] = "{A4514897-0699-40F7-8E33-C3A7D2A8FAC4}";
        imageWebPart.Properties["uniqueId"] = "4674d419-c4dd-4bcf-a8e5-9f9890a3fc95";
        imageWebPart.Properties["fixAspectRatio"] = false;

p.AddControl(imageWebPart, -1);

I'm guessing there was a change in January. My code which simply modified the uniqueId property worked January 3rd, 2018, to replace the image in an existing image web part on a modern page. When I finally got back to it yesterday, it no longer worked. I'm still not able to modify the image on an existing image web part as I did previously, but at least now--with your tip--I can add a new image web part to the page with an updated image. Thanks for the tip!

 

imageWebPart.Properties["imageSource"] = "Hic sunt dracones of Microsoft"

 

or 

 

imageWebPart.Properties["imageSource"] = false

 

would also work.

As long the variable is set, the webpart will work.