pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
988 stars 665 forks source link

Add-PnPClientSideWebPart Error #1634

Open robbert-vanandel opened 6 years ago

robbert-vanandel commented 6 years ago

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?

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?

RamaKaryam commented 5 years ago

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

dmdaviesTM commented 5 years ago

Same here.

jansenbe commented 5 years ago

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.

ravinleague commented 5 years ago

Same issue...

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

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

ToddKlindt commented 3 years ago

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

ghost commented 3 years ago

@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

ToddKlindt commented 3 years ago

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. :(

ghost commented 3 years ago

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

ToddKlindt commented 3 years ago

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

ghost commented 3 years ago

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

ravinleague commented 3 years ago

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

ghost commented 3 years ago

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

ravinleague commented 3 years ago

@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"
    }

}`

ghost commented 3 years ago

@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 commented 3 years ago

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

ravinleague commented 3 years ago

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

ghost commented 3 years ago

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.

ravinleague commented 3 years ago

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

ghost commented 3 years ago

Currently trying to add a document library

ravinleague commented 3 years ago

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

ghost commented 3 years ago

Yes, all variables are holding the correct values

ghost commented 3 years ago

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

ravinleague commented 3 years ago

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

ravinleague commented 3 years ago

I've used list and it worked

ghost commented 3 years ago

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.

ravinleague commented 3 years ago

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.

ToddKlindt commented 3 years ago

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]

ghost commented 3 years ago

@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.