Code Monkey home page Code Monkey logo

Comments (6)

koltyakov avatar koltyakov commented on June 5, 2024 6

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

from pnpjs.

Dzieni avatar Dzieni commented on June 5, 2024 2

@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.

from pnpjs.

koltyakov avatar koltyakov commented on June 5, 2024 1

Thanks! SP.MoveCopyUtil sounds to be a nice asset to add in terms where it's the only option.

from pnpjs.

Dzieni avatar Dzieni commented on June 5, 2024

@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.

from pnpjs.

koltyakov avatar koltyakov commented on June 5, 2024

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.

from pnpjs.

github-actions avatar github-actions commented on June 5, 2024

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.

from pnpjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.