Page tree
Skip to end of metadata
Go to start of metadata

Views are typically required for retrieving collections of documents from a Database. Folders can be used for holding a specific selection of documents, but in XPages applications are less typically used. The Design API allows you programmatic CRUD access to a View or Folder.

A View will need at least a name or alias. If no selection formula is set, it will include all documents in the database. Columns are added and, if a SortOrder is set, the View will be sorted by default including that View. If the designer wishes to allow users to sort on a different column, the ResortOrder can be set, including a secondary column. This will create an additional view index for the view, so will have an impact on updates.

View CRUD
Session sess = Factory.getSession(SessionType.NATIVE);
Database extLib = sess.getDatabase("extlib.nsf");
View contacts = extLib.getView("AllContactsProgrammatic");
if (null != contacts) {
	contacts.remove();
}
DatabaseDesign dbDesign = extLib.getDesign();
DesignView newView = dbDesign.createView();
newView.setSelectionFormula("SELECT Form=\"Contact\"");
newView.setName("AllContactsProgrammatic");
DesignColumn col = newView.addColumn();
col.setItemName("State");
col.setSortOrder(SortOrder.ASCENDING);
col.setTitle("STATE");
col.setCategorized(true);
DesignColumn name = newView.addColumn();
name.setFormula("FirstName+\" \"+LastName");
name.setSortOrder(SortOrder.ASCENDING);
name.setTitle("NAME");
DesignColumn city = newView.addColumn();
city.setItemName("City");
city.setTitle("CITY");
city.setResortOrder(ResortOrder.ASCENDING);
city.setSecondarySortColumn(1);
newView.save();

The example above demonstrates adding an additional View programmatically to a database based on the Extension Library demo template. It creates a view restricted to Contacts, with columns sorted for State and Name (comprising FirstName + " " + LastName). It adds a further column for City with a resort option for that column. The resort will sort additionally on the NAME column - {{setSecondaryColumn()}} counts the columns starting at 0 for the frst column.

  • No labels