Forum Discussion
Adding an image web part to modern page using PNP CSOM
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!