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

SharePoint Online文件夹内文档继承父文件夹ID的自定义脚本实现咨询

Solution for Inheriting Parent Folder ID in SharePoint Online Documents

Hey there, let's tackle this requirement where you want documents in a SharePoint Online library to inherit their parent folder's ID instead of getting a unique system-generated one. First, a critical note: the native SharePoint Document ID is a system-controlled field that can't be modified, so you'll need to create a custom field (e.g., CustomDocID) in your ALPHA document library to store the parent folder's ID. Here are your best options, broken down by approach:

1. Low-Code: Power Automate (Best for Non-Developers)

This is the easiest way to handle real-time document creation without writing full code. Here's how to set it up:

  • Trigger: Start with the "When an item is created" trigger pointing to your ALPHA document library.
  • Filter for Files: Add a condition to skip folders — only run the logic if FileSystemObjectType equals File.
  • Get Parent Folder Details: Use the "Get file properties" action to fetch the document's parent folder, then extract its ID (either the native ID field or your custom folder ID if you're using one).
  • Update Document's Custom ID: Add an "Update item" action that sets your CustomDocID field to the parent folder's ID value.

2. Code-Based: SPFx Extension (Real-Time & Customizable)

If you need more flexibility or want to embed this directly into the SharePoint UI, use SharePoint Framework (SPFx) with TypeScript/JavaScript:

  • What to Build: A List View Command Set or Application Customizer that listens for document upload/creation events.
  • Core Logic Snippets:
    1. Fetch the document's parent folder ID via REST API:
      const parentFolderId = await sp.web.getFileByServerRelativeUrl(documentServerRelativeUrl)
        .listItemAllFields.select("ParentFolderId")
        .get();
      
    2. Retrieve the parent folder's item details to get its target ID.
    3. Update the document's CustomDocID field with a PATCH request to the SharePoint REST API.
  • Deployment: Package the SPFx solution, upload it to your tenant's App Catalog, then add it to the ALPHA document library.

3. Batch/Backfill: PnP PowerShell

If you need to update existing documents or run periodic syncs, PnP PowerShell is your go-to tool:

  • Sample Script:
    # Connect to your SharePoint site
    Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yourSite" -Interactive
    
    # Target library
    $library = Get-PnPList -Identity "ALPHA"
    
    # Get all file items (skip folders)
    $fileItems = Get-PnPListItem -List $library -Fields "ID", "FileDirRef", "CustomDocID" | Where-Object { $_.FileSystemObjectType -eq "File" }
    
    foreach ($item in $fileItems) {
        # Get parent folder item
        $folderUrl = $item["FileDirRef"]
        $folderItem = Get-PnPListItem -List $library -Query "<View><Query><Where><Eq><FieldRef Name='FileRef'/><Value Type='Text'>$folderUrl</Value></Eq></Where></Query></View>"
    
        # Update custom document ID with folder's ID
        if ($folderItem) {
            Set-PnPListItem -List $library -Identity $item.Id -Values @{ CustomDocID = $folderItem.Id }
            Write-Host "Updated document ID for item $($item.Id) to $($folderItem.Id)"
        }
    }
    
  • Deployment: Run this script manually, or set it up in Azure Automation to run on a schedule.

Key Notes to Remember

  • Never attempt to modify the native SharePoint Document ID — it's locked by the system. Always use a custom field for the inherited ID.
  • Permissions: Ensure the account running the automation/PowerShell/SPFx has Edit permissions on the ALPHA document library.
  • Testing: Validate the logic with a small set of documents first before rolling out to all items.

内容的提问来源于stack exchange,提问作者OUALIT Youcef

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 14:38:11