Site provisioning: add web part to page
-
18-01-2021 - |
Question
I would like to present a document library on the home page (default.aspx
) of a classic SPO site.
What I would do with hand is to edit the page - add web part - apps - select the document library.
I exported this web part, added a few placeholders to the dynamic properties and I am able to add it to a new site, but I have difficulties with one property:
<property name="ListName" type="string">#ListID#</property>
Now, I can query the list id with
$listid = (Get-PnPList | where {$_.Title -eq $listname}).ID
and substitute the #ListID#
placeholder in the xml, but isn't there any better way to achieve this, without extra rounds? Can I maybe put this in the provisioning template?
Edit: Site provisioning done like:
New-PnPWeb -Title $Title -Url $SubSiteUrl -Template $Template -InheritNavigation #default STS#0
Connect-PnPOnline $url -Credentials $credential
Apply-PnPProvisioningTemplate -Path $template
The "Document" library is made by the default STS#0 template, I don't see, how I could get the library ID. The PnP template is almost empty.
Solution
You can use the {{listid:ListName}}
token instead of hard-coding the GUID.
When you use this token , the provisioning engine will fix the placeholder with the actual value of the List ID.
For example, if your list/document library name is Documents
, use {{listid:Documents}}
. Put the below line of code in your template xml file and then PnP will fix it for you:
<property name="ListName" type="string">{{listid:Documents}}</property>
Reference - Provisioning Engine tokens
OTHER TIPS
This is the approach I use. The problem with SP Online and remote APIs is that you have no access to a strongly-typed XsltListView webpart object, so you're stuck importing an XML-based web part and swapping out the placeholders as you've done.
I don't know what your provisioning process is like, but the ones I use are typically fully automated WebJobs and the placeholder approach is pretty solid once the process is set up.