Pages

Monday, April 8, 2013

MicroStrategy WebSdk Create User

    UserBean user = WebBeanFactory.getInstance().newUserBean();
                user.setSessionInfo(MstrSessionManagement.serverSession);
                user.InitAsNew();
                WebUser ua = (WebUser) user.getUserEntityObject();
                WebStandardLoginInfo loginInfo = ua.getStandardLoginInfo();
                //Set basic user information
                ua.setLoginName(loginName);
                ua.setFullName(fullName);
                user.getObjectInfo().setDescription(description);
                loginInfo.setPassword(password);
                //Set other properties
                //   Calendar cal = Calendar.getInstance();
                //cal.set(2013, 11, 21);
                //Date d = cal.getTime();
                loginInfo.setPasswordExpiresAutomatically(false);
                //loginInfo.setPasswordExpirationDate(d);  //Password expires on November 21, 2013
                //loginInfo.setPasswordExpirationFrequency(90); //90 days to expire
                //loginInfo.setPasswordExpiresAutomatically(true); //If set to false, password never expires
                //loginInfo.setStandardAuthAllowed(true); //The user can log in using standard auth
                user.save();
                userID = user.getObjectID();
                MstrObjectList.log.info("User: \"" + loginName + "\" has been created successfully.");

3 comments:

Henrique said...

Hello Ghulam, this post is exactly what I need, create a user in MicroStrategy using Java. But I am a newbie in MicroStrategy, so I have a question.

1. Where can I find the MstrSessionManagement class from the line user.setSessionInfo(MstrSessionManagement.serverSession);

Ghulam Mujtaba Kori said...


You have to create a MSTR seesion management class this will return mstr session:




Class: Mstr Session management:

import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectsException;
import com.microstrategy.webapi.EnumDSSXMLApplicationType;

/**
* @author Ghulam Mujtaba Kori
SME Primatics Financial
*/
public class MstrSessionManagement
{
public static WebObjectsFactory factory = null;
public static WebIServerSession serverSession = null;

/**
* Creates a new session that can be reused by other classes
*
* @return WebIServerSession
*/
public static WebIServerSession getSession(String Servername, int port, String Project, String login, String password)
{
if (serverSession == null)
{
// create factory object
factory = WebObjectsFactory.getInstance();
serverSession = factory.getIServerSession();
serverSession.setServerName(Servername);
serverSession.setServerPort(port);
serverSession.setProjectName(Project);
serverSession.setLogin(login);
serverSession.setPassword(password);
serverSession.setApplicationType(EnumDSSXMLApplicationType.DssXmlApplicationServer );
try
{ MstrObjectHandler.log.info("Session created with ID: " + serverSession.getSessionID());
MstrObjectHandler.log.info("----------------------------------------------------------");
}
catch (Exception ex)
{
MstrObjectHandler.log.error("Error creating session:" + ex.getMessage());
System.out.println("Error in creating a session"+ ex.getMessage());
}
}
return serverSession;
}

public static void closeSession(WebIServerSession serverSession)
{
try
{
serverSession.closeSession();
serverSession = null;

}
catch (WebObjectsException ex)
{
System.out.println("Error in creating a session"+ ex.getMessage());

}
}
}


getting session:
WebIServerSession serverSession serverSession = MstrSessionManagement.getSession(serverName, 0, projectName, loginName, password);
System.out.println("New session Created Successfully..............");
WebObjectSource objSource = serverSession.getFactory().getObjectSource();

Unknown said...

it throws exception on user.getUserEntityObject()

What is the fix for this?