Skip to main content

A Simple How To Guide on Scripting with the P6 API

A Technical Example Demonstrating What you can do With the P6 API

If you are a Java programmer, or if your organization has a Java programmer who is eager to learn about the Primavera API, there is a world of possibilities about what you can do with your P6 database. The P6 API is a powerful solution that lets you interact with the P6 database from a Java application.

The basic code to connect to the API and read a list of projects is very simple:

final Session session = Session.login( null, "1", "admin", "admin" );

final Iterator<Project> projectIterator =
  session.getEnterpriseLoadManager().loadProjects(
    new String[]{"Id","Name"},

  while( projectIterator.hasNext() ) {
    final Project next = projectIterator.next();
    System.out.println( next.getId() + "/ " + next.getName() );

session.logout();

That retrieves all projects from a P6 sample database that start with CORP:

CORP00118/ GIS Interface Project
CORP00307/ Online Invoice Generation Project
CORP00384/ Alliance Portal Integration Project
CORP00424/ Lead Qualification Project
CORP00591/ Order Management Redesign
CORP00595/ Nexus Project
CORP00712/ Cash Flow BI Project
CORP00768/ Logistics Reengineering Program
CORP00852/ eBusiness Transformation Program
CORPTEMPLATE/ Business Process Template

Now … I would like create a baseline for each of the projects above:

final Session session = Session.login( null, "1", "admin", "admin" );

final Iterator<Project> projectIterator =
  session.getEnterpriseLoadManager().loadProjects(
    new String[]{"Id", "Name", "IsTemplate"},
    "Id like '" + WhereClauseHelper.escapeSqlString( "CORP" ) + "%'", "Id" );

  while( projectIterator.hasNext() ) {
    final Project next = projectIterator.next();
    System.out.println( next.getId() + "/ " + next.getName() );
    if( next.getIsTemplate() ) {
      System.out.println( "Project " + next.getId() + " is a template and cannot have a baseline!" ); continue;
    }
    final ObjectId baselineObjectId = next.createCopyAsBaseline();
    final BaselineProject bp = BaselineProject.load( session, new String[]{"Id", "Name"}, baselineObjectId );
    System.out.println( "Project " + next.getId() + " has generated baseline: " + bp.getId() );

session.logout();

A few differences now – first, I am loading the “IsTemplate” field in the project since I need to check if a project is a template or not – a template cannot have a baseline. I then create the baseline with the API method “Project.createCopyAsBaseline()”, which generates the ObjectId of the baseline – the ObjectId is an unique identifier that identifies the internal ID assigned to each object in a P6 database. With the ObjectId, I can load the baseline generated and display its information. The sample program output is shown below:

CORP00103/ Order Fullfillment Phase II
Project CORP00103 has generated baseline: CORP00103 - B2
CORP00118/ GIS Interface Project
Project CORP00118 has generated baseline: CORP00118 - B2
CORP00307/ Online Invoice Generation Project
Project CORP00307 has generated baseline: CORP00307 - B2
CORP00384/ Alliance Portal Integration Project
Project CORP00384 has generated baseline: CORP00384 - B2
CORP00424/ Lead Qualification Project
Project CORP00424 has generated baseline: CORP00424 - B2
CORP00591/ Order Management Redesign
Project CORP00591 has generated baseline: CORP00591 - B2
CORP00595/ Nexus Project
Project CORP00595 has generated baseline: CORP00595 - B2
CORP00712/ Cash Flow BI Project
Project CORP00712 has generated baseline: CORP00712 - B2
CORP00768/ Logistics Reengineering Program
Project CORP00768 has generated baseline: CORP00768 - B2
CORP00852/ eBusiness Transformation Program
Project CORP00852 has generated baseline: CORP00852 - B2
CORPTEMPLATE/ Business Process Template
Project CORPTEMPLATE is a template and cannot have a baseline!

The cool thing about the API is that it allows you to make changes to the baseline even if it is not a project! For example, I will load the baseline project “CORP00103 - B2” and create 10 new activities in its root WBS node. First, I will check how many activities this project has:

final Session session = Session.login( null, "1", "admin", "admin" );

final String baselineProjectName = "CORP00103 - B2";
final Iterator<BaselineProject> bpIterator = session.getEnterpriseLoadManager().loadBaselineProjects(
    new String[]{"Id", "Name"},
    "Id='" + WhereClauseHelper.escapeSqlString( baselineProjectName ) + "'", "Id" );

final BaselineProject bp = bpIterator.next();
final ObjectId bpObjectId = bp.getObjectId();
final Activity[] bpActivities = session.getEnterpriseLoadManager().loadActivities(
  null,
  "ProjectObjectId=" + bpObjectId, null ).getAll();

System.out.println( "Baseline project: " + baselineProjectName + " has " + bpActivities.length + " activities" );

session.logout();

The result of the program above is this:
Baseline project: CORP00103 - B2 has 14 activities

Now I will create the 10 activities:

final Session session = Session.login( null, "1", "admin", "admin" );

final String baselineProjectName = "CORP00103 - B2";
final Iterator<BaselineProject> bpIterator = session.getEnterpriseLoadManager().loadBaselineProjects(
    new String[]{"Id", "Name"},
    "Id='" + WhereClauseHelper.escapeSqlString( baselineProjectName ) + "'", "Id" );
final BaselineProject bp = bpIterator.next();
final ObjectId bpObjectId = bp.getObjectId();

{ final Activity[] bpActivities =
  session.getEnterpriseLoadManager().loadActivities( null, "ProjectObjectId=" + bpObjectId, null ).getAll();
  System.out.println( "Baseline project: " + baselineProjectName + " has " + bpActivities.length + " activities" );
}

for( int i = 0; i < 10; i++ ) {
  final Activity newActivity = new Activity( session );
  newActivity.setProjectObjectId( bpObjectId );
  final ObjectId newActivityOid = newActivity.create();

  final Activity newlyCreatedActivity = Activity.load( session, new String[]{"Id"}, newActivityOid );
  System.out.println( "New activity id: " + newlyCreatedActivity.getId() );
}

{
  final Activity[] bpActivities = session.getEnterpriseLoadManager().loadActivities(
      null,
      "ProjectObjectId=" + bpObjectId,
      null ).getAll();
  System.out.println( "Baseline project: " + baselineProjectName + " has " + bpActivities.length + " activities" );
}

session.logout();

In the for… loop, I create a new activity, assign the only field I need to assign (either the ProjectObjectId or the WBSObjectId), and then I create the activity. When the creation method runs, it returns the ObjectId of the new activity. I use it to load the activity and see which Id P6 is using for it. After the activities are created, I count them in the baseline again. This was done directly in the baseline.

This is the output of the program:

Baseline project: CORP00103 - B2 has 14 activities
New activity id: CP9015
New activity id: CP9025
New activity id: CP9035
New activity id: CP9045
New activity id: CP9055
New activity id: CP9065
New activity id: CP9075
New activity id: CP9085
New activity id: CP9095
New activity id: CP9105
Baseline project: CORP00103 - B2 has 24 activities

This is just a very simple example of what you can do with the API. We can and do much more – you can summarize and schedule projects, progress activities, calculate the spread of resources, and so on. And with expertise and ingenuity, you can do some really cool tricks. We at Emerald Associates are Java, P6 and P6 API specialists. I personally use the API when I need to perform any change in the database that would take more than 15 minutes to make manually, since it does save me time, and after I do that, I have another cool trick in my collection.

The P6 API can be downloaded from the Oracle E-delivery site for the P6 edition that you are using. It requires a P6 EPPM database in order for it to work properly.

We have a team of Integration Specialists at Emerald ready to help you integrate and automate your P6 tasks. We have successfully implemented solutions of all sizes using the P6 API, and our team of specialists contain a vast accumulate knowledge of how to make the P6 API work better and faster.

No video selected.

About the Author

Ravi Wallau - Integration Specialist

Ravi started working with computers as a child, programming with BASIC language on his MSX, a platform once popular outside of North America. In 1998, Ravi started an internship where he further developed his programming skills. After one year, this internship became his first job, where he gained experience developing industrial automation systems.

Over time, Ravi has worked in many different industries and programming languages, developing banking, integration and mobile solutions in C/C++, Java, Visual Basic and C # for a variety of platforms.

From 2007 to 2011, Ravi moved from Brazil, his native country, to Canada with the desire to live and learn another culture, as well as gain experience working in diverse environments.

Since 2009, Ravi has been an important part of the team at Emerald Associates. He has participated in key integration projects with major clients, such as CL&P, Suncor and Nexen, acting as the key technical lead in all phases of the projects. Ravi is also a key contributor at Emerald Associates in the development of unique in-house products such as TAPS, EP-datawarehouse, CAPPS, P6-Auditor and P6-Loader.

Ravi is a dedicated and insightful developer, one who prides himself on the quality of his work. He truly strives to provide optimal functionality so that each piece of a complex integration system fits together to give the end-user what they really need and more.

Leave a comment

Please login to leave a comment.