How to create subfolders?

Hi, I'm trying to use this tutorial: [ Organize related files in folders on a SharePoint form — SharePoint forms (plumsail.com)).

Everything is fine and working but I can't figure out how to create subfolders. There are two independent folders in two libraries in tutorial's example. What if I want to create one folder and subfolder in one library? E.g. /documents/folder/folder2.

Hello @wflorczyk,

You can create a subfolder using this code:

const list = sp.web.lists.getByTitle('List');
const folderName = 'subfoldertitle';
const rootFolder = 'RootFolderTitle';
list.items.add({
  Title: "temporaryName",
  // The folder is created in a root, so create it first with a temp unique name
  FileSystemObjectType: 1,
  ContentTypeId: '0x0120'
  // FileLeafRef unfortunately is ignored while creation
}).then(({ item }) => {
  return item.update({
    Title: folderName, // Rename the folder name
    FileLeafRef: `${rootFolder}/${folderName}` // Move to a subfolder
  });
}).then(console.log);

Replace RootFolderTitle with the name of the existing folder, and subfoldertitle with the desired subfolder name,

Hi, thank you for this example. What if I want to create a name of subfolder dynamicaly? e.g.

var agentName = fd.field('Title').value;

instead of:

const folderName = 'subfoldertitle';

I think it demands some changes to code.

WF

Hello @wflorczyk,

You can replace subfoldertitle and RootFolderTitle with any field value:

const folderName = fd.field('Title').value;

Note that the RootFolderTitle is the existing folder name.

I founded easier way:

pnp.sp.web.folders.add("Bids/parentFolder/SubFolder2")

parentFolder have to be created.

1 Like

EDIT 2: I solved everything out:

CT - update

const folder = pnp.sp.web.getFolderByServerRelativePath(pathPriprava);
const item = folder.getItem("ContentTypeId").then((value) => {
value.update({ ContentTypeId: "0x0120D520" });
});

Create of subfolders:

const createFolder = list.rootFolder.folders.add(rootFolderName).then((result) => {
const path = result.data.ServerRelativeUrl;
pnp.sp.web.folders.add(${path}/${nameOfSubfolder});
});

HI @mnikitina ,
I Would like to change the content type of the subfolder - I used @wflorczyk one-line code and created a folder in the Document library and a subfolder.
After it's created I need to update the subfolder with the "document set" Content-type.
I tried something like this and this won't work - console says item.update is not a function:

const path = "TestVR/Radost - 124/Radost - Příprava";
const folder = pnp.sp.web.getFolderByServerRelativePath(path);
const item = folder.getItem();
console.log(item);
const result = item.update({ ContentTypeId: "0x0120D5" });

EDIT: And I also suffer from spaces, diacritics when trying to create a subfolder :frowning: Do not you have any idea, how to encode/decore URI/URL?
I am trying to use this

pnp.sp.web.folders.add("Bids/parentFolder/SubFolder2")

But "parentFolder" cannot have diacritics and spaces and special characters. Then the error in the console says it has not found the folder.

Appreciate your help,
thank you so much.
Steve

1 Like