Project Service Automation(PSA) Programming in Dynamics 365

Project Service Automation(PSA) Programming in Dynamics 365

Dynamics 365 is a cloud-based combination of both ERP and CRM enterprise system built by Microsoft for maximum flexibility and extensibility. And, Project Service Automation (PSA) is a service within Microsoft CRM that is introduced for light project management. PSA includes very interesting features for project management with fluent UI.

This extended service of CRM includes some of the integration and automation scenarios as well. While talking about integration and automation, let’s understand how CRUD operations in PSA can be performed programmatically.

In this blog post, I will discuss the environment set up configuration, adding APIs references, and performing CRUD operation on PSA from custom application.

Step 1: Download the Microsoft Dynamics 365 Software Development Kit (SDK) from the below link:

Step 2: Follow the steps from the below URL for initial setup of Console Application that connects to Microsoft Dynamics CRM 2016 using Developer Extensions and creates a contact record. You can skip creating contact record if you do not want to create new contact records programmatically.

https://msdn.microsoft.com/library/gg695803.aspx

The connection string on above link is a bit complex and I faced issues in connecting with Project Service Automation. If you too face any issue, you can add the below connection string in app.config file:

Step 3: Once the console application is created, in the program.cs file, edit the using statements as this:

using System.Text;

using System.Threading.Tasks;

using System.Configuration;

using Xrm;

using Microsoft.Xrm.Tooling.Connector;

using Microsoft.Xrm.Sdk;

using Microsoft.ProjectServer.Client;

using Microsoft.SharePoint.Client;

using System.Security;

using System.Net;

namespace Dynamic365SDK

{

class Program

{

static void Main(string[] args)

{

CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings[“Xrm"].ConnectionString);

IOrganizationService crmService = crmConn.OrganizationServiceProxy;

var xrm = new XrmServiceContext(“Xrm");

}

}

}

Step 4: You can see the projects listed in PSA.

I have explored how projects, tasks can be automated programmatically. Also, resources can be assigned and time entries can be filled programmatically.

I’m sharing the code to perform the following actions in PSA:

  1. Get list of projects
  2. Create project
  3. Get list of tasks
  4. Create task
  5. Get list of resources of a project
  6. Assign task to a resource
  7. Get time entries of a resource
  8. Enter time entries for a resource

Step 1: Get the list of projects in PSA

The below code will display project name and project ID of all the projects in PSA

private static void GetListOfProjects(XrmServiceContext xrm)

{

var projectslist = xrm.msdyn_projectSet;

foreach (var project in projectslist)

{

Console.WriteLine(“n" + project.Id);

Console.WriteLine(“n" + project.msdyn_subject);

}

Console.ReadLine();

}

Step 2: Create a new project in PSA

With this code you can create a new project in PSA.

private static void
CreateNewProject(
XrmServiceContext xrm)

        {

            var Projects = new msdyn_project

            {


msdyn_projectId =
new Guid(),


msdyn_subject =
“Dynamics
365"
//Project
name that you want to create

            };

            xrm.AddObject(Projects);


xrm.SaveChanges();

        }

Step 3: Get the list of tasks

To get the list of all the tasks of a project, you need to provide the GUID of project to get the task list. Use the below code:

private static void GetListOfTasks(XrmServiceContext xrm)

{

Guid ProjectId = new Guid(“666609d2-9510-e711-ae87-002713bd9b28");

var Tasks = xrm.msdyn_projecttaskSet.Where(T => T.msdyn_project.Id == ProjectId);

foreach (var TaskList in Tasks)

{

Console.WriteLine(“n" + TaskList.msdyn_projecttaskId);

Console.WriteLine(“n" + TaskList.msdyn_subject);

Console.WriteLine(“n" + TaskList.msdyn_project.Id);

}

Console.ReadLine();

}

Step 4: Create a new task

Run the below code to create a new task for a project:

private static void CreateNewTask(XrmServiceContext xrm)

{

Guid ProjectId = new Guid(“666609d2-9510-e711-ae87-002713bd9b28"); //Project ID

var Tasks = xrm.msdyn_projecttaskSet.Where(T => T.msdyn_project.Id == ProjectId);

int count = Tasks.ToList().Count;

Guid taskID = Guid.NewGuid();

var Task = new msdyn_projecttask

{

msdyn_projecttaskId = taskID,

msdyn_subject = “Testing",  //Task name that you want to create

msdyn_scheduledstart = (DateTime.Now),

msdyn_scheduledend = (DateTime.Now.AddDays(3)),

msdyn_autoscheduling = true,

msdyn_WBSID = (count + 1).ToString(),

msdyn_project = new Microsoft.Xrm.Client.CrmEntityReference(“msdyn_project", ProjectId),

msdyn_numberofresources = 1,

msdyn_Effort = 1,

};

xrm.AddObject(Task);

xrm.SaveChanges();

}

Step 5: Get the list of resources of a project

Using below code, you can get the list of resources in a project the team of a project:

private static void GetListOfResources(XrmServiceContext xrm)

{

Guid ProjectId = new Guid(“7982feea-a0f2-e611-8130-e0071b6a92f1"); //Project ID

var Resource = from t in xrm.msdyn_projecttaskSet

join a in xrm.msdyn_resourceassignmentSet

on t.msdyn_projecttaskId equals a.msdyn_taskid.Id

join r in xrm.BookableResourceSet

on a.msdyn_bookableresourceid.Id equals r.BookableResourceId

where t.msdyn_project.Id == ProjectId

select new

{

Task_id = t.msdyn_projecttaskId,

Task_name = t.msdyn_subject,

resource_id = a.msdyn_bookableresourceid.Id,

resource_name = r.Name,

resource_logical = r.LogicalName

};

foreach (var ResourceList in Resource)

{

Console.WriteLine(“n" + ResourceList.Task_id);

Console.WriteLine(“n" + ResourceList.Task_name);

Console.WriteLine(“n" + ResourceList.resource_id);

Console.WriteLine(“n" + ResourceList.resource_name);

Console.WriteLine(“n" + ResourceList.resource_logical);

}

Console.ReadLine();

}

Step 6: Assign task to a resource

Assign task to a resource using the below code:

private static void AssignResourceToTask(XrmServiceContext xrm)

{

Guid ProjectTeamId = new Guid();

Guid TaskId = new Guid(“104264d4-6a14-e711-8114-e0071b6ac161"); //Task ID you want to assign

Guid ResourceId = new Guid(“cd01ae07-b9cd-e611-80e7-c4346bac0910"); //Resource ID

Guid ProjectId = new Guid(“7982feea-a0f2-e611-8130-e0071b6a92f1"); //Project ID

Guid BookingStatusId = new Guid(“d0e57e11-c0cd-e611-80e8-c4346bad367c"); //Hardbook

var ProjectTeam = xrm.msdyn_projectteamSet.Where(PT => PT.msdyn_project.Id == ProjectId && PT.msdyn_bookableresourceid.Id == ResourceId);

foreach (var ProjectTeamList in ProjectTeam)

{

ProjectTeamId = ProjectTeamList.Id;

}

var AssignResource = new msdyn_resourceassignment

{

msdyn_resourceassignmentId = Guid.NewGuid(),

msdyn_bookableresourceid = new Microsoft.Xrm.Client.CrmEntityReference(“bookableresource", ResourceId),

msdyn_taskid = new Microsoft.Xrm.Client.CrmEntityReference(“msdyn_projecttask", TaskId),

msdyn_projectid = new Microsoft.Xrm.Client.CrmEntityReference(“msdyn_project", ProjectId),

msdyn_bookingstatusid = new Microsoft.Xrm.Client.CrmEntityReference(“bookingstatus", BookingStatusId),

msdyn_projectteamid = new Microsoft.Xrm.Client.CrmEntityReference(“msdyn_projectteam", ProjectTeamId)

};

xrm.AddObject(AssignResource);

xrm.SaveChanges();

}

Step 7: Get time entries

Get time entries of a resource, which he has filled in the timesheet:

private static void GetTimeEntries(XrmServiceContext xrm)

{

Guid ResourceId = new Guid(“cd01ae07-b9cd-e611-80e7-c4346bac0910"); //Resource ID

var TimeEntries = xrm.msdyn_timeentrySet.Where(t => t.msdyn_bookableresource.Id == ResourceId);

foreach (var TimeEntryList in TimeEntries)

{

Console.WriteLine(“n" + TimeEntryList.CreatedOn);

Console.WriteLine(“n" + TimeEntryList.msdyn_date);

Console.WriteLine(“n" + TimeEntryList.msdyn_description);

Console.WriteLine(“n" + TimeEntryList.msdyn_duration);

Console.WriteLine(“n" + TimeEntryList.msdyn_project.Name);

Console.WriteLine(“n" + TimeEntryList.msdyn_projectTask.Name);

Console.WriteLine(“========xxxxxx========xxxxxx========xxxxxx========");

}

Console.ReadLine();

}

Step 8: Enter time entries

Enter time entries in the timesheet for a resource, using the below code:

private static void EnterTimeEntries(XrmServiceContext xrm)

{

Guid TaskId = new Guid(“104264d4-6a14-e711-8114-e0071b6ac161"); //Task ID

Guid ResourceId = new Guid(“cd01ae07-b9cd-e611-80e7-c4346bac0910"); //Resource ID

Guid ProjectId = new Guid(“7982feea-a0f2-e611-8130-e0071b6a92f1"); //Project ID

var TimeEntry = new msdyn_timeentry

{

Id = Guid.NewGuid(),

msdyn_date = Convert.ToDateTime(“4/4/2017"),

msdyn_duration = 60

};

xrm.AddObject(TimeEntry);

xrm.SaveChanges();

}

These lines of code can help you fetch data from PSA and use it in automation, integration or reporting as per your need. Hope you find this information helpful, and feel free to comment if you would like to discuss more on this.

Let's connect to make technology work for you.





Project Portfolio ManagementCloud AdoptionAdaptive BIDigital WorkspaceConsulting

Please leave this field empty.

Posted by Preksha

Related Posts:

No Comments

Leave a Reply

Your email address will not be published.

*
*

17 − 16 =