Question

According to the pnp.js docs at: https://github.com/pnp/pnpjs/blob/dev/packages/sp/src/folders.ts

there is a method called moveTo() which presumably moves a folder to a destination. However, the library throws this error:

'odata.error': { code: '-1, Microsoft.SharePoint.Client.ResourceNotFoundException',...

I'm able to get() the folder from the source and I've successfully created a dumb folder in the destination path, but the moveTo() method just doesn't work and it seems it's not implemented in the SharePoint API.

Any thoughts?

EDIT

code below:

sp.web.getFolderByServerRelativeUrl(`${parent_url}/${list}/${folder}`).moveTo(`${parent_url}/${list}/${folder}`)
    .then( (result) => {
      //do whatever
})
Was it helpful?

Solution

Looks like this is a known issue currently in the pnp.js library. Found the following pull request that has been merged back to the dev branch, as well as is a part of the 1.3.6 Milestone that still is not released yet.

The summary of the fix that was made is the following:

The function Folder.moveTo() used the .toUrl() function, which returned a relative url, so it was not possible to get the host url. Because of this, it returned a 404 error. Replaced it with the value of "odata.id" which includes the absolute url.

Based on the changes they have made, you can get this to function in the interim by the following code.

const source = `${parent_url}/${list}/${folder}`;
const destUrl = `${parent_url}/${list}/${folder}`;

sp.web.getFolderByServerRelativeUrl(source).select("ServerRelativeUrl").get().then(({ ServerRelativeUrl: srcUrl, ["odata.id"]: absoluteUrl }) => {
    const client = new SPHttpClient();
    const webBaseUrl = absoluteUrl.id.split("/_api")[0];
    const hostUrl = webBaseUrl.replace("://", "___").split("/")[0].replace("___", "://");
    const methodUrl = `${webBaseUrl}/_api/SP.MoveCopyUtil.MoveFolder()`;
    return client.post(methodUrl, {
        body: jsS({
            destUrl: destUrl.indexOf("http") === 0 ? destUrl : `${hostUrl}${destUrl}`,
            srcUrl: `${hostUrl}${srcUrl}`,
        }),
    }).then((r) => r.json());
}).then((result) => {
    // DO WHATEVER
});

Update 10/14/19: The Pnp.js library has been updated to 1.3.6 and the fix should be in the new version.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top