TypeError: "this._dataSource is undefined" when setting root folder of library control

Hello community,

I want to update the root folder of a library control (as described here:https://plumsail.com/docs/forms-sp/how-to/root-folder.html?#change-root-folder-dynamically) but I always get an exception (TypeError: "this._dataSource is undefined" ):

.
My source code looks like this:

Hello @muellerfelix,

Could you please share the complete code as a text with us to test it.

And I see that in your code setRootFolder function runs on form load, not on field change.

To run it on field change code should be as follows.

    fd.field('staffPerson').$on('change', function() {
        calcFoldername();
        setRootFolder();
    });

Hello @mnikitina,
please find attached the complete coder:

fd.spRendered(function() {
	function calcFoldername() {		
		var mailAddress = fd.field('staffPerson').value.Description;
		console.log(mailAddress);
		if (mailAddress) {
			var folderName = mailAddress.substring(0,mailAddress.indexOf("@"));
			fd.field('Title').value = folderName;
		}
	}
	
	function setRootFolder(dt){
		var folderName = fd.field("Title").value;
		console.log(folderName);
		pnp.sp.web.getList("/sites/hr_de/site1/Shared Documents").rootFolder.folders.getByName(folderName).get().then(function(data){  
		    console.log("Folder found: " + data.Name + " - " + data.ServerRelativeUrl);
			console.log(dt);
	        dt.baseRootFolder = String(folderName);
	        dt.rootFolder = String(folderName);  
			dt.refresh();                  
		}).catch(function(data){ 
			console.log("Folder not found");
			$('.related-docs').hide();
		});  		
    }
	
	//fd.field('staffPerson').$on('change',calcFoldername);
	setRootFolder(fd.control('SPDocuments'));
	fd.control('hlLib').href ="/sites/hr_de/site1/Shared%20Documents/Forms/AllItems.aspx?id=" + decodeURIComponent("/sites/hr_de/site1/Shared Documents/" + fd.field('Title').value);
}); 

I am aware that the function did not run on a field change. That is not neccessary because field "staffPerson" can not be changed in the Edit or Display Form. The onChange event was copied from the new form and is not necessary either.

@muellerfelix,

One of the reasons might be outdated app package.

Please check the app version in the console (F12). The latest version is 1.0.7. If the latest version not installed, please follow the instructions on how to Update the app package.
image

Please also try to use this code only and se if the error still occurs. Please double-check the internal names of the fields and controls.

fd.spRendered(function() {
    fd.field("Title").value = "Test";
    fd.control('SPDocuments').ready().then(function(dt) {
      setRootFolder(dt);
    });
    function setRootFolder(dt){
        var folderName = fd.field("Title").value;
        console.log(folderName);
        pnp.sp.web.getList("/sites/hr_de/site1/Shared Documents").rootFolder.folders.getByName(folderName).get().then(function(data){  
            console.log("Folder found: " + data.Name + " - " + data.ServerRelativeUrl);
            console.log(dt);
            dt.baseRootFolder = String(folderName);
            dt.rootFolder = String(folderName);  
            dt.refresh();                  
        }).catch(function(data){ 
            console.log("Folder not found");
            $('.related-docs').hide();
        });         
    }
});

Thank you @mnikitina,

the control was not loaded completely when spRendered was fired. After using the "ready" event handler it worked.

1 Like