简体   繁体   中英

Alfresco javascript get custom DataList

I've created a custom datalist and put some values on it. Now I try to get data values within a script that will be triggered by a rule. So far, I've trouble to get the custom datalist named test with this code:

var site = siteService.getSite(document.siteShortName);
var dataLists = site.getContainer("datalists");
var listCompany = dataLists.childByNamePath("test");

It seems that its return a null object but I can't figure it out why, I tried with

"dataLists", 
"datalists" and 
"data-lists" 

but still null object... Edit: After looking int he solr log, I have an error that says that Model tracking failed NamespaceException. Something like my model has already been defined previously

Second question: How can I acces to the data inside the datalist? Is getElementById() will work?

test dataList is like:

'test'  
  field1: name, 
  field2: company, 
  field3:number, 
  field4: number2

I hope the below script will help you to resolve your datalist issue.

var site = siteService.getSite(document.siteShortName);
var dataListsContainer = site.getContainer("datalists");
var dataLists = dataListsContainer.getChildren();

logger.log("Data List length : " + dataLists.length);
for(var x=0;x<dataLists.length;x++)
{
    var dataList = dataLists[x]; //Get the current data list        
    var props = dataList.getProperties(); //Read the data list properties
    var title = props["cm:title"]; //read the datalist's title property
    logger.log("Data List title : " + title);

    if (title.indexOf("<data_list_title_goes_here>") > -1){ //check whether it's the required data list or not

        var dataListItems = dataList.getChildren(); //get the all datalistitems
        logger.log("Total dataListItems : " + dataListItems.length +" for " + title);
        for(var y = 0;y<dataListItems.length;y++) //Iterate all the datalistitems one by one
        {
            var dataListItem = dataListItems[y];
            var dataListItemProps = dataListItem.getProperties();    //Read all the properties of the current datalistitem      
            logger.log(dataListItemProps["namespace:propertyname"]); //read your custom property here
            logger.log(dataListItemProps["namespace:propertyname"]); //read your custom property here
            logger.log(dataListItemProps["namespace:propertyname"]); //read your custom property here
        }
    }       
}

If you're using Server side javascript, getElementById() won't work and this is for client side javascript.

Let me try with my custom datalist and will update you. In mean time, can you please share, alfresco.log & solr.log file errors?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM