Monday, June 9, 2008

Create a project in project server programmatically

Here is the way how programmatically create a project in project server 2007 (PWA) step by step using PSI.

For creating a project you have to use “_vti_bin/psi/Project.asmx” web service.
To use any PSI web service first you have to login to the server. Check my previous post

Project server has predefined database structure.
So for creating a new project we need t o create a new row in project table.
This row has many columns but only few are required for input and others are non mandatory or filled atomically.

Here are that fields
PROJ_UID : new GUID for project.
PROJ_NAME : name of the project which should be unique.
PROJ_INFO_START_DATE : it is not mendatory but every project must have start date other wuse it will take system date.
PROJ_TYPE : this is important. There are two type of porject in project server one is project and other is Template. For project its value is 0 and for template its value is 1. So we are using 0 as we are creating a project.

then this field value in row we have to add this row in project table.

After that project.asmx have a method called QueueCreateProject.
This method will create a project in draft database and push the argument in Project server Queue.

After generating project in draft database we need to publish that project.
For publishing project we have to call QueuePublish method from Project.asmx
Note: Only published project is available to user in PWA.

And last operation after publishing project we need to do is “check in the project”
Checked out project is visible to user but cannot be editable by any one. For check in the project there is also another method in project.asmx QueueCheckInProject
And as per experienced developer we have to logofffrom PSI server.
Here is the snippet for creating project in project server (PWA).
private WebSvcProject.ProjectDataSet dsProjectDataSet = null;
private Project objProject = null;
private bool CreateProject()
{

try
{
//for creating a new we need to create a new row in project table of project dataset
WebSvcProject.ProjectDataSet.ProjectRow projectRow = dsProjectDataSet.Project.NewProjectRow();
Guid _projectGUID = Guid.NewGuid(); // new GUID for each project
projectRow.PROJ_UID = _projectGUID;
projectRow.PROJ_NAME = "My new Project";
projectRow.PROJ_INFO_START_DATE = System.DateTime.Now;
projectRow.PROJ_TYPE = 0; //0 is for project and 1 is for template
dsProjectDataSet.Project.AddProjectRow(projectRow);

//create a project using project.asmx
objProject.QueueCreateProject(Guid.NewGuid(), dsProjectDataSet,
false);

//after creating project by default it is in draft
//we must have to publish the project
objProject.QueuePublish(Guid.NewGuid(), _projectGUID, true, string.Empty);

//when project is created bu default it is checked out
// we must checkin the project.
objProject.QueueCheckInProject(Guid.NewGuid(), _projectGUID, true, Guid.Empty, "Checking by system");

return true;
}
catch (Exception ex)
{
//log error
return false;
}
}

5 comments:

Kit said...

Thank you for this post. It is all working fine for me until I get to objProject.QueueCreateProject(Guid.NewGuid(), dsProjectDataSet, false);

I get the following error:

System.Net.WebException: The request failed with the error message:
--
Object moved to ...
__

Do you have any ideas? Thank you in advance for your help, kit_mx1@hotmail.com

SharePoint Kings said...

Kit,

can you check error log of project server that might be explain more then this error.

because with only this error we can not get anything.

Anonymous said...

Thank you for your response. Could you please point me to where I might find the project server 2007 error log? I am trying to call the web service from within a SharePoint event handler. Thank a bunch.

Anonymous said...

I am looking for simliar solution to call web service from event handler. Is that possible?

SharePoint Kings said...

Anonymous,
yes it is possible but just be careful that calling web service will take time, so using async method is advisable.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com