SOLVED

Rename a document stored inside a SharePoint document library using Office Dev PnP

Bronze Contributor

How to rename a document/file stored inside a documentlibrary using Office Dev PnP? I haven't found a cmdlet for it.

 

Specifically I want to rename the OneNote Notebook called "Team Site Notebook" that's generated for each team site to a name which corresponds to the site title, e.g. "{SiteTitle} Notebook".

 

If that's not possible, the alternative solution would be to create a new OneNote Notebook which corresponds to the site title, e.g. "{SiteTitle} Notebook". If you have a solution for that, that's also great.

 

Team Site Notebook to rename.png

 

 

6 Replies
you just want to rename this file on each provisioned site? or do you also upload the file? maybe a extention provider would help. Kr, Paul
best response confirmed by Harold van de Kamp (Bronze Contributor)
Solution

I might be misinterpreting the question, but does this help?

"MoveTo" can be used to rename files.

Connect-SPOnline -url [yoururl]
$ctx = Get-SPOContext
$web = Get-SPOWeb
$file = $web.GetFileByServerRelativeUrl("[your file url]")
$file.MoveTo("[your NEW server-relative url]", 'Overwrite')
$ctx.ExecuteQuery()

I was just surprised that there is not a cmdlet inside the Office Dev PnP library, e.g. Rename-SPOFile($SourceFileName, $DestinationFileName) method, or Move-SPOFile($SourceFileName, $DestinationFileName).

 

Thanks @Deleted and @Charles Willwerth for the solutions.

I'm now using the SharePoint Online CSOM based solution of Charles to realize the business requirement.

 

Well, PnP is an open source project that can always be improved and extended by the community :-). This might be an opportunity to add a cmdlet for this scenario :)
Does that Best Response actually work? My suspicion is that it doesn't because a OneNote notebook isn't a file but rather is a folder. You can use PnP to do that because it is an item in the library, but the easiest thing to do with PowerShell is to use WebDav to treat the library like any other folder, e.g. with Rename-Item. The biggest advantage to using WebDav is that all of your knowledge for dealing with files and folders applies and you can use the same scripts you might already have.

You are right @Douglas Ware.

@Charles Willwerth pointed me in the right direction. I was aware about a OneNote Notebook being a folder instead of an item, so I've realised it with this code:

$ctx = Get-SPOContext
$web = Get-SPOWeb
$folder = $web.GetFolderByServerRelativeUrl("/sites/SITEURL/SiteAssets/Team Site Notebook")
$folder.MoveTo("/sites/SITEURL/SiteAssets/MySpecificSiteNotebookName")
$ctx.ExecuteQuery()
1 best response

Accepted Solutions
best response confirmed by Harold van de Kamp (Bronze Contributor)
Solution

I might be misinterpreting the question, but does this help?

"MoveTo" can be used to rename files.

Connect-SPOnline -url [yoururl]
$ctx = Get-SPOContext
$web = Get-SPOWeb
$file = $web.GetFileByServerRelativeUrl("[your file url]")
$file.MoveTo("[your NEW server-relative url]", 'Overwrite')
$ctx.ExecuteQuery()

View solution in original post