Update folder field and copied items' parent field

Hi,
I have a special request from people in our company.
They need to work with presaved documents linked to the request, so I decided to let them create a list item (request) and with the help of plumsail forms to display the people documents that meet the requirements.

So there exist one request - list item - and it looks like this:


Standard setup for saving documents to different library, and display them based on parent field in the child document library.
Child document library looks like this:

So the usual work would be to create a list item (request), upload data and it will be forever connected via "Parent" field with the request.

But, they need to create request and based on some data in the request, I need to find folders (different document library) on the web and copy the folder to document library called "SmlouvyDokumenty" and link the folder and it's content with the request.
I have tried so many approaches, but nothing worked for me.
This is the code that works for copying the folder and files from one place to another.
EDIT: 12/12/2022 8:30 AM* - *GOT IT ALL WORKING
Here is the code - solution for everybody who is curious. Thank you for this space to share.

const folder = pnp.sp.web.getFolderByServerRelativePath("SmlouvyDokumenty/XXXX Štěpán - XXX 8798");
const folderUpdate = folder.getItem().then((res) => {
  res.update({ ParentConnectionId: 53 });
});
//const result = pnp.sp.web.item.update({ ParentConnection: 53 });

const folderItems = pnp.sp.web
  .getFolderByServerRelativePath("SmlouvyDokumenty/XXXX Štěpán - XXX 8798")
  .files()
  .then((files) => {
    console.log(files);
    for (let i = 0; i < files.length; i++) {
      pnp.sp.web
        .getFileByServerRelativePath(`${files[i].ServerRelativeUrl}`)
        .getItem()
        .then((FileFields) => {
          FileFields.update({
            ParentConnectionId: 53,
          });
        });
    }
  });

EDIT: 12/12/2022 7:30 AM - WORKING Solved FOLDER - not files in the folder

const folder = pnp.sp.web.getFolderByServerRelativePath("SmlouvyDokumenty/XXX Štěpán - XXX 8798");
const folderUpdate = folder.getItem().then((res) => {
  res.update({ ParentConnectionId: 53 });
});

EDIT: 12/12/2022 6:25 AM
(Still not working)
ERR - ParentConnectionId does not exist on type SP.Folder

pnp.sp.web
  .getFolderByServerRelativePath("SmlouvyDokumenty/XXXX Štěpán - XXX 8798")
  .listItemAllFields.get()
  .then((res) => {
    pnp.sp.web
      .getFolderByServerRelativePath("SmlouvyDokumenty/XXXX Štěpán XXX 8798")
      .update({
        ParentConnectionId: 53,
      })
      .catch((err) => {
        console.log(err);
      });
  });

 const folder = pnp.sp.web.getFolderByServerRelativeUrl("/xxx/sluzebni-automobily/TestDokumenty/MyFolder");
  folder.copyTo(`/xxx/sluzebni-automobily/SmlouvyDokumenty/${carUser} - ${carRZ}`, false).then(res => {
// HERE (for the folder) I need to update Lookup value to link it with request
// The best would be to update Lookup field for a Folder and for every content inside too
   pnp.sp.web.getFolderByServerRelativePath("SmlouvyDokumenty/XXXX Štěpán - XXX 8798").then(res => {
    console.log(res);
   })

  })

Final look should look like this:

I really appreciate your help, I try hard to do it, but it always fail to work.
Thank you for any opinion.

Steo

1 Like