Tuesday, 23 September 2014

Readers and Authors Fields

During the past period I have repeatedly been asked how Readers and Authors fields can be added in XPages.
There are several ways to achieve this. For example you could use the property ComputeWithForm.
In this blog post I will show you my solution to add Readers and Authors fields to a Notes Document in XPages.

1. Notes Form
In this example I use a simple form with an Authors and Readers field.


2. XPage
To add the Readers and Authors Fields to the Notes Document I use the following SSJS in the querySave event of the XPage (or Custom Control).
Use getDocument(true) to have the backend document synced with changes made in the frontend document.

var doc:NotesDocument = document1.getDocument(true)


3. Create a new document


4. Readers and Author Fields Notes Document
In the Notes document the Authors en Readers have been set properly.

Authors Field
SSJS : var authors = new Array("[Admin]","[Editors]", @Name("[CN]",@UserName()));


Readers Field
SSJS: var readers = new Array("[Users]","[Admin]");


Sample Code SSJS

//Sync backend document with changes made in the frontend document
var doc:NotesDocument = document1.getDocument(true)
//Authors Field
var authors = new Array("[Admin]","[Editors]", @Name("[CN]",@UserName()));
var item:NotesItem = doc.replaceItemValue("Authors", authors);
item.setAuthors(true);
document1.replaceItemValue("Authors",item);
//Readers Field
var readers = new Array("[Users]","[Admin]");
var item:NotesItem = doc.replaceItemValue("Readers", readers);
item.setReaders(true);
document1.replaceItemValue("Readers",item);
//Save Document
document1.save();

So, this is only one solution to add Readers and Authors Fields to a Notes Document, but this solution works for me.

3 comments:

  1. Very nice article. I'm not a fan of "computeWithForrm" at all and would much prefer a code solution like this.

    Just a couple of quick comments on the Readers/Authors Fields themselves..

    I've always done what you're doing. Using @Name("[CN]",@UserName()) to get the username. My understanding now though is that you really don't want the Common Name. You want the full Domino name. Using the short name like you're doing means you can't have 2 users of the same name in the app regardless of division.

    John Smith/MIS/Company is going to be the same as John Smith/Accounting/Company since you're grabbing the common name. So something to be careful of.

    Also as just a matter of personal preference. I don't like using multi-value fields if I can help it.. You can have as many as you want. Took me YEARS to realize that. haha. So I'll have "Servers" field... and "Admin" field.. that's basically hardcoded. Then the Users might be either multi value or if I can I'll break that up to something like:
    SalesRep
    SalesManager
    DivisionManager
    etc....

    For the apps I did that were heavy readers fields that made it easier for me then always having to deal with arrays as people were assigned and unassigned. You just have to control it. You don't want to auto populate "Servers" as a readers field if there's no actual user with a readers field for instance.

    Just some quick thoughts on a neat topic. Thanks for posting!

    ReplyDelete
    Replies
    1. Thanks for your comment.
      Good point regarding the username.

      Delete
  2. thx you. i like your pages with all the examples.

    ReplyDelete