You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求SharePoint PnP脚本:获取WebPart Location术语并检查近期使用情况

Get Location Terms Used in SharePoint Web Parts with PnP PowerShell

Hey Vikram! Great start pulling terms from the taxonomy store—let's build on that to track down which Location terms are actually being used in web parts across your site. Here's a step-by-step script and breakdown to help you out:

Core Script to Extract Web Part Location Terms

This script will loop through all pages in your Site Pages library, check each web part on the page, and pull out any Location-associated terms along with context about where they're used:

# Connect to your SharePoint site (use -Interactive for modern auth, adjust as needed)
Connect-PnPOnline -Url $siteurl -Interactive

# Create an empty array to store our results
$webPartTermUsage = @()

# Fetch all pages from the Site Pages library (add additional libraries if you have pages elsewhere)
$sitePages = Get-PnPListItem -List "Site Pages"

foreach ($page in $sitePages) {
    # Get all web parts on the current page
    $webParts = Get-PnPWebPart -ServerRelativeUrl $page.FieldValues.FileRef

    foreach ($webPart in $webParts) {
        # Web part properties are stored as XML—parse that to access field values
        $wpProperties = [xml]$webPart.Properties

        # Adjust the XPath below to match the actual property name for Location in your web part
        # For example, if your web part uses a field named "SelectedLocation", update the @Name value
        $locationProperty = $wpProperties.SelectSingleNode("//Property[@Name='Location']")

        if ($locationProperty -ne $null) {
            # Managed metadata values in web parts often follow this format: "-1;#TermName|TermID"
            $termRawValue = $locationProperty.InnerText
            if ($termRawValue -match "^\-1;#(?<TermName>.+)\|(?<TermID>.+)") {
                $termName = $matches.TermName
                $termId = $matches.TermID

                # Pull full term details from the taxonomy store to get path, dates, etc.
                $termDetails = Get-PnPTerm -Identity $termId -ErrorAction SilentlyContinue

                if ($termDetails) {
                    # Add the result to our array with context about the page and web part
                    $result = [PSCustomObject]@{
                        PageTitle = $page.FieldValues.Title
                        PageFullUrl = "$siteurl$($page.FieldValues.FileRef)"
                        WebPartTitle = $webPart.Title
                        TermName = $termName
                        TermID = $termId
                        TermFullPath = $termDetails.PathOfTerm
                        PageLastModified = $page.FieldValues.Modified
                    }
                    $webPartTermUsage += $result
                }
            }
        }
    }
}

# Export the results to a CSV for analysis
$webPartTermUsage | Export-Csv -Path "C:/Terms/WebPartLocationTermUsage.csv" -NoTypeInformation

Key Notes to Adjust for Your Environment

  • Web Part Property Name: The script uses //Property[@Name='Location'] to find the Location field—you'll need to update this to match the actual property name used in your web part. To find this, run $webPart.Properties for a test page to view the full XML structure.
  • Additional Page Libraries: If you have pages stored in other libraries (like a custom Pages library), add another Get-PnPListItem call for that library and loop through those pages too.
  • Term Value Format: Some web parts might store term IDs directly instead of the -1;#Name|ID format. If that's the case, skip the regex matching and just use $locationProperty.InnerText as the Term ID.

Once you have this CSV, you can cross-reference it with your original term export to identify which terms are actively used in web parts, and filter by PageLastModified to check recent usage.

内容的提问来源于stack exchange,提问作者Vikram Mohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:17:56