Skip to content

Commit 4c180ba

Browse files
authored
Merge pull request #4238 from KoenZomers/Reset-PnPDocumentID
Added `Reset-PnPDocumentID` to recalculate the document id on a file
2 parents bf5083d + 51fe7e8 commit 4c180ba

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1010

1111
### Added
1212

13+
- Added `Reset-PnPDocumentID` cmdlet to request resetting the document ID for a document [#4238](https://github.com/pnp/powershell/pull/4238)
14+
1315
### Changed
1416

1517
### Fixed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
title: Reset-PnPDocumentId
4+
schema: 2.0.0
5+
applicable: SharePoint Online
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
online version: https://pnp.github.io/powershell/cmdlets/Reset-PnPDocumentId.html
8+
---
9+
10+
# Reset-PnPDocumentId
11+
12+
## SYNOPSIS
13+
Requests the unique document ID of a file to be recalculated and reassigned.
14+
15+
## SYNTAX
16+
17+
```powershell
18+
Reset-PnPDocumentId -Identity <FilePipeBind> [-Verbose] [-Connection <PnPConnection>]
19+
```
20+
21+
## DESCRIPTION
22+
This cmdlet allows requesting SharePoint Online to recalculate and reassign the unique document ID of a file. This can be useful if the document ID of a file has been lost, has gotten corrupted or duplicated. The unique document ID will be calculated based on an internal predictable algorithm and will contain parts of the site collection, web, list and listitem. It should only take seconds for it to recalculate and reassign the document ID. If it remains the same after running this cmdlet, it means the assigned document ID is correct. There's no use of running it multiple times on the same file.
23+
24+
You need to be connected to the same site collection in which the file on which you wish to perform the operation resides.
25+
26+
## EXAMPLES
27+
28+
### EXAMPLE 1
29+
```powershell
30+
Reset-PnPDocumentId -Identity "/sites/demo/Shared Documents/MyDocument.docx"
31+
```
32+
33+
This will request SharePoint Online to recalculate and reassign the unique document ID of the file MyDocument.docx in the Shared Documents library of the demo site collection.
34+
35+
### EXAMPLE 2
36+
```powershell
37+
Get-PnPFileInFolder -Recurse -FolderSiteRelativeUrl "Shared Documents" -ItemName "MyDocument.docx" | Reset-PnPDocumentId
38+
```
39+
40+
This will request SharePoint Online to recalculate and reassign the unique document ID of the file MyDocument.docx in the Shared Documents library of the current site collection.
41+
42+
## PARAMETERS
43+
44+
### -Identity
45+
The ID, listitem instance, File instance or server relative path of the file for which you want to request a document id reset.
46+
47+
```yaml
48+
Type: FilePipeBind
49+
Parameter Sets: (All)
50+
51+
Required: True
52+
Position: Named
53+
Default value: None
54+
Accept pipeline input: False
55+
Accept wildcard characters: False
56+
```
57+
58+
## RELATED LINKS
59+
60+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Management.Automation;
3+
using Microsoft.SharePoint.Client;
4+
using PnP.PowerShell.Commands.Base.PipeBinds;
5+
using PnP.PowerShell.Commands.Utilities.REST;
6+
7+
namespace PnP.PowerShell.Commands.Files
8+
{
9+
[Cmdlet(VerbsCommon.Reset, "PnPDocumentId")]
10+
public class ResetDocumentId : PnPWebCmdlet
11+
{
12+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
13+
public FilePipeBind Identity;
14+
15+
protected override void ExecuteCmdlet()
16+
{
17+
var file = Identity.GetFile(ClientContext, this);
18+
file.EnsureProperties(f => f.ServerRelativeUrl);
19+
CurrentWeb.EnsureProperty(w => w.Url);
20+
21+
// Even though the URL parameter states server relative path, it requires the site relative path of the file
22+
var siteRelativeUrl = file.ServerRelativeUrl.Remove(0, new Uri(CurrentWeb.Url).AbsolutePath.Length);
23+
var url = $"{CurrentWeb.Url}/_api/SP.DocumentManagement.DocumentId/ResetDocIdByServerRelativePath(decodedUrl='{siteRelativeUrl}')";
24+
25+
WriteVerbose($"Making a POST request to {url} to request a new document ID for the file {file.ServerRelativeUrl}");
26+
RestHelper.Post(Connection.HttpClient, url, ClientContext);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)