Best Practices - How to write Custom Controls for XPages

Niklas Heidloff | 06:13:59 AM July 20 2011
I hope you've all heard about the XPages Development Contest by now. Since I've reviewed the submitted controls and since I've done a couple of controls myself lately I wanted to share some best practices that I've seen. Some things are pretty obvious, other things are not. Here are some tips how to make custom controls reusable and consumable.

Document how to use the control in custom NSFs

Some custom controls contain all functionality in the one control. More typically however they require other design elements like views, stylesheets, etc. Or they even require to download and import other open source projects. I think the best place for this documentation is in the control itself in the control's design definition. Here you can enter HTML representing the control in the design tab. Since I don't really use this tab I think it would be great if Designer could show this also in the source tab, e.g. when moving over the control. Patrick Kwinten uses this technique in his movie player control.



Use custom properties correctly

Custom properties can be more than strings. Here are a couple of recommendations. Use intuitive names and define descriptions since they show up in the XPages UI. Also don't only use strings but also other types like boolean. Special editors make it easier to enter valid values. Require only as little properties as possible and use optional properties with good defaults. Also make usage of multi value properties where it makes sense.

Document the custom properties

As mentioned use good names and descriptions since they show up when embedding the controls in XPages. Here is a sample from Serdar Basegmez' involve control.





Ferry Kranenburg describes in his blog how to generate Excel documentation easily.

In some cases your controls might have a lot of properties that you might want to categorize. Mark Hughes uses a certain technique in his view picklist control. He categorizes the properties by assigning them categories in the xxx.xsp-config XML file that you can open from the Java perspective.



Separate the control from the data

In most cases it makes sense to separate the data source from the control. For example don't create properties for database, server and view name. Instead allow the XPage to pass in an existing datasource. Mark's control from above shows how to use the data source picker. Matt White also describes this in his XPages101 video. This technique even allows using a different type of data source (e.g. relation db query instead of a Domino view).

Also often views or forms are required by controls. In these cases document how these views and forms should be designed (e.g. which columns the view needs) so that custom views can be used.

Don't hardcode styles in your controls

Don't put style definitions directly in your controls. This makes it unnecessary difficult to overwrite the styles from the XPages. For example when you define a style in a control and the same one in the XPage the one from the control is used (always I think). So instead source the styles out in css files that you include.

Provide simple test XPages

I always recommend to also provide XPages showing how to use the controls which can be used at unit tests at the same time.

Many controls require some sample data. Since many people don't want or may put real people names, addresses etc. in the NSFs they share you can use a dummy data generator. The Extension Library comes with such a tool which generates lorem ipsum text, people and state names. Check out the Admin_Home.xsp.


  • Comments [9]


1Serdar Basegmez  7/20/2011 3:52:16 PM  Best Practices - How to write Custom Controls for XPages

Niklas, Thanks for referring :)

Excellent post.

I had a couple of things in my mind during my designs.

In involve control (v1.1), I have seperated CSS file and add a custom CSS parameter. I also seperated my CSJS methods into another file.

There is a clear disadvantage that if there are too much files outside your control, it will be more difficult to reuse later. However, using seperate files for javascript and CSS decreases footprint if you are using multiple copies of the same control (involve can be used in views).

Alternatively, you may include your JS codes into script blocks or CSS into stylesheet resources directly. A couple of things you should consider for CSS: if you want to take additional CSS file reference into the control, it may come before yours and may be overrided. The solution may be using custom class names seperately (class=somePanel somePanelCustom). I made it complicated, sorry :)

Alteration of YOUR CSS is not suggested because you may decide to change some class names or structures in later versions.

There are complicated things that custom controls can do. But you have to be careful. In partial refreshes, you should consider page cycles. Use your events responsibly :)

Multiple copies of the same control needs special attention because references inside control may be confused by the XSP engine. If you are going to use the custom control inside data tables, repeat elements or views then be careful getting custom properties. For example if you are getting ANY parameter computed from viewEntry object, you cannot use compositeData map in before/after page load events of your custom control (because it's not ready yet).

These are my best practices from two custom controls I designed so far :)



2Niklas Heidloff  7/20/2011 4:06:07 PM  Best Practices - How to write Custom Controls for XPages

Good points Serdar, they need to be added to this list.

I've done the separate CSS and JS files as well in my mobile controls project for the same reason.

Re css, not sure I understand. The fact that css files are overwritten by css files in XPages is a good thing in my mind since then you don't have to touch the control's css or code. What is the custom css parameter used for?

The usage of events and controls in repeats deserve another article I think. Feel free :) I'll be out for two weeks.



3Serdar Basegmez  7/21/2011 6:49:39 AM  Best Practices - How to write Custom Controls for XPages

Niklas,

I'm using custom css for additional CSS file reference like "./custom.css". It's not so elegant but it was the optimum as I saw.

I'll blog about additional issues soon I hope :)

... as soon as I moved to my new office :)



4Patrick Kwinten  8/1/2011 10:57:54 AM  Best Practices - How to write Custom Controls for XPages

I attended some XPages courses but when talking about custom controls this subject never had high attention. I thought in an 'advanced xpages development' workshop it was being discussed.

I also could not find any documentation on the subject so looking at other apps on OpenNTF helped well.



5Find freeware jobs  12/13/2012 2:32:49 AM  Niklas Heidloff’s Personal Website: Blog

This piece of writing is in fact a pleasant one it helps new net people, who are wishing in favor of blogging.



6Wholesale Jerseys  4/16/2013 10:21:44 AM  Niklas Heidloff’s Personal Website: Blog

I like the helpful information you provide in your articles.

I will bookmark your weblog and check again

here frequently. I am quite certain I will learn many new stuff right

here! Good luck for the next!



7TN Nike  4/16/2013 4:08:33 PM  Niklas Heidloff’s Personal Website: Blog

Valuable information. Lucky me I discovered your website

by chance, and I'm shocked why this coincidence didn't took place in advance!

I bookmarked it.



8Air Jordan Pas Cher  4/17/2013 4:30:26 AM  Niklas Heidloff’s Personal Website: Blog

Thank you for some other informative website. The place else may just I get that type of info written in

such an ideal method? I have a challenge that I'm just now working on, and I've been on the look

out for such info.



9yztracker  5/14/2013 8:57:32 AM  Niklas Heidloff’s Personal Website: Blog

Accurate Rank Tracking

Check keyword the same as normal user, exclude the news and images in serp and adwords.

Acrruate result from specific Countries and Language in Google(.

com .ca/.co.uk/ect with en/es/fr/etc). default is from Google

English USA

Quick and easy to use , support mobile

Track keyword in minutes after you add keyword.

After that, it will contiute to track keyword rank every day.

Clean Interface by twitter opensource gui competiable with mobile phone and different browser.

Type keywords one time, check from everywhere

from mobile, pc only if have internet access.

No description for this

Data is Save, We backup everyday.

If you use desktop one, you may get data loss due pc is crashed or move to another pc.

It will not happen here. host company do it naturally.

Track rank over time, glance by graphic chart

View your historical rankings over specific range by line chart or table

Multiple Project / Clients by Keyword CategoriesManage keywords

into Categories. Add, modify, manage, del categories as need Check report for any category

by a private link without login with clients.

Share a private link

Send someone a private link to keyword rank chart in a specific category

to easily view real time result without login or embed it in any webpage to rich your content.

Excel Report

How to use?

1, Click here to signup for free

2, create a category

3, import keyword and domain

4, wait a few of minutes to get first rank info.

5, it update daily automatically

Below are some thanks given on bhw

by awint