{"id":7284,"date":"2024-12-30T14:38:33","date_gmt":"2024-12-30T14:38:33","guid":{"rendered":"https:\/\/softreetechnology.com\/blog\/?p=7284"},"modified":"2024-12-30T14:38:34","modified_gmt":"2024-12-30T14:38:34","slug":"add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/","title":{"rendered":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell"},"content":{"rendered":"\n<p>In this blog, we are going to retrieve all the webparts present in a modern site page. Also, we will see how to add different web parts and delete a web part from a modern site page using PnP PowerShell.<\/p>\n\n\n\n<p>First, we need to connect to the site. To perform the connection, add the following lines.<\/p>\n\n\n\n<p>$siteURL = Read-Host &#8220;Provide site url&#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>Connect-PnPOnline -Url $siteURL&nbsp;&nbsp;<\/p>\n\n\n\n<p>#Executing this line will ask for credentials. Provide use name and password to connect.&nbsp;<\/p>\n\n\n\n<p>$page=Get-PnPClientSidePage Identity &#8220;ModernWebPage.aspx&#8221; #Get the page on which you are going to perform add and remove web parts.&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>Add different Webparts<\/strong><\/p>\n\n\n\n<p>To add a text editor web part,<\/p>\n\n\n\n<p>Add-PnPClientSideText -Page $page -Text &#8220;Welcomes To SharePoint&#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>To add a list view web part,<\/p>\n\n\n\n<p>Add-PnPClientSideWebPart -Page $page -DefaultWebPartType \u201cList\u201d -WebPartProperties @{selectedListId=&#8221;609f95e4-7022-417d-a57f-693673f7eff9&#8243;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>You can see two web parts are added successfully in the modern site page.&nbsp;<\/p>\n\n\n\n<p>Retrieve all web parts used in the modern site pages.<\/p>\n\n\n\n<p>$webParts = $page.Controls&nbsp;&nbsp;<\/p>\n\n\n\n<p>#if there are more than one webparts&nbsp;&nbsp;<\/p>\n\n\n\n<p>foreach($webpart in $webparts) {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Write &#8211; Host &#8220;WebPart Id &#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$webpart.InstanceId&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Write &#8211; Host &#8220;Title &#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$webpart.Title&nbsp;&nbsp;<\/p>\n\n\n\n<p>}&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>Remove a webpart<\/strong><\/p>\n\n\n\n<p>Remove-PnPClientSideComponent -Page $page -InstanceId 0c6a1475-c5e8-414e-9e6d-c90c3b5c01b7&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>To remove web parts, we need to provide web part instance id that we have already got in PowerShell window output while retrieving the web parts. After executing the above command, it will open a dialog box to confirm web part delete. Click on &#8220;yes&#8221; to delete the web part.<\/p>\n\n\n\n<p>$siteURL = Read &#8211; Host &#8220;Please provide site url&#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>try {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Connect &#8211; PnPOnline &#8211; Url $siteURL&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$page = Get &#8211; PnPClientSidePage &#8211; Identity &#8220;ModernWebPage.aspx&#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;#Add webparts in modern page&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Add &#8211; PnPClientSideText &#8211; Page $page &#8211; Text &#8220;Welcomes To SharePoint&#8221; &#8211; Section 2 &#8211; Column 1&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Add &#8211; PnPClientSideWebPart &#8211; Page $page &#8211; DefaultWebPartType\u201c List\u201d &#8211; Section 2 &#8211; Column 1 &#8211; WebPartProperties @ {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;selectedListId = &#8220;609f95e4-7022-417d-a57f-693673f7eff9&#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;#Retrieve webparts from modern page&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$webParts = $page.Controls&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;foreach($webpart in $webparts) {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Write &#8211; Host &#8220;WebPart Id &#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$webpart.InstanceId&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Write &#8211; Host &#8220;Title &#8221;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$webpart.Title&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;#Remove webparts from modern page&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Remove &#8211; PnPClientSideComponent &#8211; Page $page &#8211; InstanceId 0 c6a1475 &#8211; c5e8 &#8211; 414e-9 e6d &#8211; c90c3b5c01b7&nbsp;&nbsp;<\/p>\n\n\n\n<p>} catch {&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Write &#8211; Host &#8211; ForegroundColor Red &#8216;Error &#8216;, &#8216;:&#8217;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$Error[0].ToString();&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;sleep 10&nbsp;&nbsp;<\/p>\n\n\n\n<p>}&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going to retrieve all the webparts present in a modern site page. Also, we will see how to add different web parts and delete a web part from a modern site page using PnP PowerShell. First, we need to connect to the site. To perform the connection, add the following lines. $siteURL = Read-Host &#8220;Provide [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[298],"tags":[208,341,56,159],"class_list":["post-7284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pnp-powershell","tag-pnp-powershell","tag-power-automate","tag-sharepoint","tag-sharepoint-online"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"In this blog, we are going to retrieve all the webparts present in a modern site page. Also, we will see how to add different web parts and delete a web part from a modern site page using PnP PowerShell. First, we need to connect to the site. To perform the connection, add the following lines. $siteURL = Read-Host &#8220;Provide [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-30T14:38:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-30T14:38:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/","og_locale":"en_US","og_type":"article","og_title":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell - Softree Technology","og_description":"In this blog, we are going to retrieve all the webparts present in a modern site page. Also, we will see how to add different web parts and delete a web part from a modern site page using PnP PowerShell. First, we need to connect to the site. To perform the connection, add the following lines. $siteURL = Read-Host &#8220;Provide [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/","og_site_name":"Softree Technology","article_published_time":"2024-12-30T14:38:33+00:00","article_modified_time":"2024-12-30T14:38:34+00:00","og_image":[{"width":1080,"height":1080,"url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell","datePublished":"2024-12-30T14:38:33+00:00","dateModified":"2024-12-30T14:38:34+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/"},"wordCount":599,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#primaryimage"},"thumbnailUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png","keywords":["PnP PowerShell","Power Automate","SharePoint","SharePoint Online"],"articleSection":["PnP PowerShell"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/","url":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/","name":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#primaryimage"},"thumbnailUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png","datePublished":"2024-12-30T14:38:33+00:00","dateModified":"2024-12-30T14:38:34+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#primaryimage","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2024\/12\/we-build-an-amazing-website.png","width":1080,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/pnp-powershell\/add-remove-and-get-all-web-parts-from-modern-site-page-using-pnp-powershell-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Add, Remove, And Get All Web Parts From Modern Site Page Using PnP PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=7284"}],"version-history":[{"count":1,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7284\/revisions"}],"predecessor-version":[{"id":7286,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/7284\/revisions\/7286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media\/7285"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=7284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=7284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=7284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}