Post

1 follower Follow
0
Avatar

Listing ONLY active projects with PHP and the API

We are creating a tool internally for tracking our developer's time. This tool needs to be able to add a task to a currently active project and assign it to the correct developer. I am able to pull all of the projects that we have, but I am NOT able to pull only the active projects to get the hashed IDs.

Any assistance with this would be greatly appreciated!

Here is what I have so far:

 

<?php

//LOGIN PROCCESS

$soapUrl = 'https://api.clarizen.com/v1.0/Clarizen.svc?WSDL';

$soapApiUrl = 'http://clarizen.com/api';

$soapConfig = array('exceptions' => 1);

$request = array();

$params = array(

'userName' => 'UserName',

'password' => 'PassWord'

);

$client = new SoapClient($soapUrl, $soapConfig);

$response = $client->Login($params);

$sessionId = $response->LoginResult->SessionId;

$userId = $response->LoginResult->UserId;

//Create a SOAP header containing the session ID for future requests

$header = new SoapHeader($soapApiUrl, 'Session', array("ID"=>$sessionId));

$client->__setSoapHeaders($header);

$userQuery->TypeName = 'Project';

//Select the fields you want retrieved from that entity

$userQuery->Fields = array('Name', 'State');

 

$request[] = new SoapVar($userQuery, SOAP_ENC_OBJECT, 'EntityQuery', 'http://clarizen.com/api/queries');

//Execute the request

$result = $client->Execute(array("request"=>$request));

echo "<pre>";

print_r($result);

echo "</pre>";

 

James Celli Answered

Please sign in to leave a comment.

11 comments

0
Avatar

I was able to get it to list all of the active Projects, and I was able to add a task. However I am not able to assign it to a particular Project. Here is what I have for Creating a Task:

Where/How do I assign it to a Project if I have the hashed Project ID?

//Create an entity representing the new task NEW TASK CREATION

$newTask = new stdClass();

$newTask->Id = new stdClass();

$newTask->Id->TypeName = 'Issue';

$newTask->Id->Value = null;

//Set issue name

$nameField = new stdClass();

$nameField->FieldName = "Title";

$nameField->Value = new SoapVar("Test Issue", XSD_STRING, "This is just a test", "http://www.w3.org/2001/XMLSchema");

//Set task start date

$startDateField = new stdClass();

$startDateField->FieldName = "DueDate";

$date = '2014-10-31';

$startDateField->Value = new SoapVar($date, XSD_DATETIME, "dateTime", "http://www.w3.org/2001/XMLSchema");

//Assign the fields to the task

$newTask->Values = array($nameField, $startDateField);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', $soapApiUrl);

$result = $client->Execute(array("request"=>$request));

James Celli 0 votes
0
Avatar

I was finally able to add a task to a project. Now I need to be able to add resources to it... i.e. Developer that is assigned to it. Here is the code snippet that worked:

 

//Create an entity representing the new task

$newTask = new stdClass();

$newTask->Id = new stdClass();

$newTask->Id->TypeName = 'Task';

$newTask->Id->Value = null;

//Set Task name

$nameField = new stdClass();

$nameField->FieldName = "Name";

$nameField->Value = new SoapVar("Test Task", XSD_STRING, "string",

"http://www.w3.org/2001/XMLSchema");

//Set task start date

$startDateField = new stdClass();

$startDateField->FieldName = "DueDate";

$date = '2014-20-11';

$startDateField->Value = new SoapVar($date, XSD_DATETIME, "dateTime",

"http://www.w3.org/2001/XMLSchema");

//Assign the fields to the task

$newTask->Values = array($nameField, $startDateField);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT,

"GenericEntity", 'http://clarizen.com/api');

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage','http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

$new_id = $result -> ExecuteResult -> Result -> Id;

$responseJSON = json_encode($new_id);

echo $responseJSON . "<br/><br/>";

// Link

$link = new stdClass();

$link->Id = new stdClass();

$link->Id->TypeName = 'WorkItemHierarchyLink';

$parent_project = array ("TypeName"=>"Project", "Value" => "52b8b0bf-fdc4-467d-879a-cf9219b79239");

$parent = new stdClass();

$parent->FieldName = 'Parent';

$parent->Value = new SoapVar($parent_project, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

$child = new stdClass();

$child->FieldName = 'Child';

$child->Value = new SoapVar($new_id, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

$link->Values = array($parent, $child);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($link, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

$request = array();

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

James Celli 0 votes
0
Avatar

Hi James,

You're almost there. All you need to do is to add a RegularResourceLink with the WorkItem field containing the id of the new task and the Resource _Field containing the Id of the user. The rest is pretty much the same as adding the _WorkItemHierarchyLink you've already accomplished.

BTW, I recommend you specify the Parent as a field (with the Id of the project) within the fields of the Task on the appropriate CreateMessage. _That's a better alternative to adding a _WorkItemHierarchyLink.

Hope this helps,

Ophir

 

Ophir Kenig 0 votes
0
Avatar

Hey Ophir, 

Thank you for that, but do you have an example of what the code needs to look like? I have tried every combination that I can think of and none of them are working. Thank you for your assistance.

James

James Celli 0 votes
0
Avatar

Here is what I have tried, however it is not working and I am not getting any errors. I also need to add the the hours that we estimate to the Work column as well. 

$resourceAssign = new stdClass();

$resourceAssign->Id = new stdClass();

$resourceAssign->Id->TypeName = 'RegularResourceLink';

$workitem = array ("TypeName"=>"WorkItem", "Value" => "d0d5cb1a-d096-43da-a414-c22bba3d184b");

$resource = array ("TypeName"=>"Resource", "Value" => "96dddd72-6557-4fa0-9fbc-cbc9abd1d17c");

$resourceAssign->Values = array($workitem, $resource);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($resourceAssign, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

$request = array();

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

 

Thanks

James

James Celli 0 votes
0
Avatar

Hi James,

You may have noticed your latest code for adding a RegularResourceLink is different from the one you used for  WorkItemHierarchyLink (which was good). The Values collection expects an array of FieldValue e.g. FieldName + Value

I'm a bit surprised there were no errors neither from the parser nor from Clarizen (responseJSON).

You may specify Work as field (don't forget the FieldName) on the link with a value type of Duration, check this post for a sample:

https://success.clarizen.com/entries/100468853-Query-to-insert-work-hours-in-Work-field-in-PHP

Note: specifying Work on the link should work only if the work item is a 'leaf' and specifies Individual for the Progress Reporting Policy. 

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

What if I wanted to add the Resource to the creation of the task. Here is what I am playing with, but it appears that the following does not have the correct FieldValue setup:

$developer_value = array ("TypeName"=>"Resource", "Id" => "dee713c4-a50b-4eec-8d3f-98040078c521");

$developer = new stdClass();

$developer->FieldName = 'Resource';

$developer->Value = new SoapVar($developer_value, SOAP_ENC_OBJECT, "User","http://clarizen.com/api");

 

Here is all of the code for the script that I am developing:

 

<?php

//LOGIN PROCCESS

$soapUrl = 'https://api.clarizen.com/v1.0/Clarizen.svc?WSDL';

$soapApiUrl = 'http://clarizen.com/api';

$soapConfig = array('exceptions' => 1);

$request = array();

$params = array(

'userName' => 'alan@verite.com',

'password' => 'r0cc0riley'

);

$client = new SoapClient($soapUrl, $soapConfig);

$response = $client->Login($params);

$sessionId = $response->LoginResult->SessionId;

$userId = $response->LoginResult->UserId;

//Create a SOAP header containing the session ID for future requests

$header = new SoapHeader($soapApiUrl, 'Session', array("ID"=>$sessionId));

$client->__setSoapHeaders($header);

//Create an entity representing the new task

$newTask = new stdClass();

$newTask->Id = new stdClass();

$newTask->Id->TypeName = 'Task';

$newTask->Id->Value = null;

//Set Task name

$nameField = new stdClass();

$nameField->FieldName = "Name";

$nameField->Value = new SoapVar("Test Task", XSD_STRING, "string",

"http://www.w3.org/2001/XMLSchema");

//Set task start date

$startDateField = new stdClass();

$startDateField->FieldName = "DueDate";

$date = '2014-11-22';

$startDateField->Value = new SoapVar($date, XSD_DATETIME, "dateTime",

"http://www.w3.org/2001/XMLSchema");

//Adds time to the task.

$duration_value = array ("Unit"=>"Hours", "Value" => "2");

$duration = new stdClass();

$duration->FieldName = 'Work';

$duration->Value = new SoapVar($duration_value, SOAP_ENC_OBJECT, "Duration","http://clarizen.com/api");

//Assigns the Developer to the task.

$developer_value = array ("TypeName"=>"Resource", "Id" => "dee713c4-a50b-4eec-8d3f-98040078c521");

$developer = new stdClass();

$developer->FieldName = 'Resource';

$developer->Value = new SoapVar($developer_value, SOAP_ENC_OBJECT, "User","http://clarizen.com/api");

//Assign the fields to the task

$newTask->Values = array($nameField, $startDateField, $duration, $developer);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT,

"GenericEntity", 'http://clarizen.com/api');

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage','http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

$new_id = $result -> ExecuteResult -> Result -> Id;

$responseJSON = json_encode($new_id);

echo $responseJSON . "<br/><br/>";

// Link

$link = new stdClass();

$link->Id = new stdClass();

$link->Id->TypeName = 'WorkItemHierarchyLink';

$parent_project = array ("TypeName"=>"Project", "Value" => "52b8b0bf-fdc4-467d-879a-cf9219b79239");

$parent = new stdClass();

$parent->FieldName = 'Parent';

$parent->Value = new SoapVar($parent_project, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

$child = new stdClass();

$child->FieldName = 'Child';

$child->Value = new SoapVar($new_id, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

$link->Values = array($parent, $child);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($link, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

$request = array();

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

/*

$resourceAssign = new stdClass();

$resourceAssign->Id = new stdClass();

$resourceAssign->Id->TypeName = 'RegularResourceLink';

$workitem = array ("TypeName"=>"WorkItem", "Value" => "d0d5cb1a-d096-43da-a414-c22bba3d184b");

$resource = array ("TypeName"=>"Resource", "Value" => "96dddd72-6557-4fa0-9fbc-cbc9abd1d17c");

$resourceAssign->Values = array($workitem, $resource);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($resourceAssign, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

$request = array();

$request[] = new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

*/

/*echo "<pre>";

print_r($result);

echo "</pre>";*/

//ID of the new test task that was created

/*{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"Task","Value":"d0d5cb1a-d096-43da-a414-c22bba3d184b"}}}}

{"TypeName":"Task","Value":"d0d5cb1a-d096-43da-a414-c22bba3d184b"}

{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"RealWorkItemHierarchyLink","Value":"95644fb0-6e54-4ad7-8ec7-680682c384e6"}}}} */

?>

 

James Celli 0 votes
0
Avatar

Hi James.

What you're trying to do is not supported. There's no field 'Resource' for Task (the relationship between Task and User is many <-> many, so such a field wouldn't be enough for work items with multiple resources).

Fields available for each entity can found on 'Settings -> Configure' page in Clarizen, or retrieved programmatically using DescribeEntitiesMessage.

You do have to create a RegularResourceLink in a separate CreateMessage

You can, however bundle the Task and RegularResourceLink create messages in a single Execute call (the request parameter expects an array of message), or even include both in a TransactionMessage.

I also suggest you check the Clarizen REST API. Personally I find work with PHP & Clarizen SOAP API quite cumbersome.

The Clarizen REST API may be a better alternative for you. 

Hope this helps,

Ophir

Ophir Kenig 0 votes
0
Avatar

Ok, I have gotten a little further. The script will create the task under the correct project, and assign the duration to the task. However, with the following code it will NOT assign the Resource to the Task:

$link = new stdClass();

$link->Id = new stdClass();

$link->Id->TypeName = 'RegularResourceLink';

$parent_project = array ("TypeName"=>"Task", "Value" => $new_id);

$parent = new stdClass();

$parent->FieldName = 'Parent';

$parent->Value = new SoapVar($new_id, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

$child_project = array ("Resource"=>"User", "ID" => "560d5ff8-8ed9-424d-9290-8e1e4869a1c1");

$child = new stdClass();

$child->FieldName = 'Child';

$child->Value = new SoapVar($child_project, SOAP_ENC_OBJECT, "WorkItem","http://clarizen.com/api");

$link->Values = array($parent, $child);

$createMesage = new stdClass();

$createMesage->Entity = new SoapVar($link, SOAP_ENC_OBJECT, "Resource", 'http://clarizen.com/api');

$request = array();

$request[] = new SoapVar($createMesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

 

I am not getting any errors when I run the script, here is the output that it is giving me:

{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"Task","Value":"fe80abd9-df22-47f7-948f-e615cf94408d"}}}}

{"TypeName":"Task","Value":"fe80abd9-df22-47f7-948f-e615cf94408d"}

{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"RealWorkItemHierarchyLink","Value":"2af3d1a9-f0b5-49ad-a183-d807c5664ae7"}}}}

Any help on completing this would be greatly appreciated.

 

James Celli 0 votes
0
Avatar

I have also tried the following and it is not giving me any errors:

 

echo "step1<br/>";

$developer_value = array ("User"=>"Id", "Value" => "ff29ef3b-51c6-4a93-9701-19db9f296e13");

$developer = new stdClass();

$developer->FieldName = 'User';

$developer->Value = new SoapVar($developer_value, SOAP_ENC_OBJECT, "User","http://clarizen.com/api");

echo "step2<br/>";

//Assign the fields to the task

$newTask->Values = array($developer);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT,"GenericEntity", 'http://clarizen.com/api');

$request[] = new SoapVar($createMesage, SOAP_ENC_OBJECT, 'CreateMessage','http://clarizen.com/api');

echo "step3<br/>";

$link = new stdClass();

$link->Id = new stdClass();

$link->Id->TypeName = 'RegularResourceLink';

$parent_project = array ("TypeName"=>"Task", "Value" => $new_id);

$parent = new stdClass();

$parent->FieldName = 'Parent';

$parent->Value = new SoapVar($parent_project, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

echo "step4<br/>";

$child = new stdClass();

$child->FieldName = 'Child';

$child->Value = new SoapVar($developer, SOAP_ENC_OBJECT, "CreateMessage","http://clarizen.com/api");

echo "step5<br/>";

$link->Values = array($parent, $child);

$createMesage = new stdClass();

$createMesage->Entity = new SoapVar($link, SOAP_ENC_OBJECT, "GenericEntity", 'http://clarizen.com/api');

echo "step6<br/>";

$request = array();

$request[] = new SoapVar($createMesage, SOAP_ENC_OBJECT, 'CreateMessage', 'http://clarizen.com/api');

echo "step7<br/>"; // Script breaks from here down.

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

?>

Here is the output of that part of the script after running:

 

{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"Task","Value":"ad0788b4-e347-4790-9900-f76018124207"}}}}

{"TypeName":"Task","Value":"ad0788b4-e347-4790-9900-f76018124207"}

{"ExecuteResult":{"Result":{"Error":null,"Success":true,"Id":{"TypeName":"RealWorkItemHierarchyLink","Value":"3f0826d5-2648-441d-b209-72ce62cac899"}}}}

step1

step2

step3

step4

step5

step6

step7

I have placed the Numbered steps in the code to see where it is breaking and it will get to the last one, but if I move the step 7 1 line lower in the code it will NOT display it. 

 

James Celli 0 votes
0
Avatar

Hi James. Try the following code to create a task with a parent and a resource link.

Hope this helps,

Ophir

// Create GUID (for the task id)

function GUID()

{

if (function_exists('com_create_guid') === true)

{

return trim(com_create_guid(), '{}');

}

return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));

}

