XPages Tip: Accessing a custom control’s custom property from outside of the custom control

If you’ve used custom properties, then you’re aware that the compositeData object provides access to those properties within the custom control. However, you can also access those properties from outside of the custom control. In this post, I’ll show how.

Accessing a Custom Property from Within a Custom Control

Let’s start with a custom control named myCC that has a custom property named myProperty. Here’s what it looks like to include it on an XPage and pass in a value of myValue:

<xc:myCC myProperty="myValue"></xc:myCC>

On the custom control, you can refer to that value as compositeData.myProperty. Here’s an example of a computed field that displays the property value.

<xp:text escape="true" id="computedField1" 
  value="#{javascript:return compositeData.myProperty;}">
</xp:text>

Accessing a Custom Property from Outside of a Custom Control

If you have a need to access the a value passed to a custom property of a custom control from outside if the custom control, there are two steps to take:

  1. Add an id to the custom control instance on the page (so you can refer to it)
  2. Get the custom control component, retrieve a map of its property values, and then access the property that you need

Here’s an updated example of the custom control implementation on the XPage, with an id attribute added.

<xc:myCC myProperty="myValue" id="ccID"></xc:myCC>

Here’s how to refer to the custom property from the XPage:

getComponent('ccID').getPropertyMap().getProperty('myProperty');

This concept will work fine with multiple instances of the same custom control within a page because each instance would be required to have its own unique ID.

If you want to make the property available in client-side Javascript, you can use the "#{javascript: }" syntax:

var propertyValue = "#{javascript:getComponent('ccID').getPropertyMap().getProperty('myProperty')";

A Use Case

Here’s an example where I’ve recently found this useful. I have an application where I have a flexible grid custom control that takes a parameter with the URL for a REST service to supply the data. When I execute a search, I don’t want to reload the entire page — I just want to call the REST service to get the updated results and apply them to the grid.

But the search field exists as part of the application layout, so it’s outside of the grid custom control. Since the grid control is used for several types of grids, the REST service will vary, so I need to read a custom property of the custom control from another place on the page. This allows the Search feature to be reusable like the custom control.

Advertisements

8 responses to “XPages Tip: Accessing a custom control’s custom property from outside of the custom control”

  1. Sven Hasselbach says :

    If you want to access a datasource from a custom control, you can use this little snippet: http://hasselba.ch/blog/?p=777

  2. Michael G. Smith (@michaelgsmith) says :

    You can also update custom control custom properties by simply calling “put”:

    getComponent(‘ccID’).getPropertyMap().put(‘myProperty’,’my new value’);

  3. Bill says :

    Trying to get the value from a field inside a custom control and used
    var rtn = getComponent(“myControl”).getPropertyMap().getProperty(“pName”);
    While this does not return an error it does not return a value, and I’m not sure if it is an issue that the value of the field is not available or if I’m not calling it correctly

    • Brad Balassaitis says :

      Did you add an ID to the custom control tag in the source where it’s included on the XPage?

      It would need to look something like this, but replacing myCC with the name of your custom control:

      You can retrieve it by the value in the id attribute — not by the name after the <xc: opening tag.

      • Brad Balassaitis says :

        It removed my code from the previous comment. It should look like this, but with the proper opening angle brackets:

        xc:myCC pName=”myValue” id=”myControl”> /xc:myCC>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: