SOLVED

Changing a Page Banner Image inside a Modern Page using PnP

%3CLINGO-SUB%20id%3D%22lingo-sub-93529%22%20slang%3D%22en-US%22%3EChanging%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93529%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EHow%20can%20I%20change%20the%20picture%20in%20the%20banner%20on%20top%20of%20a%20Modern%20Page%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsing%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fpnp_articles%2Fmodern-experience-customizations-customize-pages%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fpnp_articles%2Fmodern-experience-customizations-customize-pages%3C%2FA%3E%20I%20managed%20to%20create%20modern%20pages%20using%20PnP.%20I%20have%20also%20managed%20to%20add%20text%20using%20the%20ClientSideText%20control.%20But%20how%20can%20I%20change%20the%20picture%20in%20the%20banner%20on%20top%20of%20the%20page%3F%20I%20have%20found%20examples%20how%20to%20add%20a%20separate%20Image%20control%2C%20but%20I%20haven't%20found%20a%20solution%20how%20to%20change%20the%20banner%20on%20top%20of%20a%20page%20(where%20also%20the%20title%20is%20displayed).%3C%2FP%3E%3CP%3EAny%20examples%20how%20to%20do%20that%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-93529%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-189052%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-189052%22%20slang%3D%22en-US%22%3E%3CP%3ERe-opening%20this%20-%20why%20when%20I%20create%20a%20new%20page%20using%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BClientSidePage%20page%20%3D%20new%20ClientSidePage(ctx)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eis%20the%26nbsp%3BLayoutWebpartsContent%20empty%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%26nbsp%3Bwaited%20for%20a%20number%20of%20minutes%2C%20tried%20publishing%20the%20page%2C%20tried%20getting%20the%20attribute%20in%20pnp%20PowerShell%20and%20it's%20still%20empty!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-182263%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-182263%22%20slang%3D%22en-US%22%3E%3CP%3ESupport%20for%20changing%20the%20Page%20Banner%20Image%20url%20of%20a%20Modern%20Page%20using%20PnP%20is%20on%20the%20todo%20list%2C%20as%20mentioned%20on%20GitHub%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-Sites-Core%2Fissues%2F1579%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-Sites-Core%2Fissues%2F1579%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102696%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102696%22%20slang%3D%22en-US%22%3ENot%20got%20my%20code%20handy%20but%20I%20believe%20you%20can%20set%20the%20bannerimageurl%20property%20on%20the%20page.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102620%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102620%22%20slang%3D%22en-US%22%3E%3CP%3EMy%20initial%20Regular%20Expression%20works%20only%20when%20banner%20is%20not%20set.%20So%20it%20will%20not%20work%20for%20the%20pages%20with%20banner.%3C%2FP%3E%3CP%3EBelow%20is%20the%20code%20to%20process%20any%20pages%3A%3C%2FP%3E%3CPRE%3Evar%20ctx%20%3D%20SP.ClientContext.get_current()%3B%20%2F%2F%20SP%20context%0Avar%20web%20%3D%20ctx.get_web()%3B%20%2F%2F%20current%20web%0Avar%20list%20%3D%20web.get_lists().getByTitle('Site%20Pages')%3B%20%2F%2F%20pages%20library%0Avar%20items%20%3D%20list.getItems(SP.CamlQuery.createAllItemsQuery())%3B%20%2F%2F%20all%20items%0Actx.load(items)%3B%20%2F%2F%20loading%20items%20from%20server...%0Actx.executeQueryAsync(function()%20%7B%0A%20%20%20%20var%20item%20%3D%20items.get_item(7)%3B%20%2F%2F%20here%20I'm%20working%20with%20single%20item%20by%20index.%20But%20you%20can%20iterate%20through%20all%20pages%20here%0A%20%20%20%20var%20layoutWebpartsContent%20%3D%20item.get_item('LayoutWebpartsContent')%3B%20%2F%2F%20getting%20content%0A%20%20%20%20console.log(layoutWebpartsContent)%3B%20%2F%2F%20let's%20display%20the%20content%0A%20%20%20%20%0A%20%20%20%20var%20dataAttrContent%20%3D%20%2Fdata-sp-controldata%3D%22(%5B%5E%22%5D%2B)%22%2Fgmi.exec(layoutWebpartsContent)%3B%20%2F%2F%20getting%20data-sp-controldata%20content%0A%20%20%20%20%0A%20%20%20%20if%20(dataAttrContent.length)%20%7B%20%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20found%20the%20attribute.%0A%20%20%20%20%20%20%20%20%2F%2F%20Let's%20unescape%20and%20parse%20it%20to%20JSON%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20content%20of%20the%20attribute%20is%20a%20'group'%20in%20RegExp%20result.%20It%20will%20be%20located%20as%20second%20entry%20(with%20index%201)%0A%20%20%20%20%20%20%20%20var%20unescaped%20%3D%20unescape(dataAttrContent%5B1%5D)%20%2F%2F%20unescape%0A%20%20%20%20%20%20%20%20%2F%2Funescaped%20%3D%20unescape(unescaped)%3B%0A%20%20%20%20%20%20%20%20var%20content%20%3D%20JSON.parse(unescaped.replace(%2F%26amp%3B%2358%3B%2Fgmi%2C%20'%3A'))%3B%20%2F%2F%20replace%20%26amp%3B%2358%3B%20with%20%3A%0A%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20changing%20imageSource%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20if%20(!content.serverProcessedContent)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20content.serverProcessedContent%20%3D%20%7B%7D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(!content.serverProcessedContent.imageSources)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20content.serverProcessedContent.imageSources%20%3D%20%7B%7D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20content.serverProcessedContent.imageSources.imageSource%20%3D%20'%2Fsites%2Fdocuments-aggregator%2FSiteAssets%2FSitePages%2F1200px-MarvelLogo.svg.png'%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20Changing%20imageSourceType%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20if%20(!content.properties)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20content.properties%20%3D%20%7B%7D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20content.properties.imageSourceType%20%3D%202%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20%2F%2F%20escaping%20back%20and%20updating%20item%0A%20%20%20%20%20%20%20%20%2F%2F%0A%20%20%20%20%20%20%20%20debugger%3B%0A%20%20%20%20%20%20%20%20var%20newContent%20%3D%20JSON.stringify(content)%3B%0A%20%20%20%20%20%20%20%20newContent%20%3D%20escape(newContent)%3B%20%2F%2F%20escaping%0A%20%20%20%20%20%20%20%20newContent%20%3D%20newContent.replace(%2F%253A%2Fgmi%2C%20'%26amp%3B%2358%3B').replace(%2F%252C%2Fgmi%2C%20'%2C')%3B%20%2F%2F%20we%20need%20to%20replace%20%253A%20(%3A)%20with%20%26amp%3B%2358%3B%20and%20%252C%20(%2C)%20with%20%2C%0A%20%20%20%20%20%20%20%20layoutWebpartsContent%20%3D%20layoutWebpartsContent.replace(dataAttrContent%5B1%5D%2C%20newContent)%3B%0A%20%20%20%20%20%20%20%20item.set_item('LayoutWebpartsContent'%2C%20layoutWebpartsContent)%3B%0A%20%20%20%20%20%20%20%20item.update()%3B%0A%20%20%20%20%20%20%20%20ctx.executeQueryAsync(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log('success')%3B%0A%20%20%20%20%20%20%20%20%7D%2C%20function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log('fail')%3B%0A%20%20%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%7D%0A%7D)%3B%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102544%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102544%22%20slang%3D%22en-US%22%3EThank%20you%20for%20the%20detailed%20hack%20%2F%20work%20around%20Aleksei!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102484%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102484%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20is%20a%20code%20in%20JavaScript%20(JSOM).%3C%2FP%3E%3CP%3EFirst%2C%20let's%20say%20you've%20added%20some%20picture%20that%20should%20be%20used%20on%20the%20page.%20For%20example%2C%20to%20Site%20Assets.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%2C%20you%20can%20run%20next%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20ctx%20%3D%20SP.ClientContext.get_current()%3B%20%2F%2F%20current%20context%0Avar%20web%20%3D%20ctx.get_web()%3B%20%2F%2F%20current%20web%0Avar%20list%20%3D%20web.get_lists().getByTitle('Site%20Pages')%3B%20%2F%2F%20getting%20Site%20Pages%20library%0Avar%20items%20%3D%20list.getItems(SP.CamlQuery.createAllItemsQuery())%3B%20%2F%2F%20all%20pages%0Actx.load(items)%3B%20%2F%2F%20loading%20items%0Actx.executeQueryAsync(function()%20%7B%0A%20%20%20%20var%20item%20%3D%20items.get_item(6)%3B%20%2F%2F%20in%20my%20case%20I'll%20work%20with%20some%20page%20under%20index%206%0A%20%20%20%20%2F%2F%0A%20%20%20%20%2F%2F%20LayoutWebpartsContent%20field%20contains%20settings%20for%20the%20page%20layout%0A%20%20%20%20%2F%2F%0A%20%20%20%20var%20layoutWebpartsContent%20%3D%20item.get_item('LayoutWebpartsContent')%3B%0A%20%20%20%20console.log(layoutWebpartsContent)%3B%20%2F%2F%20let's%20look%20at%20it%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20providing%20image%20source%0A%20%20%20%20layoutWebpartsContent%20%3D%20layoutWebpartsContent.replace(%2F%2522imageSources%2522%26amp%3B%2358%3B%257B%5B%5E%257D%5D*%257D%2Fgmi%2C%20'%2522imageSources%2522%26amp%3B%2358%3B%257B%2522imageSource%2522%26amp%3B%2358%3B%2522server-relative-url-to-img-goes-here%2522%257D')%3B%0A%20%20%20%20%2F%2F%20changing%20imageSourceType%20from%204%20to%204%20if%20needed%0A%20%20%20%20layoutWebpartsContent%20%3D%20layoutWebpartsContent.replace(%2F%2522imageSourceType%2522%26amp%3B%2358%3B4%2Fgmi%2C%20'%2522imageSourceType%2522%26amp%3B%2358%3B2')%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20saving%20changes%0A%20%20%20%20item.set_item('LayoutWebpartsContent'%2C%20layoutWebpartsContent)%0A%20%20%20%20item.update()%3B%0A%20%20%20%20ctx.executeQueryAsync()%3B%0A%7D)%3B%0A%0A%3C%2FPRE%3E%3CP%3ESo%2C%20basically%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Eyou%20can%20get%20a%20page%20as%20a%20list%20item%3C%2FLI%3E%3CLI%3Echange%20LayoutWebpartsContent%20property%3C%2FLI%3E%3CLI%3Eupdate%20list%20item%3C%2FLI%3E%3C%2FUL%3E%3CP%3EIn%20LayoutWebpartsContent%20first%20thing%20to%20change%20is%20imageSource.%20It%20may%20be%20absent%20(for%20new%20created%20page)%20or%20contain%20server%20relative%20url%20to%20current%20page.%3C%2FP%3E%3CP%3EThe%20second%20thing%20to%20change%20is%20imageSourceType.%20For%20new%20created%20page%20without%20image%20it's%20equal%20to%204%20and%20we%20need%20to%20change%20it%20to%202.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOf%20course%2C%20it's%20more%20like%20a%20hack.%20But%20it%20works%20for%20your%20scenario.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-102324%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-102324%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EAny%20idea%20how%20to%20realize%20this%20programmatically%20%2F%20scripted%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F369%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F5670%22%20target%3D%22_blank%22%3E%40Bert%20Jansen%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%3F%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-96980%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-96980%22%20slang%3D%22en-US%22%3E%3CP%3EDo%20you%20know%20what%20size%20image%20it%20supports%20or%20reccomended%20size%3F%20Some%20images%20dont%20show%20but%20it%20appears%20smaller%20do.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-94173%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-94173%22%20slang%3D%22en-US%22%3E%3CP%3EAdding%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F5670%22%20target%3D%22_blank%22%3E%40Bert%20Jansen%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93645%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93645%22%20slang%3D%22en-US%22%3E%3CP%3EAdding%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F369%22%20target%3D%22_blank%22%3E%40Vesa%20Juvonen%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93639%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93639%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20your%20reply%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F65712%22%20target%3D%22_blank%22%3E%40Suhail%20Jamaldeen%3C%2FA%3E.%20Sadly%20it%20doesn't%20anwer%20my%20question.%3C%2FP%3E%3CP%3EThe%20method%20you%20described%2C%20is%20what%20I%20use%20when%20I%20manually%20update%20Modern%20Page%20using%20the%20SharePoint%20User%20Interface.%20But%20I'm%20looking%20for%20a%20way%20to%20change%20a%20Page%20Banner%20using%20PnP%2C%20so%20using%20a%20programmatically%20%2F%20scripted%20way%20to%20realize%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93614%22%20slang%3D%22en-US%22%3ERe%3A%20Changing%20a%20Page%20Banner%20Image%20inside%20a%20Modern%20Page%20using%20PnP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93614%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20you%20go%20to%20the%20edit%20mode%20of%20the%20page%3B%20You%20will%20find%20%26nbsp%3B%22Add%20Image%22%20icon%20on%20the%20left%2C%20just%20click%20on%20it%20and%20add%20the%20image%20from%20your%20site%20or%20upload%20it.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheck%20the%20attached%20screenshot.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

How can I change the picture in the banner on top of a Modern Page?

 

Using https://msdn.microsoft.com/en-us/pnp_articles/modern-experience-customizations-customize-pages I managed to create modern pages using PnP. I have also managed to add text using the ClientSideText control. But how can I change the picture in the banner on top of the page? I have found examples how to add a separate Image control, but I haven't found a solution how to change the banner on top of a page (where also the title is displayed).

Any examples how to do that?

12 Replies
Highlighted

Hi,

 

When you go to the edit mode of the page; You will find  "Add Image" icon on the left, just click on it and add the image from your site or upload it. 

 

Check the attached screenshot. 

 

Highlighted

Thanks for your reply @Suhail Jamaldeen. Sadly it doesn't anwer my question.

The method you described, is what I use when I manually update Modern Page using the SharePoint User Interface. But I'm looking for a way to change a Page Banner using PnP, so using a programmatically / scripted way to realize this.

 

Any ideas?

Highlighted
Highlighted

Do you know what size image it supports or reccomended size? Some images dont show but it appears smaller do. 

Highlighted

Any idea how to realize this programmatically / scripted @Vesa Juvonen, @Bert Jansen, @Erwin van Hunen?  

Highlighted

Here is a code in JavaScript (JSOM).

First, let's say you've added some picture that should be used on the page. For example, to Site Assets.

 

Then, you can run next code:

 

var ctx = SP.ClientContext.get_current(); // current context
var web = ctx.get_web(); // current web
var list = web.get_lists().getByTitle('Site Pages'); // getting Site Pages library
var items = list.getItems(SP.CamlQuery.createAllItemsQuery()); // all pages
ctx.load(items); // loading items
ctx.executeQueryAsync(function() {
    var item = items.get_item(6); // in my case I'll work with some page under index 6
    //
    // LayoutWebpartsContent field contains settings for the page layout
    //
    var layoutWebpartsContent = item.get_item('LayoutWebpartsContent');
    console.log(layoutWebpartsContent); // let's look at it
    
    // providing image source
    layoutWebpartsContent = layoutWebpartsContent.replace(/%22imageSources%22:%7B[^%7D]*%7D/gmi, '%22imageSources%22:%7B%22imageSource%22:%22server-relative-url-to-img-goes-here%22%7D');
    // changing imageSourceType from 4 to 4 if needed
    layoutWebpartsContent = layoutWebpartsContent.replace(/%22imageSourceType%22:4/gmi, '%22imageSourceType%22:2');
    
    // saving changes
    item.set_item('LayoutWebpartsContent', layoutWebpartsContent)
    item.update();
    ctx.executeQueryAsync();
});

So, basically

 

  • you can get a page as a list item
  • change LayoutWebpartsContent property
  • update list item

In LayoutWebpartsContent first thing to change is imageSource. It may be absent (for new created page) or contain server relative url to current page.

The second thing to change is imageSourceType. For new created page without image it's equal to 4 and we need to change it to 2.

 

Of course, it's more like a hack. But it works for your scenario.

Highlighted
Thank you for the detailed hack / work around Aleksei!
Highlighted
Best Response
Solution

My initial Regular Expression works only when banner is not set. So it will not work for the pages with banner.

Below is the code to process any pages:

var ctx = SP.ClientContext.get_current(); // SP context
var web = ctx.get_web(); // current web
var list = web.get_lists().getByTitle('Site Pages'); // pages library
var items = list.getItems(SP.CamlQuery.createAllItemsQuery()); // all items
ctx.load(items); // loading items from server...
ctx.executeQueryAsync(function() {
    var item = items.get_item(7); // here I'm working with single item by index. But you can iterate through all pages here
    var layoutWebpartsContent = item.get_item('LayoutWebpartsContent'); // getting content
    console.log(layoutWebpartsContent); // let's display the content
    
    var dataAttrContent = /data-sp-controldata="([^"]+)"/gmi.exec(layoutWebpartsContent); // getting data-sp-controldata content
    
    if (dataAttrContent.length) { 
        // we found the attribute.
        // Let's unescape and parse it to JSON
        // the content of the attribute is a 'group' in RegExp result. It will be located as second entry (with index 1)
        var unescaped = unescape(dataAttrContent[1]) // unescape
        //unescaped = unescape(unescaped);
        var content = JSON.parse(unescaped.replace(/:/gmi, ':')); // replace : with :

        //
        // changing imageSource
        //
        if (!content.serverProcessedContent) {
            content.serverProcessedContent = {};
        }
        if (!content.serverProcessedContent.imageSources) {
            content.serverProcessedContent.imageSources = {};
        }
        content.serverProcessedContent.imageSources.imageSource = '/sites/documents-aggregator/SiteAssets/SitePages/1200px-MarvelLogo.svg.png';

        //
        // Changing imageSourceType
        //
        if (!content.properties) {
            content.properties = {};
        }
        content.properties.imageSourceType = 2;

        //
        // escaping back and updating item
        //
        debugger;
        var newContent = JSON.stringify(content);
        newContent = escape(newContent); // escaping
        newContent = newContent.replace(/%3A/gmi, ':').replace(/%2C/gmi, ','); // we need to replace %3A (:) with : and %2C (,) with ,
        layoutWebpartsContent = layoutWebpartsContent.replace(dataAttrContent[1], newContent);
        item.set_item('LayoutWebpartsContent', layoutWebpartsContent);
        item.update();
        ctx.executeQueryAsync(function() {
            console.log('success');
        }, function() {
            console.log('fail');
        });
    }
});
Highlighted
Not got my code handy but I believe you can set the bannerimageurl property on the page.
Highlighted

Support for changing the Page Banner Image url of a Modern Page using PnP is on the todo list, as mentioned on GitHub: https://github.com/SharePoint/PnP-Sites-Core/issues/1579

Highlighted

Re-opening this - why when I create a new page using 

 ClientSidePage page = new ClientSidePage(ctx);

 

is the LayoutWebpartsContent empty?

 

I've waited for a number of minutes, tried publishing the page, tried getting the attribute in pnp PowerShell and it's still empty!