Skip to content

Commit

Permalink
Fixed document set parent folder implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Sheth committed Aug 9, 2024
1 parent a2557ab commit 518d8ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
8 changes: 4 additions & 4 deletions documentation/Add-PnPDocumentSet.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Creates a new document set in a library.
## SYNTAX

```powershell
Add-PnPDocumentSet [-List] <ListPipeBind> [-Name] <String> [-ContentType <ContentTypePipeBind>] [-Folder <String>]
Add-PnPDocumentSet [-List] <ListPipeBind> [-Name] <String> [-ContentType <ContentTypePipeBind>] [-Folder <FolderPipeBind>]
[-Connection <PnPConnection>]
```

Expand Down Expand Up @@ -65,12 +65,12 @@ Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Folder
The list relative URL of a folder. E.g. "MyFolder" for a folder located in the root of the list, or "MyFolder/SubFolder" for a folder located in the MyFolder folder which is located in the root of the list.
### -Folder
The folder in the site/list where the document set needs to be created.
```yaml
Type: String
Type: FolderPipeBind
Parameter Sets: (All)

Required: False
Expand Down
30 changes: 28 additions & 2 deletions src/Commands/DocumentSets/AddDocumentSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AddDocumentSet : PnPWebCmdlet
public string Name;

[Parameter(Mandatory = false)]
public string Folder;
public FolderPipeBind Folder;

[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty]
Expand Down Expand Up @@ -50,7 +50,7 @@ protected override void ExecuteCmdlet()
if (Folder != null)
{
// Create the folder if it doesn't exist
targetFolder = CurrentWeb.EnsureFolder(list.RootFolder, Folder);
targetFolder = EnsureFolder();
}

// Create the document set
Expand All @@ -59,5 +59,31 @@ protected override void ExecuteCmdlet()

WriteObject(result.Value);
}

/// <summary>
/// Ensures the folder to which the document set is to be created exists. Changed from using the EnsureFolder implementation in PnP Framework as that requires at least member rights to the entire site to work.
/// </summary>
/// <returns>The folder to which the document set needs to be created</returns>
private Folder EnsureFolder()
{
// First try to get the folder if it exists already. This avoids an Access Denied exception if the current user doesn't have Full Control access at Web level
CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);

Folder folder = null;
try
{
folder = Folder.GetFolder(CurrentWeb);
folder.EnsureProperties(f => f.ServerRelativeUrl);
return folder;
}
// Exception will be thrown if the folder does not exist yet on SharePoint
catch (ServerException serverEx) when (serverEx.ServerErrorCode == -2147024894)
{
// Try to create the folder
folder = CurrentWeb.EnsureFolder(CurrentWeb.RootFolder, Folder.ServerRelativeUrl);
folder.EnsureProperties(f => f.ServerRelativeUrl);
return folder;
}
}
}
}

0 comments on commit 518d8ee

Please sign in to comment.