Pnpjs: How to rename SharePoint folder with pnp/sp?

Created on 12 Apr 2018  路  5Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [x] Documentation gap/issue

Version

@pnp/sp v. 1.0.4

Question

I need to rename SharePoint folder. How can I do that?

Observed Behavior

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.

sample answered question

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.
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);

All 5 comments

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:

  1. You mentioned differences between getFolderByServerRelativePath and getFolderByServerRelativeUrl. I haven't noticed them before because getFolderByServerRelativePath works for both absolute and relative paths. Is it an excepted behavior?
  2. Is there a property which defines the parent directory, so I can move my folder somewhere else (i.e. 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.
  3. Mostly related to no. 2: is there a method to list all properties of an item? Neither 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.

Was this page helpful?
0 / 5 - 0 ratings