// Create task

//Create an entity representing the new task

$TaskUniqueId = GUID();

$newTask = new stdClass();

$newTask->Id = new stdClass();

$newTask->Id->TypeName = 'Task';

$newTask->Id->Value = $TaskUniqueId;

//Set Task name

$nameField = new stdClass();

$nameField->FieldName = "Name";

$nameField->Value = new SoapVar("My Task", XSD_STRING, "string",

"http://www.w3.org/2001/XMLSchema");

//Set task start date

$startDateField = new stdClass();

$startDateField->FieldName = "DueDate";

$date = '2015-05-01';

$startDateField->Value = new SoapVar($date, XSD_DATETIME, "dateTime", "http://www.w3.org/2001/XMLSchema");

$parent_project = array ("TypeName"=>"Project", "Value" => "641534f5-43d2-464e-8f46-b8c20cced8ac");

$parent = new stdClass();

$parent->FieldName = 'Parent';

$parent->Value = new SoapVar($parent_project, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

//Assign the fields to the task

$newTask->Values = array($nameField, $startDateField, $parent);

$createMeesage = new stdClass();

$createMeesage->Entity = new SoapVar($newTask, SOAP_ENC_OBJECT,"GenericEntity", 'http://clarizen.com/api');

//Create an entity representing the Resource Link

$newResourceLink = new stdClass();

$newResourceLink->Id = new stdClass();

$newResourceLink->Id->TypeName = 'RegularResourceLink';

$newResourceLink->Id->Value = null;

//Set the Work Item Field

$WorkItemId = array ("TypeName"=>"Task", "Value" => $TaskUniqueId);

$WorkItemField = new stdClass();

$WorkItemField->FieldName = 'WorkItem';

$WorkItemField->Value = new SoapVar($WorkItemId, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

//Set the Resource Field

$ResourceId = array ("TypeName"=>"User", "Value" => "3ced92be-d3ec-4d2f-abbd-85bc18eaa8b1");

$ResourceField = new stdClass();

$ResourceField->FieldName = 'Resource';

$ResourceField->Value = new SoapVar($ResourceId, SOAP_ENC_OBJECT, "EntityId","http://clarizen.com/api");

//Assign the fields to the task

$newResourceLink->Values = array($ResourceField, $WorkItemField);

$createResourceLinkMeesage = new stdClass();

$createResourceLinkMeesage->Entity = new SoapVar($newResourceLink, SOAP_ENC_OBJECT,"GenericEntity", 'http://clarizen.com/api');

$request = array (

new SoapVar($createMeesage, SOAP_ENC_OBJECT, 'CreateMessage','http://clarizen.com/api'),

new SoapVar($createResourceLinkMeesage, SOAP_ENC_OBJECT, 'CreateMessage','http://clarizen.com/api'));

$result = $client->Execute(array("request"=>$request));

$responseJSON = json_encode($result);

echo $responseJSON . "<br/><br/>";

$new_id = $result -> ExecuteResult -> Result[0] -> Id;

$responseJSON = json_encode($new_id);

$new_id = $result -> ExecuteResult -> Result[1] -> Id;

$responseJSON2 = json_encode($new_id);

echo $responseJSON . "<br/><br/>";

echo $responseJSON2 . "<br/><br/>";

Ophir Kenig 0 votes