0

I have a classic page in SharePoint Online. It's a standard homepage from a teamsite in a classic web and I want to add a list webpart to it.

When the web is created, the Home.aspx has three webparts, a Promoted Links webpart ("Get started with your site"), a Documents webpart and a Newsfeed webpart. This is all standard.

I need to remove the Promoted Links webpart and the Documents and run this:

# Remove the Promoted Links webpart:
$wpId = (Get-PnPWebPart -ServerRelativePageUrl "$relLeafURL/SitePages/Home.aspx" -Connection $conn | Where-Object {$_.Webpart.Title -eq "Get started with your site" }).Id
Remove-PnPWebPart -ServerRelativePageUrl "$relLeafURL/SitePages/Home.aspx" -Identity $wpId -Connection $conn

# Remove the Documents webpart:
$wpId = (Get-PnPWebPart -ServerRelativePageUrl "$relLeafURL/SitePages/Home.aspx" -Connection $conn | Where-Object {$_.Webpart.Title -eq "Documents"}).Id
Remove-PnPWebPart -ServerRelativePageUrl "$relLeafURL/SitePages/Home.aspx" -Identity $wpId -Connection $conn

The two webparts are gone from Home.aspx, only the Newsfeed remains. Excellent.

Now I need to add a custom list webpart called Contacts. I get the XML from the AllItems view and add it to the page:

# Add webpart Contacts:
$webPartXml = Get-PnPWebPartXml -Identity Contacts -ServerRelativePageUrl "$relLeafURL/Lists/Contacts/AllItems.aspx" -Connection $conn
Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "$relLeafURL/SitePages/Home.aspx" -XML $webPartXml -ZoneId "WPZ" -ZoneIndex 2 -Connection $conn

The webpart is indeed added to the page, but when I edit the page it's not there. Only the Newsfeed webpart which was created by default shows up. This means that I can't change any settings for the webpart, since it doesn't show in edit mode.

The webpart is listed if I run Get-PnPWebPart and also shows when I add ?contents=1 to the URL. Just not when I edit the page.

I think for some reason my webpart doesn't end up in a webpart zone, but I can't figure out how to do this. Can anyone please help?

enter image description here

1 Answer 1

0

Try adding the list web part this way.

1.Create the Web Part XML:

  • Use the following template for a .dwp file:

    <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v2"> <metaData> <type name="Microsoft.SharePoint.WebPartPages.ListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /> <importErrorMessage>Cannot import this Web Part.</importErrorMessage> </metaData> <data> <properties> <property name="ListName" type="string">{list-guid}</property> <property name="Title" type="string">Your List Title</property> </properties> </data> </webPart> </webParts>

  • Replace {list-guid} with the actual GUID of your list. You can get the GUID by navigating to the list settings and copying it from the URL.

2.Save the XML as a .dwp File:

  • Save the above XML content to a file with a .dwp extension, for example, C:\path\to\your\webpart.dwp.

3.Add the Web Part to the Page:

  • Use the Add-PnPWebPartToWebPartPage cmdlet to add the web part to your page:

    $page = "YourPage.aspx" $dwpPath = "C:\\path\\to\\your\\webpart.dwp" Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/yoursite/SitePages/$page" -Path $dwpPath -ZoneId "Main" -ZoneIndex 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.