0
votes

I want to create a folder with hashtag(#) in the name in SharePoint Online using PowerShell pnp and following command fails with error message "The argument must be a single folder name and cannot contain path characters". (From SharePoint Online UI, it allows to create folders/files with #).

Add-PnPFolder -Name "Test # Lists" -Folder "ABC/"

The Same error occurs when uploading a file with the name containing a # using Add-PnPFile

I have tried escaping (`#) but it's giving the same error. Also tried encoding # with '%23', but it creates a folder with %23 in the name instead of #.

Any solution or a workaround is much appreciated.

3

3 Answers

2
votes

Not all APIs support creating files/folders with # or %.

https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/supporting-and-in-file-and-folder-with-the-resourcepath-api

https://developer.microsoft.com/en-us/office/blogs/upcoming-changes-to-sharepoint-and-onedrive-for-business-apis-to-support-and-in-file-names/

You could use SharePoint Online Powershell to create a folder with "#" in name.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$SiteUrl = "https://contoso.sharepoint.com/sites/dev"
$ListURL="/sites/dev/lib"
$FolderName="Reports#"
$UserName="[email protected]"
$Password ="Password"
  
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
Try {
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
   
    #Get the List Root Folder
    $ParentFolder=$Context.web.GetFolderByServerRelativeUrl($ListURL)
 
    #sharepoint online powershell create folder
    $Folder = $ParentFolder.Folders.Add($FolderName)
    $ParentFolder.Context.ExecuteQuery()
 
    Write-host "New Folder Created Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
1
votes

you can run the line of code to create the folder without the '#':

Add-PnPFolder -Name 'Test Lists' -Folder 'ABC'

And then rename the folder:

Rename-PnPFolder -Folder 'ABC/Test Lists' -TargetFolderName 'Test # Lists'
0
votes

& is also not supported. Seems like a similar issue is fixed for "Copy" routine but not for "Add" till now. Looks like String breaks with these characters and it can also confuse browsers to render the file name which can be assumed as parameters