none
Add a custom webpart with properties using powershell to sharepoint online RRS feed

  • Question

  • I would like to add a custom webpart with properties to some pages on Sharepoint online using powershell. tried using "Add-PnPClientSideWebPart" but it only seems to add webpart to "modern page" but not to other migrated pages (webpart pages) from sharepoint 2010. The same command is failing to add the custom webpart to modern page also. can you please suggest any other way of adding a custom webaprt.

    Thanks

     

    yes

    Friday, September 13, 2019 1:54 PM

All replies

  • Hi Niru,

    What is the error your are getting? You cannot add a custom web part in modern pages. you can only add SPFX web part in modern pages. The "Add-PnPClientSideWebPart"  is for adding client webpart to client pages - 

    https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpclientsidewebpart?view=sharepoint-ps

    To add a web part in classic pages follow this blog - 

    https://www.c-sharpcorner.com/article/adding-web-parts-to-sharepoint-pages-using-pnp-powershell/ 


    K Mohit

    Friday, September 13, 2019 2:18 PM
  • the error i was getting is "page not found" for the classic pages when used "Add-PnPClientSideWebPart".

    The webpart i am trying to add with script is "SPFX" webpart and since the webaprt is configured differently on different pages, i would like to set  it properties, that the reason i was using "Add-PnPClientSideWebPart" command.  By using "Add-SPOWebPartToWebPartPage", and converting webpart to XML, it won't let me set properties? is it?

    Thanks.


    yes

    Friday, September 13, 2019 2:42 PM
  • So as per my understanding you want to add SPFX part in modern and classic pages with different properties? 

    Add-PnPClientSideWebPart won't work with classic pages. In modern pages please try this one - 

    Add-PnPClientSideWebPart -Page $page -DefaultWebPartType Image -WebPartProperties @{imageSource="https://modex123.sharepoint.com/sites/testmig2/PublishingImages/NHC/kenneth%20anne%20ryan.jpg"}

    Here is the link to add a spfx webpart to classic or webpart pages - 

    https://sharepoint.stackexchange.com/questions/248683/script-to-add-spfx-webpart-to-a-classic-page-using-pnp-powershell


    K Mohit

    Friday, September 13, 2019 3:06 PM
  • Hi Niru,

    For classic page, here are the steps to add SPFX with properties using PnP PowerShell:

    1. Go to your SharePoint workbench page (i.e https://tenant.sharepoint.com/sites/test/_layouts/15/workbench.aspx)

    2. Add your SPFx webpart on the page and configure properties as per your requirements.

    3. Once done, you can export your webpart data by clicking on Webpart Data tab as below:

    4. In the dialog, you will find the classic pages tab. Click on it, which will open the webpart's xml as below:

    5. Copy the entire data, i.e whole section including the <webParts>...</webparts> tag.

    6.  In your local file system, create a yourWebpart.dwp or yourWebpart.xml file and paste that data inside this file and save your file.

    7. After that, you can run the below PS commands:

    $webpartXML = [IO.File]::ReadAllText("C:\Users\Admin\Desktop\yourWebpart.dwp")
    
    Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/test/Pages/classicPage.aspx" -Xml $webpartXML -ZoneId "Custom Zone" -ZoneIndex 0  

    Results looks like this:

    Here is a same question in StackOverflow for your reference:

    Script to add SPFx webpart to a classic page using PnP Powershell

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, September 16, 2019 8:17 AM
  • Thanks KumarMohiT for your reply. 

    I am able to add webpart to classic page but not able to add values to properties of it. "Get-PnPClientSideComponent" command is not able to find existing classic page. I am unable to add SPFX webpart to modern page. Tried to using both "Add-PnPClientSideWebPart" and "Add-PnPWebPartToWebPartPage" commands, but both don't work.

    Please help.

    thanks.


    yes


    • Edited by Niru1 Wednesday, September 18, 2019 3:21 AM
    Wednesday, September 18, 2019 3:20 AM
  • I am able to add webpart to classic page but not able to add values to properties of it. "Get-PnPClientSideComponent" command is not able to find existing classic page. I am unable to add SPFX webpart to modern page. Tried to using both "Add-PnPClientSideWebPart" and "Add-PnPWebPartToWebPartPage" commands, but both don't work.

    Please help.

    thanks.


    yes

    Wednesday, September 18, 2019 3:21 AM
  • I am able to add webpart to classic page but not able to add values to properties of it. "Get-PnPClientSideComponent" command is not able to find existing classic page. I am unable to add SPFX webpart to modern page. Tried to using both "Add-PnPClientSideWebPart" and "Add-PnPWebPartToWebPartPage" commands, but both don't work.

    Please help.

    thanks.


    yes

    Hi Niru,

    For adding properties to web part and add web part into classic page, please refer my post above, In Web Part Data xml , you can specify the properties needed and then use Add-PnPWebPartToWebPartPage command, add into a classic page.

    For adding web part to a Modern Page, please use Add-PnPClientSideWebPart command like below:

    Add-PnPClientSideWebPart -Page "Home" -DefaultWebPartType "QuickLinks" -section 1 -Column 1 -WebPartProperties $jsonPropsGridBlankInitial 

    Reference:

    https://www.dmdavi.es/2019/03/11/adding-quick-links-items-using-pnp-powershell/

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, September 18, 2019 4:03 AM
  • Hi Jerry,
    Thanks for your replay.

    From your reply, "In Web Part Data xml , you can specify the properties needed", this is manually setting up the properties but we have lot of webpart instances of a custom list view webpart which has been configured to display different lists on different pages. we have retrieved those configurations into a database table from the existing SP2010 environment. I will be reading those configurations from the database table and want to pass them as values to the webpart parameters, then add that webpart to a respective page, by script. Please let me know if that is possible?

    regarding adding webpart to modern page, the challenge is, how do i specify a custom webpart in the place of "Quicklinks" in your example. this would be my first question. I am looking to see if this example works for me. thanks


    yes

    Wednesday, September 18, 2019 4:10 PM
  • Hi Jerry,
    Thanks for your replay.

    From your reply, "In Web Part Data xml , you can specify the properties needed", this is manually setting up the properties but we have lot of webpart instances of a custom list view webpart which has been configured to display different lists on different pages. we have retrieved those configurations into a database table from the existing SP2010 environment. I will be reading those configurations from the database table and want to pass them as values to the webpart parameters, then add that webpart to a respective page, by script. Please let me know if that is possible?

    regarding adding webpart to modern page, the challenge is, how do i specify a custom webpart in the place of "Quicklinks" in your example. this would be my first question. I am looking to see if this example works for me. thanks


    yes

    Hi Niru,

    The situation seems some complex based on my understanding.

    If you store Web Part properties inside SQL Datatable, you should store as json like this:

     {"title":"Site Contact","layout":2,"persons":[{"id":"i:0#.f|membership|alexw@m365x642699.onmicrosoft.com"}]}

    Then use Read-SqlTableData command read data:

    Read-SqlTableData

    For adding custom SPFX web part with properties, you can add with "Componment" parameter like this:

    Connect-PnPOnline -Url https://m365x642699.sharepoint.com/sites/NewYork03
    
    $jsonObj = '{"title":"Site Contact","layout":2,"persons":[{"id":"i:0#.f|membership|alexw@m365x642699.onmicrosoft.com"}]}'
    $wp = $jsonObj | ConvertFrom-Json | ConvertTo-HashTable
    
    #Add People webpart with default user, to column 2 in section 1
    Add-PnPClientSideWebPart -Page "Home" -Component "HelloWorld" -Section 1 -Column 2 -WebPartProperties $wp
    
    #Finally publish the page
    Set-PnPClientSidePage -Identity "Home" -Publish

    Add-PnPClientSideWebPart

    Add SPFx WebPart to modern page using PnP PowerShell Cmdlets

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, September 19, 2019 9:21 AM