Setting rootfolder based on Item Colom in SP List

Hi There,

I have used already in a different list the functionality to set a library (rootfolder) based on a column field in the parent list. However it seems I can't replicate the same succes.

I am using the following code in JS editor

fd.spRendered(function(ctx) {
var dt = fd.control('ProjectLibrary');
dt.ready(function() {
setRootFolder();
});

//set root folder when Category field changes
fd.field('ProjectID').$on('change', function() {
    setRootFolder();
});

function setRootFolder(){
    var category = fd.field('ProjectID').value;
    if(category){
        dt.baseRootFolder = category;
        dt.rootFolder = category;
    }
}

});

These are the current settings in the Forms.

And bellow also a view of the SP List working:

Anyone any idea, what I am doing wrong ? I can't see it, have tried many other codes. (Constant, Dynamic, with var, with Strings, also tried other coloms in the SP list with or without Index. Don't know.

BTW, This is what I get (always) ... as if the JS did not run at all? .
While it should have filtered the folder based on the Project ID

Hello @Sami,

I have tested your code on my side. It worked good.
Do you have any other code in the *JS section?

I tried this:

fd.spRendered(function (ctx) {
  var dt = fd.control("ProjectLibrary");
  dt.ready(function () {
    setRootFolder();
  });

  //set root folder when Category field changes
  fd.field("Title").$on("change", function () {
    setRootFolder();
  });

  function setRootFolder() {
    var category = fd.field("Title").value;
    if (category) {
      dt.baseRootFolder = category;
      dt.rootFolder = category;
    }
  }
});

The designer:

Also tried to change the "ProjectID" - "Title" on my side and it worked. It was switching.

EDIT: So, is there any other code? Please see the developer console (F12) and check if your code fails or not.

1 Like

Hey Stepan,

Thnx for your reply. No it is the only code in JS and in CSS. No other codes are applied on this form.

Weird, yeah the code works form me on a different form and list. But here something is wrong. Can't figure out what's going wrong.

Already many thanks to think with me along this issue <3

I just checked indd the Dev Tool ...

Dear @Sami,
Which field type are using for the value? Is ProjectID a lookup field?

If so, you need to wait for it to be ready as well, and to get the text value out of it:

fd.spRendered(() => {
  var dt = fd.control('ProjectLibrary');
  var categoryField = fd.field('ProjectID');
  dt.ready(() => {
    categoryField.ready(() => {
      //set root folder when the form loads
      setRootFolder();
      //set root folder when ProjectID field changes
      categoryField.$on('change', setRootFolder);
    });
  });


  function setRootFolder() {
    var category = categoryField.value.LookupValue;
    if (category) {
      dt.baseRootFolder = category;
      dt.rootFolder = category;
    }
  }

It works easier with a text field.

1 Like

Hello @Sami ,

thanks for the response. Check @Nikita_Kurguzov comment, but it looks like your column is Text. What comes to my mind is, that InternalName of the column is not "ProjectID". Can you check it and send screenshot?

Thanks
Stepan

Hi Stepan and Nikita,

Checked, your guys post.

It is a text value, which I let PowerAutomate fill in based on the 'project' (look-up) field.

So it is a text value and the internalName checks out.

THank @Sami ,

I think that the field is not present on the canvas.
You are trying to read data from ProjectID, but the field is not on the canvas in the designer.

Looks like code is working, but as the developer console alerts, you can see that "cannot read of properties "undefined"".

Try to check it if the field is present (and yes, I saw the image at the top). Please try to enable this field - disable "read only" for testing purposes.

If it works, disable the fields programatically.

fd.field("ProjectID").disabled = true;

NOT via Designer.

Give it a try and let us know :slight_smile:
Regards
Stepan

Hey Stepan,

Correct is was a 'read-only' field. I have turned that off for testing purposes. However still no luck here.

Weird that a different form where I used also this code and also a read-only fiels does work ? right ?

Example where it works (even cross tabs):

Hey Guys,

I just tried out of the blue, to create a new column.
and tried to aply as a test the code on that column type :slight_smile:

AND IT WORKS (but weird, that it not works on the projectID colum ? )

Hello @Sami,

strange behaviour. Sometimes I had some issues also with some text fields (randomly), on SharePoint 2019 (have not figured it out). Always solved by creating a new column and replaced that. Of course during the development, not in the production where all the documents were related to the field.

What about triming the input from ProjectID?
Like:

const projectID = fd.field("ProjectID").value.trim();

You know sometimes blank/white spaces give us headache when querying etc...
Give it a try also :slight_smile:
But I am glad you figured it out.

Stepan

Just tried triming the field's input,

Also no luck pfff ... yeah let's just put it on weird behaviour :smiley:

Can't explain it, maybe also some weird malipulations with the colomn in SP that conflicts with the code or so. I have no idea.

But thanks for the active help !

1 Like