Getting a Project GUID on the Cheap

Getting a Project GUID on the Cheap

  • Comments 1

Many people have requested for a complementary method to GetProjectNameFromProjectUid where they can pass in a project name and it returns the project's GUID. Unfortunately it did not make it into the Project API. Since it has been requested many times, I figured that it would be useful to post a method, GetProjectUidFromProjectName, which did just that:

public static Guid GetProjectUidFromProjectName(string projectName)
{
  Guid projectGUID;

  WSProject.ProjectDataSet readProjDs = projWS.ReadProjectStatus(
                                                                                            Guid.Empty,
                                                                                            WSProject.DataStoreEnum.WorkingStore, 
                                                                                            projectName, 
                                                                                            0
                                                                                                     );

  if (readProjDs.Project.Rows.Count == 1)
  {
    projectGUID = new Guid(readProjDs.Project[0].PROJ_UID.ToString());
  }
  else
  {
    throw new Exception("No Project by the name: " + projectName + " Found");
  }

  return projectGUID;

}  

First thing to note is that projWS is defined elsewhere and it is a connection to the Project Web Service. See my earlier post on Getting Started with the PSI on how to make the connection to the Project Web Service.

To get the GUID we are going to use the ReadProjectStatus method, as it is the cheapest call to make to get the project's GUID. As you will see, this method takes 4 parameters. The first parameter is the project GUID. In this case, we pass in an empty GUID because that is what we are looking for. Passing in an empty GUID tells Project Server not to search for a project by the GUID.  

The second parameter is the store to get the project GUID from. Here we have the option of getting the GUID from the working, published or archived store. In this example, I am getting it from the working store, since I want to get the GUID for projects that are actively being worked on and may or may not have been published. Note that this will also get projects that have been published, since they exist in both stores with the same GUID. You may want to change the store based on your requirements.

The third parameter is the name of the project. This is passed into our GetProjectUidFromProjectName method.

The last parameter is the type of project. There a number of different project types:

http://msdn2.microsoft.com/en-us/library/microsoft.office.project.server.library.project.projecttype.aspx

In most cases 0, which represents standard projects, will be the correct project type.

The ReadProjectStatus returns a project dataset. If the number of rows returned for the project table is equal to 1, than we have found a project with the given name and we can return the GUID for the project. If the number of rows is not equal to one, than no project by the name was found and we throw an exception.

Hope this helps,

Chris Boyd

Leave a Comment
  • Please add 2 and 7 and type the answer here:
  • Post
  • Great method, just saved at least a hour of swearing before i'd have got it right...definetely made my morning! Thanks!

    Cheers

    Hristiyan

Page 1 of 1 (1 items)