SharePoint Online文件夹内文档继承父文件夹ID的自定义脚本实现咨询
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
ALPHAdocument library. - Filter for Files: Add a condition to skip folders — only run the logic if
FileSystemObjectTypeequalsFile. - Get Parent Folder Details: Use the "Get file properties" action to fetch the document's parent folder, then extract its ID (either the native
IDfield or your custom folder ID if you're using one). - Update Document's Custom ID: Add an "Update item" action that sets your
CustomDocIDfield 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:
- Fetch the document's parent folder ID via REST API:
const parentFolderId = await sp.web.getFileByServerRelativeUrl(documentServerRelativeUrl) .listItemAllFields.select("ParentFolderId") .get(); - Retrieve the parent folder's item details to get its target ID.
- Update the document's
CustomDocIDfield with a PATCH request to the SharePoint REST API.
- Fetch the document's parent folder ID via REST API:
- Deployment: Package the SPFx solution, upload it to your tenant's App Catalog, then add it to the
ALPHAdocument 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
ALPHAdocument library. - Testing: Validate the logic with a small set of documents first before rolling out to all items.
内容的提问来源于stack exchange,提问作者OUALIT Youcef

