Get-PnPSiteTemplate Invoke-PnPSiteTemplate

Pnp PowerShell Export Import Site

On 02/07/2025

In Powershell

 

Define JSON configuration https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/configuring-the-pnp-provisioning-engine

Connect-PnPOnline -Url $siteUrl -ClientId $clientId -Tenant $tenantName -Thumbprint $cert
 

$xml = Get-PnPSiteTemplate -Out $SchmaXMLPath -Configuration $configPath

# Invoke-PnPSiteTemplate -Path $SchmaXMLPath



{
  "$schema": "https://aka.ms/sppnp-extract-configuration-schema",
  "handlers": [
    "ContentTypes",
    "Fields",
    "Lists"
  ],
  "persistAssetFiles": false,
  "lists": {
    "lists": [
      {
        "title": "Eutelsat Entity",
        "includeItems": false,
        "query": {
          "includeAttachments": false
        }
      }
    ]
  },
  "contentTypes": {
    "groups": [
      "Market Access"
    ]
  }
}

# remove particular nodes
$SchmaXMLPathDest = "MA_pmsatEntity_15.xml"
[xml]$xmlContent = Get-Content $SchmaXMLPath 
Clear-Host
# Define the namespace if needed (common in SharePoint XML)
$nsmgr = New-Object System.Xml.XmlNamespaceManager($xmlContent.NameTable)
# Add the namespace you are using in the XML document. Use "ns" as a prefix here.
$nsmgr.AddNamespace("pnp", "http://schemas.dev.office.com/PnP/2022/09/ProvisioningSchema")

# Use XPath to select fields. Adjust the XPath query based on your XML structure.
$fields = $xmlContent.SelectNodes("//Field", $nsmgr)

Write-Host "cout $($fields.Count)"
# Iterate over fields and remove those whose InternalName starts with an underscore
for ($i = $fields.Count - 1; $i -ge 0; $i--) {
    $internalName = $fields[$i].Attributes["Name"].Value
    if ($internalName -like "_*") {
        # Remove the field from its parent node
        $fields[$i].ParentNode.RemoveChild($fields[$i]) | Out-Null
        continue;
    }
    $internalName = $fields[$i].Attributes["Group"].Value
    if ($internalName -eq "_Hidden") {
        # Remove the field from its parent node
        $fields[$i].ParentNode.RemoveChild($fields[$i]) | Out-Null
    }
}


$fields = $xmlContent.SelectNodes("//pnp:ContentType", $nsmgr)
for ($i = $fields.Count - 1; $i -ge 0; $i--) {
    $internalName = $fields[$i].Attributes["Name"].Value
    if ($internalName -ne "MA_EutelsatEntity") {
        # Remove the field from its parent node
        $fields[$i].ParentNode.RemoveChild($fields[$i]) | Out-Null
    }
}


# Save the modified XML back to a file
$xmlContent.Save("{0}\template\test2.xml" -f (get-location))

 

powershell Sharepoint

No ratings yet - be the first to rate this.