XPages: Access a datasource from one custom control in another one

On stackoverflow.com, a very interesting question was asked: How can you access a document datasource from one custom control in another custom control. And here comes my solution in a small SSJS function.

First you have to add an id to the custom control which contains the datasource. This gives you a handle for other custom controls:

<xc:ccDS1 id="idDSComponent"></xc:ccDS1>

Now it is possible to get the custom control by using the SSJS function getComponent(). The datasources are stored in the data attribute of the custom control. You just have to iterate through them and check every entry for the name you are looking for.

And here is the code snippet to access the datasource in another custom control:

/***
 * getDatasource
 * Resolves a datasource from a custom control
 * 
 * @param componentId    id of the component containing the datasource
 * @param dsName    name of the datasource in the component
 * @author Sven Hasselbach
 */
 function getDatasource( componentId:String, dataSourceName:String ):com.ibm.xsp.model.domino.DominoDocumentData {
    try{
       var data:java.util.ArrayList = getComponent( componentId ).getAttributes().get("data");
       if( data == null )
          return null;
                    
       var it:java.util.Iterator = data.iterator();
       var obj = null;
       while( it.hasNext() ){
          obj = it.next();
          if( obj.getVar() == dataSourceName )
             return obj;
       }
    }catch(e){
       print( e );
    }
}

To use the function you have to use it like this:

getDatasource( "idDSComponent", "document1" )

Here is the link to the original question: How to get document datasource in another Custom Control?

Dieser Beitrag wurde unter Java Script, ServerSide JavaScript, XPages abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

2 Antworten zu XPages: Access a datasource from one custom control in another one

  1. Louis Lieberman sagt:

    Hi Sven…we are fairly new to Xpages, where does this function go? Into a Library on the Xpage or into one of the custom controls or somewhere else?

    We are trying to populate fields on another CustomControl, based on a dropdown selection ComboBox. We are getting a NULL Conversion Error Setting Value ….for Null Converter when trying the code. We are calling the Function from the second Custom Control containing multiple fields.

    Thanks for your time, and assistance, as always

    Are there any other code that is needed….

    • You can use the function wherever you want, it is plain SSJS.
      But it is best practise to add the function into a SSJS library and include the library in the component which requires the method.
      And no, there is no other code needed.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.