PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

Add-PnPClientSideWebPart Error

Open robbert-vanandel opened this issue 6 years ago • 29 comments

###Notice: many issues / bugs reported are actually related to the PnP Core Library which is used behind the scenes. Consider carefully where to report an issue:###

  1. Are you using Apply-SPOProvisioningTemplate or Get-SPOProvisioningTemplate? The issue is most likely related to the Provisioning Engine. The Provisioning engine is not located in the PowerShell repo. Please report the issue here: https://github.com/officedev/PnP-Sites-Core/issues.
  2. Is the issue related to the cmdlet itself, its parameters, the syntax, or do you suspect it is the code of the cmdlet that is causing the issue? Then please continue reporting the issue in this repo.
  3. If you think that the functionality might be related to the underlying libraries that the cmdlet is calling (We realize that that might be difficult to determine), please first double check the code of the cmdlet, which can be found here: https://github.com/OfficeDev/PnP-PowerShell/tree/master/Commands. If related to the cmdlet, continue reporting the issue here, otherwise report the issue at https://github.com/officedev/PnP-Sites-Core/issues

Reporting an Issue or Missing Feature

This is an issue.

Expected behavior

Add-PnPClientSideWebPart should allow me to add a web part to a SharePoint site's homepage created when an Office 365 group is created.

Actual behavior

When I run Add-PnPClientSideWebPart -DefaultWebPartType List -page "Home" -WebPartProperties $options -Section 1 -Column 1 -order 2; I get an error

Add-PnPClientSideWebPart : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index`

When I use Get-PnPClientSidePage -identity Home, I get a ClientSidePage object but there are no sections. This despite seeing the default web parts on the site's home page. When I try adding a new section using Add-PnPClientSidePageSection, I can successfully add the web part, but the page only contains the web part and nothing else. Version history for the page does not allow me to go back to the original page. It's almost like this code doesn't recognize that there's a page there.

Steps to reproduce behavior

$cred = get-credential
connect-pnponline -Url https://mytenant.sharepoint.com/sites/WebPartTest2 -Credentials $cred;
$web = get-pnpweb;

$page = get-pnpclientsidepage -Identity "Home.aspx";
Add-PnPClientSidePageSection -Page "Home.aspx" -Order 1 -SectionTemplate TwoColumn;

# Engagement Information
$list = get-pnplist -Identity "Engagement Information";
$options = @{"isDocumentLibrary"=$false;
  "selectedListId"=$list.id;
  "listTitle"="Engagement Information";
  "selectedListUrl"= $web.ServerRelativeUrl + "/lists/Engagement Information" ;
  "webRelativeListUrl"="/Lists/Engagement Information";
  "webpartHeightKey"=4
 };
Add-PnPClientSideWebPart -DefaultWebPartType List -page $page -WebPartProperties $options ---Section 1 -Column 1 -order 1;

$page does appear to be right PS Z:> $page.PageListItem Id Title GUID
-- ----- ----
1 Home 72db2ecb-612d-4734-80ac-285945a81c5e

However, none of the page's controls or sections are appearing. $page.controls.count shows 0. $page.sections.count shows 0.

Which version of the PnP-PowerShell Cmdlets are you using?

  • [ ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [x] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

I'm running version 2.25.1804.1

How did you install the PnP-PowerShell Cmdlets?

  • [ ] MSI Installed downloaded from GitHub
  • [x] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means

robbert-vanandel avatar Jun 29 '18 15:06 robbert-vanandel

I have the same issue. Tried setting section/column/order to various combinations. Didn't work.

RamaKaryam avatar Nov 10 '18 01:11 RamaKaryam

Same here.

dmdaviesTM avatar Jan 24 '19 23:01 dmdaviesTM

Home page is a "special" case as by default the home page's HTML is written as empty string in the site pages list item...once you do a change and populate the home page the html is populated and your scenario should start working.

jansenbe avatar Jan 25 '19 02:01 jansenbe

Same issue...

Add-PnPClientSideWebPart : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

ravinleague avatar Mar 15 '19 01:03 ravinleague

Same issue currently, very critical needs to be resolved asap, any suggestions??

ghost avatar Nov 25 '20 23:11 ghost

If it's urgent, try @jansenbe 's suggestion. Manually edit the page, then the cmdlet should work.

ToddKlindt avatar Nov 25 '20 23:11 ToddKlindt

@ToddKlindt Thanks for the quick reply but the script is being used in a migration of around 300 items. Doing anything manually from the UI would be too time consuming

ghost avatar Nov 25 '20 23:11 ghost

With this being all community driven, we can't really make demands. If someone knows how to fix it and wants to dig in, they will. If not, there's not much we can do. :(

ToddKlindt avatar Nov 25 '20 23:11 ToddKlindt

Yeah I know, all good man, thanks for your time - doing some more research now

ghost avatar Nov 25 '20 23:11 ghost

If you find a fix or workaround, don't forget us folks in here. :)

ToddKlindt avatar Nov 26 '20 00:11 ToddKlindt

I'll be sure to keep ya posted, waiting on a colleague who has more experience

ghost avatar Nov 26 '20 00:11 ghost

I may have done something to fix this. Please spare me a few minutes to dig out the script..wont be long

ravinleague avatar Nov 26 '20 00:11 ravinleague

@ravinleague That would be amazing thank you, still searching on our end.

ghost avatar Nov 26 '20 00:11 ghost

@kyle-herrmann Here you go.... `function Add-WebPart($webPartName,$webpartprops,$page){ #add feedback component. Uses $feedbackListPropsJSON

Start-Sleep -Seconds 5

$appDetails = $null
$appDetails = Get-PnPApp -Identity "$webPartName"  -ErrorAction SilentlyContinue

#if app is not installed, first try installing it...
if($appDetails -eq $null){
    $installApp = Install-PnPApp -Identity "$webPartName"  -ErrorAction SilentlyContinue
}

Write-Host "Adding $webPartName Web part ... to the page..." -ForegroundColor Yellow
 if($webPartName -eq "CKEditor"){
        Add-PnPClientSideWebPart -Page $page -Component "$webPartName" -WebPartProperties $webpartprops -Section 1 -Column 2
        Write-Host "Added CKEditor WebPart"
    }

    if($webPartName -eq "Key resources"){
        Add-PnPClientSideWebPart -Page $page -Component "$webPartName" -WebPartProperties $webpartprops -Section 1 -Column 2
        Write-Host "Added Key Resources WebPart"
    }


    if($webPartName -eq "Page Contacts"){
        Add-PnPClientSideWebPart -Page $page -Component "$webPartName" -WebPartProperties $webpartprops -Section 1 -Column 3
        Write-Host "Added Page contacts WebPart"
    }
    if($webPartName -eq "CPNav"){
        Add-PnPClientSideWebPart -Page $page -Component "$webPartName" -Section 1 -Column 1
        Write-Host "Added Content Page nav WebPart"
    }

    if($webPartName -eq "FeedbackWebPart"){
        Add-PnPClientSideWebPart -Page $page -Component "$webPartName" -Section 2 -Column 1
        Write-Host "Added Feedback WebPart"
    }

}`

ravinleague avatar Nov 26 '20 00:11 ravinleague

@ravinleague you amazing man, while its not yet fully fixed using your installation check in the first branch of your code resolved the error

ghost avatar Nov 26 '20 00:11 ghost

well I spoke too soon, one the web part references was incorrect and after updating it we came across the same error. back to the drawing board

ghost avatar Nov 26 '20 00:11 ghost

OK. I may have a solution for your problem. What are you actually trying to do ?

ravinleague avatar Nov 26 '20 00:11 ravinleague

I've written a fairly large script which provisions microsoft teams, does some site configuration and finally sets up a landing page. Clearing the page and adding the sections is working fine but when I try to run Add-PnPClientSideWebPart I get the error:

Add-PnPClientSideWebPart : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I've swapped out parameters, changed the page layout, checked the connection as well as the web but the error is still occurring each time. I will insert the function below.

`# Function: AddWebPart

Adds a web-part to a SharePoint page

