@pnp/sp v. 1.0.4
I need to rename SharePoint folder. How can I do that?
I looked into Folder class source code and found update() function. Tried to use it like this:
sp.web.getFolderByServerRelativePath('/my/path/here').update({Name: 'newName'})
Request is getting passed, but it doesn't affect the folder name.
Thank you in advance for the help.
Hi Micha艂,
First of all, thanks for using the lib!
I can see some probable issues related to incorrect usage of getFolderByServerRelativePath.
getFolderByServerRelativePath expects relative path without webServerRelativeUrl part and without / in the beginning.
In some cases, it can be handy using getFolderByServerRelativeUrl.
Then, folder name can't be updated like this, you got to get folder's item and update FileLeafRef property. This is REST API related nuances so they can be omitted in PnPjs's docs.
Here is the example that should make all clear:
/**
* Where `/sites/site` is a `webServerRelativeUrl`
*/
// const folder = pnp.sp.web.getFolderByServerRelativeUrl('/sites/site/DocLib/Folder1');
const folder = pnp.sp.web.getFolderByServerRelativePath('DocLib/Folder1'); // equivalent
folder.getItem()
.then(item => item.update({ FileLeafRef: 'Folder2' }))
.then(console.log);
@koltyakov
Thank you very much!
Three supplementary questions:
getFolderByServerRelativePath and getFolderByServerRelativeUrl. I haven't noticed them before because getFolderByServerRelativePath works for both absolute and relative paths. Is it an excepted behavior?DocLib/Folder1/moveme -> DocLib/Folder2/moveme)? Basing on the only place in SharePoint documentation where FileLeafRef is mentioned I tried to set ParentFolderId with GUID of desired parent, but got 400 Bad Request error. Same with ParentID, ParentItemID and FileDirRef.get() nor getItem() give information about existence of FileLeafRef, so it's a bit tricky, especially when documentation sample uses Name property.I haven't noticed them before because getFolderByServerRelativePath works for both absolute and relative paths. Is it an excepted behavior?
Oh, that is true. I missed this. Sorry. Only used getFolderByServerRelativePath with paths like in the example above. And yes it resolves both types of paths. Thank you! Never stop learning something new.
Is there a property which defines the parent directory, so I can move my folder somewhere else
If a parent folder exists, FileLeafRef can include it, and the folder will be moved.
const folder = pnp.sp.web.getFolderByServerRelativePath('DocLib/Folder3');
folder.getItem()
.then(item => item.update({ FileLeafRef: 'Folder2/Folder3' }))
.then(console.log);
Yet, can't find a way of moving folder on a level above existing (i.e. /sites/site/DocLib/Folder2/Folder4 -> /sites/dev-a/DocLib1/Folder4). And have a feeling that it's not possible in REST and JSOM's SP.MoveCopyUtil should be used as a workaround.
Mostly related to no. 2: is there a method to list all properties of an item?
These props are requestable only via strict direct definition in $select.
Good question how to get all the options. I use a knowledge of other object models and some techniques (e.i. folder.listItemAllFields.select('FieldValuesAsText').expand('FieldValuesAsText').get().then(console.log) or getting entities.
@koltyakov
After doing some research and sniffing requests being sent by SharePoint's browser interface, I found out the way to run SP.MoveCopyUtil with REST API!
Here is the code:
const spLib = require('./node_modules/@pnp/sp')
const spHttp = new spLib.SPHttpClient()
await spHttp.fetch(
`https://test.sharepoint.com/sites/APP/_api/SP.MoveCopyUtil.MoveFolderByPath()`,
{
method: 'POST',
body: JSON.stringify({
srcPath: {
__metadata: {type: 'SP.ResourcePath'},
DecodedUrl:
'https://test.sharepoint.com/path/to/your/folder/here/1',
},
destPath: {
__metadata: {type: 'SP.ResourcePath'},
DecodedUrl:
'https://test.sharepoint.com/path/to/your/folder/here/2',
},
}),
headers: {
accept: 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose',
},
}
)
I think it would be great if we had a helper function available, something like sp.moveCopyUtil.moveFolderByPath.
Thanks! SP.MoveCopyUtil sounds to be a nice asset to add in terms where it's the only option.
Most helpful comment
Hi Micha艂,
First of all, thanks for using the lib!
I can see some probable issues related to incorrect usage of
getFolderByServerRelativePath.getFolderByServerRelativePathexpects relative path withoutwebServerRelativeUrlpart and without/in the beginning.In some cases, it can be handy using
getFolderByServerRelativeUrl.Then, folder name can't be updated like this, you got to get folder's item and update
FileLeafRefproperty. This is REST API related nuances so they can be omitted in PnPjs's docs.Here is the example that should make all clear: