[Q&A Friday] Automatic Creation of User Workspaces


Fri 08 March 2013 By Laurent Doguin


 Automatic creation of user workspace
Automatic creation of user workspaces

Today we're answering a question about user workspaces. How can you create them automatically? First, if you don't know what a personal workspace is, here's a definition taken from our documentation:

Personal workspaces can only be accessed by their owner, by default. You can of course share the access to your personal workspace with other users. Personal workspaces are accessible in the header of the application.

What you need to know to answer that is that those personal workspaces are usually created lazily. It means they are created the first time something tries to access it. So the solution Ron proposed is to access the user personal workspace each time he successfully logged in.

You can access it using the following Java code:

  UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
userWorkspaceService.getCurrentUserPersonalWorkspace();

But, to do that, everybody would have to log in. If you still need to create all of them automatically, the good news is that there is a new method that takes a detached user as input. This means you can call it for every user of your application. But, as you can see, it's really new and not released yet. It will be available on Nuxeo 5.7.

    /**

 * Gets a detached user workspace of a specified user depending of the
 * passed principal.
 *
 * @param principal of the wanted user's workspace owner
 * @param context is a document to determine the domain
 * @return the DocumentModel for the personal workspace
 * @since 5.7
 */
DocumentModel getUserPersonalWorkspace(NuxeoPrincipal principal,
        DocumentModel context) throws ClientException;


So you could use this in an operation like this:

    @Context
UserManager userManager;

@Context
UserWorkspaceService uws;

@OperationMethod(collector = DocumentModelCollector.class)
public DocumentModel run(DocumentModel input) throws ClientException {
    List<String> userIds = userManager.getUserIds();
    for (String userId : userIds) {
        NuxeoPrincipal user = userManager.getPrincipal(userId);
        uws.getUserPersonalWorkspace(user, input);
    }
    return input;
}


Please keep in mind that this can be a very costly operation. You should probably batch this if you have many users too.


Category: Product & Development
Tagged: Q&A