-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateResourceDelegations.cs
More file actions
68 lines (59 loc) · 3.25 KB
/
CreateResourceDelegations.cs
File metadata and controls
68 lines (59 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.ServiceModel;
namespace CF.MI.ConsoleApp
{
class ResourceDelegations
{
/// <summary>
/// Create Resource Delegations for all Bookable resources
/// </summary>
/// <param name="client">CRM Service Client object</param>
private static void CreateResourceDelegations(CrmServiceClient _client)
{
//// GUID of the the Person/ Bookable Resource to delegate the Time entries - Usually Resource manager or HR
Guid DelegateToId = new Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
#region Get Bookable Resources
string userBookableResourcesFetch = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='bookableresource'>
<attribute name='name' />
<attribute name='msdyn_organizationalunit' />
<attribute name='msdyn_targetutilization' />
<attribute name='bookableresourceid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='resourcetype' operator='eq' value='3' />
</filter>
<link-entity name='systemuser' from='systemuserid' to='userid' visible='false' link-type='outer' alias='user'>
<attribute name='title' />
</link-entity>
</entity>
</fetch>";
EntityCollection userBookableResources = _client.RetrieveMultiple(new FetchExpression(userBookableResourcesFetch));
#endregion
#region Create Delegations for all User Bookable Resources to Particular person
foreach (Entity bookableResource in userBookableResources.Entities)
{
Entity TimeEntryDelegation = new Entity("msdyn_delegation");
TimeEntryDelegation["msdyn_delegationfrom"] = new EntityReference("bookableresource", bookableResource.Id);
TimeEntryDelegation["msdyn_delegationto"] = new EntityReference("bookableresource", DelegateToId);
TimeEntryDelegation["msdyn_startdate"] = new DateTime(2018, 10, 1);
TimeEntryDelegation["msdyn_enddate"] = new DateTime(2022, 12, 31);
TimeEntryDelegation["msdyn_type"] = new OptionSetValue(192350000); //Time Entry
TimeEntryDelegation["msdyn_name"] = string.Format("Delegation to HR for {0}", bookableResource.GetAttributeValue<string>("name"));
try
{
Guid DelegationId = _client.Create(TimeEntryDelegation);
}
catch (FaultException<OrganizationServiceFault> ex)
{
Console.WriteLine("Resource: " + bookableResource.GetAttributeValue<string>("name") + " Error: " + ex.Message);
}
}
#endregion
}
}
}