Dynamics 365 is a cloud-based ERP and CRM enterprise system that was built by Microsoft for maximum flexibility and extensibility. With programming, we can access Dynamics 365 data and create SSRS reports or Power BI reports. Also, we can integrate this fetched data with any other tool like Project Online.

For doing any kind of custom development in Dynamics 365, you need to follow these steps:

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

https://www.microsoft.com/en-us/download/details.aspx?id=50032

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

If you face any issue in the connection string, 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 below:

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: Now, follow these below steps to automate the creation of project, task, assigning task to a resource, and time entries.

Step 4.1  Get the list of projects in Dynamics 365:

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 4.2  Create a new project in Dynamics 365:

Run the code below and project will be automatically created in Dynamics 365

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 4.3 Get the list of tasks:

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.4  Create a new task:

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 4.5  Get the list of resources 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 4.6  Assign task to a resource:

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 4.7  Get time entries:

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 4.8  Enter time entries:

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();

}

This above code can help you fetch data from PSA and use it in 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.

Posted by Advaiya

    Let's connect to make technology work for you.





    Please tick the options most relevant to your business challenges
    Decision makingBusiness productivityCustomer experienceTechnology-led innovationDigital transformation


    [By using this form you agree with the storage and handling of your data by this website.]