Forum Discussion
Nigel Witherdin
Jan 28, 2018Iron Contributor
Adding an image web part to modern page using PNP CSOM
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-on...
Nigel Witherdin
Jan 28, 2018Iron Contributor
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);
peni4142
Jul 30, 2019Copper Contributor
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.