Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Reset-PnPDocumentID to recalculate the document id on a file #4238

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Added

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

### Changed

### Fixed
Expand Down
60 changes: 60 additions & 0 deletions documentation/Reset-PnPDocumentId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
Module Name: PnP.PowerShell
title: Reset-PnPDocumentId
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Reset-PnPDocumentId.html
---

# Reset-PnPDocumentId

## SYNOPSIS
Requests the unique document ID of a file to be recalculated and reassigned.

## SYNTAX

```powershell
Reset-PnPDocumentId -Identity <FilePipeBind> [-Verbose] [-Connection <PnPConnection>]
```

## DESCRIPTION
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.

You need to be connected to the same site collection in which the file on which you wish to perform the operation resides.

## EXAMPLES

### EXAMPLE 1
```powershell
Reset-PnPDocumentId -Identity "/sites/demo/Shared Documents/MyDocument.docx"
```

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.

### EXAMPLE 2
```powershell
Get-PnPFileInFolder -Recurse -FolderSiteRelativeUrl "Shared Documents" -ItemName "MyDocument.docx" | Reset-PnPDocumentId
```

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.

## PARAMETERS

### -Identity
The ID, listitem instance, File instance or server relative path of the file for which you want to request a document id reset.

```yaml
Type: FilePipeBind
Parameter Sets: (All)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
29 changes: 29 additions & 0 deletions src/Commands/Files/ResetDocumentId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities.REST;

namespace PnP.PowerShell.Commands.Files
{
[Cmdlet(VerbsCommon.Reset, "PnPDocumentId")]
public class ResetDocumentId : PnPWebCmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public FilePipeBind Identity;

protected override void ExecuteCmdlet()
{
var file = Identity.GetFile(ClientContext, this);
file.EnsureProperties(f => f.ServerRelativeUrl);
CurrentWeb.EnsureProperty(w => w.Url);

// Even though the URL parameter states server relative path, it requires the site relative path of the file
var siteRelativeUrl = file.ServerRelativeUrl.Remove(0, new Uri(CurrentWeb.Url).AbsolutePath.Length);
var url = $"{CurrentWeb.Url}/_api/SP.DocumentManagement.DocumentId/ResetDocIdByServerRelativePath(decodedUrl='{siteRelativeUrl}')";

WriteVerbose($"Making a POST request to {url} to request a new document ID for the file {file.ServerRelativeUrl}");
RestHelper.Post(Connection.HttpClient, url, ClientContext);
}
}
}
Loading