Kashif Nizam Qureshi

Adding User to Project Server Programmatically

Use the following code to add user to Project Server.


private bool CreateUser()

{

// Before creating epm user, the user who’s creating the epm user need to provide cookie, either use windows or form user 

bool useWinLogon = true;  // If using Custom Sign-In page, form user must be used.

string userName = “”;

string password = “”;

LoginWindowsWeb.LoginWindows loginWindows = new LoginWindowsWeb.LoginWindows();

LoginFormsWeb.LoginForms loginForms = new LoginFormsWeb.LoginForms();

CookieContainer cookies = newCookieContainer();

string PROJECT_SERVER_URL = “http://kashif:/PWA/“;

string LOGINFORMS = “_vti_bin/PSI/LoginForms.asmx”;

// Login

if(useWinLogon)

{

loginWindows.Url = PROJECT_SERVER_URL + LOGINFORMS;

loginWindows.Credentials = CredentialCache.DefaultCredentials;

try

{

if(loginWindows.Login())

return true;

}

catch (Exception ex)

{

string str = ex.Message;

}

}

else

// Use LoginForms

{

loginForms.Url = PROJECT_SERVER_URL + LOGINFORMS;

loginForms.CookieContainer = cookies;

if(loginForms.Login(userName, password))

return true;

}

//Creating FBA User First

MembershipUser newUser = System.Web.Security.Membership.CreateUser(txtUsername.Text, txtpassword.Text, txtEmail.Text);

newUser.IsApproved = true;

//Creating Project Server User

ResourceWeb.Resource resource = new ResourceWeb.Resource();

// Add the Url property first.

resource.Url = PROJECT_SERVER_URL + RESOURCE_SERVICE_PATH;

if (useWinLogon)

// Add Windows credentials

{

resource.Credentials =CredentialCache.DefaultCredentials;

}

else

// Add Project Server logon cookie

{

resource.CookieContainer = loginForms.CookieContainer;

}

string[] resourceNames = newstring[] { txtEPMDisplayName.Text };

PSLibrary.Resource.Type[] resourceTypes =new PSLibrary.Resource.Type[] { PSLibrary.Resource.Type.WorkResource };

ResourceWeb.ResourceDataSet rDS = new ResourceWeb.ResourceDataSet();

ResourceWeb.ResourceDataSet.ResourcesRow resourceRow =rDS.Resources.NewResourcesRow();

Guid[] resources = newGuid[1];

resourceRow.RES_UID =Guid.NewGuid();

resources[0] = resourceRow.RES_UID;

resourceRow.RES_NAME = resourceNames[0];

resourceRow.RES_INITIALS = resourceNames[0].Substring(0, 1) +

(resourceNames[0].IndexOf(” “) > 0 ? resourceNames[0].Substring(resourceNames[0].IndexOf(” “) + 1, 1):“”);

resourceRow.RES_TYPE = (int)resourceTypes[0];

resourceRow.WRES_EMAIL = txtEmail.Text;

rDS.Resources.AddResourcesRow(resourceRow);

ResourceWeb.ResourceDataSet.ResourceAvailabilitiesRow availRow = rDS.ResourceAvailabilities.NewResourceAvailabilitiesRow();

availRow.RES_UID = resourceRow.RES_UID;

availRow.SetRES_AVAIL_FROMNull();

availRow.SetRES_AVAIL_TONull();

availRow.SetRES_AVAIL_UNITSNull();

ResourceWeb.ResourceDataSet.ResourcesRow resRow = (ResourceWeb.ResourceDataSet.ResourcesRow)rDS.Resources.Rows[0];

availRow.ResourcesRow = resRow;

 resource.CreateResources(rDS,false, true);

ResourceWeb.ResourceAuthorizationDataSet resourceAuthDs = new ResourceWeb.ResourceAuthorizationDataSet();

ResourceWeb.ResourceAuthorizationDataSet.ResourcesRow resourceAuthRow = resourceAuthDs.Resources.NewResourcesRow();

 resourceAuthRow.RES_UID = resourceRow.RES_UID;

 resourceAuthRow.RES_IS_WINDOWS_USER =false;

resourceAuthRow.WRES_ACCOUNT =“i:0#.f|” + providerName + “|”+ txtUsername.Text; ;

resourceAuthDs.Resources.AddResourcesRow(resourceAuthRow);

 resource.SetResourceAuthorization(resourceAuthDs);

 return true;

}

2 thoughts on “Adding User to Project Server Programmatically

  1. Salam Kashif,

    Can you please reply me back,

    How I can get the following objects created from LoginForms or LoginWindows classes since these classes do not exists in the LoginForms or LoginWindows asmx services.

    LoginWindowsWeb.LoginWindows loginWindows = new LoginWindowsWeb.LoginWindows();

    LoginFormsWeb.LoginForms loginForms = new LoginFormsWeb.LoginForms();

    I really need your help over this doubt…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s