function AddWebPart($siteUrl, $connection) {

# Notify
WriteInfo "`nAdding specified web-parts..."

# Ensure project web is not null
if ($null -ne $siteUrl) {

    # Format title
    $homePageTitle = $global:xmlinput.Configuration.LandingPageSetup.Title

    # Retrieve home page
    $page = Get-PnPClientSidePage -Web $projectWeb -Identity $homePageTitle -Connection $connection

    # Retrieve sub-web
    $projectWeb = Get-PnPWeb -Connection $connection

    # Configure sections
    Add-PnPClientSidePageSection -SectionTemplate OneColumn -Page $pageTitle -Connection $connection

    # For each web part in the XML
    foreach ($webPart in $($xmlinput.Configuration.LandingPageSetup.AddWebparts.webpart) ) {
        
        # Notify
        WriteInfo("Adding web part $($webPart.Type)...")

        # Attempt to retrieve web-part
        $appDetails = Get-PnPApp -Identity $webPart.Component -ErrorAction SilentlyContinue

        # If not yet installed, install app
        if($appDetails -eq $null){
            $installApp = Install-PnPApp -Identity $webPart.Component  -ErrorAction SilentlyContinue
        }

        # Attempt to add web-part
        try {
            # Web part: Addin
            if ($webPart.Type -eq "Addin") {
                Add-PnPClientSideWebPart -Page $page -DefaultWebPartType "Hero" -Section $webPart.Section -Column $webPart.Column -Order $webPart.Order -Web $projectWeb -Connection $connection
            }
            # Notify success
            WriteSuccess "$($webPart.Type) web-part successfully added"
        } catch {
            # Notify failure
            WriteError "$($webPart.Type) web-part failed while adding"
        }
        
    }
}
else {
    WriteError("The site specified does not exist")
}

# Save and publish
$page.save()
$page.publish()

# Set page
Set-PnPClientSidePage -Identity $page -Publish -HeaderType None -CommentsEnabled:$false

}`

Thanks for taking the time to review this.

ghost avatar Nov 26 '20 01:11 ghost

I have added a list on my modern page using the script you provided at the top and it worked for me

ravinleague avatar Nov 26 '20 01:11 ravinleague

Currently trying to add a document library

ghost avatar Nov 26 '20 01:11 ghost

Can you print
$webPart.Section $webPart.Column $webPart.Order ?

ravinleague avatar Nov 26 '20 01:11 ravinleague

Yes, all variables are holding the correct values

ghost avatar Nov 26 '20 01:11 ghost

You can see in the code I've swapped out the web-part name for "Hero" which was still failing, assuming then that this is not specific to the type of web-part

ghost avatar Nov 26 '20 01:11 ghost

Hero wont work @kyle-herrmann Add-PnPClientSideWebPart -DefaultWebPartType List -page $page -WebPartProperties $options -Section 1 -Column 1 -order 1;

ravinleague avatar Nov 26 '20 01:11 ravinleague

I've used list and it worked

ravinleague avatar Nov 26 '20 01:11 ravinleague

Still receiving the same error when trying to add a list. Just had someone send me a version of their function which is working with other web-parts, gonna hop in a call with them. Thanks for your help man.

ghost avatar Nov 26 '20 01:11 ghost

Also, there might be some issue with adding web parts to home pages. What you can do is create a dummy page and set it as home page. Now in your actual page where you were getting the errors you may try to run the script and see if that makes any difference. If it worked, you can then in your script set the actual page as home page.

ravinleague avatar Nov 26 '20 01:11 ravinleague

Not helpful for the web part issue, but here is another way to get the home page for the web: ((Get-PnPWeb -Includes WelcomePage).WelcomePage -split "/")[-1]

ToddKlindt avatar Nov 26 '20 01:11 ToddKlindt

@ravinleague @ToddKlindt The issue was in the XML file, from what I’d seen online I assumed you could specify the web-part type as whichever app you wanted to use directly - but this wasn’t the case. After making some adjustments to how we retrieved the web, changing the type to a list, and passing in some JSON properties to specify that we wanted to display a library, the issue was resolved. Ravi was certainly on the right track, there must have been an issue with my connections that prevented me from adding a standard list either. Thanks all, rather embarrassed it took me this long.

ghost avatar Nov 26 '20 02:11 ghost