martes, 1 de abril de 2008

Create a new Project from a template

Today, in our first post, we are going to learn how to crate a new project from a template using PSI.
PSI are web services wich allow us to connect with Project Server.
Let's see how to use them.
First open a new project and import PSI web references. The first one you need is project.asmx
You can find it in your server under the Project Web Access site.
http://servername/pwa/_vti_bin/PSI/project.asmx
Once you have imported it you can create new projects from a template.

Variables
server --> name of the server.
PROJECTWEBSERVICE --> Project web service adress

1. Create and configure connection
Project.Project projWS;
if (projWS == null)
{
projWS = new Project.Project();
projWS.Url = server + PROJECTWEBSERVICE;
projWS.CookieContainer = new CookieContainer();
projWS.Credentials = CredentialCache.DefaultCredentials;
}

2. Implement functions to get UIDs from templates
private Guid GetProjectTemplateGUID(string templateName) {
Guid tempateGuid = Guid.NewGuid();
Project.ProjectDataSet myProjectList = projWS.ReadProjectList();
foreach (DataRow row in myProjectList.Project) {
if ((string)row["PROJ_NAME"] == templateName) {
tempateGuid = (Guid)row["PROJ_UID"];
return tempateGuid;
}
}
return tempateGuid;
}

3. Create the project from the template

Guid plantillaid = GetProjectTemplateGUID(plantilla.Text);
Guid newProject = Guid.Empty;
newProject = projWS.CreateProjectFromTemplate(plantillaid, nombre.Text);

4. Once you have your new project Created let's change some of its information

4.1 Declare variables to read the project

Project.ProjectDataSet templateProject;
Guid jobGuid = Guid.NewGuid();
Guid sessionGuid = Guid.NewGuid();

4.2 Check-out the project to read it's properties

projWS.CheckOutProject(newProject, sessionGuid, "");
templateProject = projWS.ReadProject(newProject, Project.DataStoreEnum.WorkingStore);

4.3 Create a temp DataSet to store the changes.
//Hemos creado el proyecto, cambiamos los parámetros según los introducidos ProjectDataSet temporal = ((Project.ProjectDataSet)(templateProject.Copy()));

4.4 Change the start date of the project

temporal.Tables[temporal.Project.TableName].Rows[0][temporal.Project.PROJ_INFO_START_DATEColumn] = fechaInicio.SelectedDate;

4.5 Change the owner of the project

Guid propietario = GetResourceGuid(realUser);
if (propietario != Guid.Empty)
temporal.Tables[temporal.Project.TableName].Rows[0][temporal.Project.ProjectOwnerIDColumn] = propietario.ToString();
ProjectDataSet updateProjectDataSet = new ProjectDataSet(); updateProjectDataSet.Project.Merge(temporal.Tables[temporal.Project.TableName], true);
updateProjectDataSet = ((Project.ProjectDataSet)updateProjectDataSet.GetChanges(DataRowState.Modified));

4.6 If there are any changes --> store them

if (updateProjectDataSet != null)
{
projWS.QueueUpdateProject(jobGuid, sessionGuid, updateProjectDataSet, false);
WaitForQueueJobCompletion(jobGuid, 1);
}

4.7 Check-in and publish the project

jobGuid = Guid.NewGuid();
projWS.QueueCheckInProject(jobGuid, newProject, true, sessionGuid,"");
WaitForQueueJobCompletion(jobGuid, 1);
jobGuid = Guid.NewGuid();
projWS.QueuePublish(jobGuid, newProject, true, "");
WaitForQueueJobCompletion(jobGuid, 1);

No hay comentarios: