« Learning XPages Table Of Contents | Main| Learning XPages Part 33⅓ : Simple Data Validation. »

Learning XPages Part 32 : Using Roles To Make Fields Editable

Tags : Lotus Domino XPages Roles
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

Now that we can switch our page into Edit Mode you'll see that all of the fields are editable. In most applications you may not want this so we can disable the ability to edit certain fields. Start by opening the content_Person custom control again.

Lets disable a couple of the fields that we don't want the end user changing. Select the 'FirstName' field and look at the properties. In the 'Edit Box' properties you will see a 'Read Only' tickbox. Turning this on will make the field uneditable. Nice and simple for this particular field.

A picture named M2

You may also notice that the 'Read-Only' tickbox has a diamond beside it. You can use this to create a computed result to determine if the field should be read only or not.

Lets say that we don't want the end user editing their Job Title but we do want somebody with the 'PhonebookEditor' role in the ACL to be able to update peoples job titles. First of all click on the 'JobTitle' field in the design and then in the properties click the diamond for the 'Read-Only' flag and select compute value.  In the script editor that appears we need to enter in the code to see if the currently logged in person has the role or not.  Here's the code I'm using :


var s1 = context.getUser().getRoles();
var s2 = "{PhonebookEditor}";

if(@Contains(s1, s2) == @True())
        return false;
else
        return true;

NOTE : Replace curly brackets with square brackets in the above code.

This very simple code could be reduced in size but I've left it expanded so you can see how it is working, I am setting up two variables and then doing an @contains to see if the role exists inside the current users .getRoles value. This would be very similar to how you would do it on a form in traditional notes client development.

In the next part I'll add some validation to the fields that can be edited so make sure the person enters in valid data.

Comments

Gravatar Image1 - Dude if all these great posts are penance for some sin, it must have been one helluva sin.

Sorry for the interruption. Please continue...Emoticon

Gravatar Image2 - What I want to know is, why wasn't @UserRoles implemented in xPages? It seems like it would be really simple for that to be equivalent to context.getUser().getRoles().....

There's many @functions like that -- seemingly simple to implement, but they weren't.

Gravatar Image3 - @2 - I agree. I just realized today that @IsDocBeingEdited isn't implemented either -grrrrr.