Creating Workspaces at user login

A valued customer asks:

There are two technologies that you’ll need to use. The first is the new Events feature available in 8.x and the second is the new Workspace API also available in 8.x.

Events:
Using the events editor (after modifying the Portlet Application project to have the CAF Events facet), add a handler that handles the MWSLoginEvent. Create a java function that switches based on what type of Login Event is fired. (There are numerous Actions including Login Script Done, and first time Login Script Done).


	public String handleUserLogin() throws Exception {
	    MWSLoginEvent event = (MWSLoginEvent)getEvent();
	    Action action = event.getAction();
		if (action == MWSLoginEvent.Action.ACTION_FIRST_LOGIN_SCRIPT_DONE) {
	    	//create the workspace
			runInUserContext(new CreateWorkspace(), event);
	    } else if (action == MWSLoginEvent.Action.ACTION_LOGIN_SCRIPT_DONE) {
	    	//redirect as you want
	    	runInUserContext(new SetDefaultWorkspace(), event);
	    }
	    
		return OUTCOME_OK;
	}

Workspace Creation:
Using the workspace API, you’ll create a new workspace based on an existing template and then add that workspace to the user’s current Tab Set.

There are a couple of caveats. First, any workspace user should belong to the My webMethods Users Role. Secondly, the user who is getting a new workspace created must have Read access to the workspace template.


		public void handleEvent(MWSLoginEvent event) throws Exception {
			// Create a new workspace 
			IWorkspaceServiceProvider workspaceProvider = WorkspaceServiceProviderFactory.getWorkspaceServiceProvider(); 
			IWorkspace workspace = workspaceProvider.createWorkspaceFromTemplate(MY_NEW_WORKSPACE, 
					IWorkspaceServiceProvider.TYPE_GENERIC, "workspace.sample", false, true); 
	
			//add the new workspace to the left hand navigation
			workspaceProvider.createWorkspaceView("wsfolder-recent", workspace.getURL()); 
	
			// create a new tab
			IUserTabSetService tabProvider = UserTabSetServiceFactory.getUserTabSetService(); 
			IUserTabSet tabSet = tabProvider.getTabSet(); 
			tabSet.addTab(workspace.getName(),workspace.getURL(), true); 
		}

Default Workspace:
When you want to set a workspace as the current page for a user, you’ll just make sure that the workspace is the current tab as in the following example:

		public void handleEvent(MWSLoginEvent event) throws Exception {
			IUserTabSetService tabProvider = UserTabSetServiceFactory.getUserTabSetService(); 
			IUserTabSet tabSet = tabProvider.getTabSet(); 
			IUserTab tab = tabSet.getTabByName(MY_NEW_WORKSPACE);
			if (tab != null) {
				tabSet.setCurrentTab(tab);
				tabSet.save();
			}
		}

Please see the attached project for the complete sample.
Regards,
–mark
WorkspaceApp.zip (10 KB)

1 Like

Thanks Mark,

The code works for me.

Regards,
Kishore

Hi Mark,

The code snippets that you have provided , are really helpful…We are able to achieve creation of workspaces at the login.

Thanks,
Chandra

how to reject login if the user has already logged on??

Could display a Error message for this and block the complete portal?