In this article , I am going to explain how to Schedule a resource
For Better Understanding I divided this article in multiple parts
(i) Create the van resource
(ii) Create the contraints for creating the plumber resource group
(iii) Create the plumber resource group
(iv) Create the resource specification
(v) Create the plumber required resource object
(vi) Create the service for the equipment
(vii) Create the van required resource object
(viii) Create the appointment request
(ix) Verify that the resources have been scheduled
Namespace need to include
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
Create the van resource
Equipment van = new Equipment
{
Name = "Van 1",
TimeZoneCode = 1,
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId)
};
Guid _vanId = _serviceProxy.Create(van);
Create the contraints for creating the plumber resource group
System.Text.StringBuilder builder = new System.Text.StringBuilder("<Constraints>");
builder.Append("<Constraint>");
builder.Append("<Expression>");
builder.Append("<Body>resource[\"Id\"] == ");
builder.Append(userResponse.UserId.ToString("B"));
builder.Append(" || resource[\"Id\"] == ");
builder.Append(_vanId.ToString("B"));
builder.Append("</Body>");
builder.Append("<Parameters>");
builder.Append("<Parameter name=\"resource\" />");
builder.Append("</Parameters>");
builder.Append("</Expression>");
builder.Append("</Constraint>");
builder.Append("</Constraints>");
Create the plumber resource group
ConstraintBasedGroup group = new ConstraintBasedGroup
{
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId),
Name = "Plumber with Van 1",
Constraints = builder.ToString(),
GroupTypeCode = new OptionSetValue(ConstraintBasedGroupTypeCode.Static),
};
Guid _groupId = _serviceProxy.Create(group);
Create the resource specification
ResourceSpec spec = new ResourceSpec
{
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId),
ObjectiveExpression = @"
<Expression>
<Body>udf ""Random""(factory,resource,appointment,request,leftoffset,rightoffset)</Body>
<Parameters>
<Parameter name=""factory"" />
<Parameter name=""resource"" />
<Parameter name=""appointment"" />
<Parameter name=""request"" />
<Parameter name=""leftoffset"" />
<Parameter name=""rightoffset"" />
</Parameters>
<Properties EvaluationInterval=""P0D"" evaluationcost=""0"" />
</Expression>",
RequiredCount = 1,
Name = "Test Spec",
GroupObjectId = _groupId
};
Guid _specId = _serviceProxy.Create(spec);
Create the plumber required resource object
RequiredResource plumberReq = new RequiredResource
{
ResourceId = userResponse.UserId, // assume current user is the plumber
ResourceSpecId = _specId
};
Create the service for the equipment
Service plumberService = new Service
{
Name = "Plumber 1",
Duration = 60,
InitialStatusCode = new OptionSetValue(1),
Granularity = "FREQ=MINUTELY;INTERVAL=15;",
ResourceSpecId = new EntityReference(ResourceSpec.EntityLogicalName, _specId)
};
Guid _plumberServiceId = _serviceProxy.Create(plumberService);
Create the van required resource object
RequiredResource vanReq = new RequiredResource
{
ResourceId = _vanId,
ResourceSpecId = _specId
};
Create the appointment request
AppointmentRequest appointmentReq = new AppointmentRequest
{
RequiredResources = new RequiredResource[] { vanReq },
Direction = SearchDirection.Backward,
Duration = 60,
NumberOfResults = 10,
ServiceId = _plumberServiceId,
// The search window describes the time when the resouce can be scheduled.
// It must be set.
SearchWindowStart = DateTime.Now.ToUniversalTime(),
SearchWindowEnd = DateTime.Now.AddDays(7).ToUniversalTime(),
UserTimeZoneCode = 1
};
Verify that the resources have been scheduled
SearchRequest search = new SearchRequest
{
AppointmentRequest = appointmentReq
};
SearchResponse searched = (SearchResponse)_serviceProxy.Execute(search);
Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
serverConfig.HomeRealmUri,
serverConfig.Credentials,
serverConfig.DeviceCredentials))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
// Get the current user's information.
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);
// Create the van resource.
Equipment van = new Equipment
{
Name = "Van 1",
TimeZoneCode = 1,
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId)
};
Guid _vanId = _serviceProxy.Create(van);
// Create the contraints for creating the plumber resource group
System.Text.StringBuilder builder = new System.Text.StringBuilder("<Constraints>");
builder.Append("<Constraint>");
builder.Append("<Expression>");
builder.Append("<Body>resource[\"Id\"] == ");
builder.Append(userResponse.UserId.ToString("B"));
builder.Append(" || resource[\"Id\"] == ");
builder.Append(_vanId.ToString("B"));
builder.Append("</Body>");
builder.Append("<Parameters>");
builder.Append("<Parameter name=\"resource\" />");
builder.Append("</Parameters>");
builder.Append("</Expression>");
builder.Append("</Constraint>");
builder.Append("</Constraints>");
// Define an anonymous type to define the possible constraint based group type code values.
var ConstraintBasedGroupTypeCode = new
{
Static = 0,
Dynamic = 1,
Implicit = 2
};
// Create the plumber resource group.
ConstraintBasedGroup group = new ConstraintBasedGroup
{
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId),
Name = "Plumber with Van 1",
Constraints = builder.ToString(),
GroupTypeCode = new OptionSetValue(ConstraintBasedGroupTypeCode.Static),
};
Guid _groupId = _serviceProxy.Create(group);
// Create the resource specification.
ResourceSpec spec = new ResourceSpec
{
BusinessUnitId = new EntityReference(BusinessUnit.EntityLogicalName, userResponse.BusinessUnitId),
ObjectiveExpression = @"
<Expression>
<Body>udf ""Random""(factory,resource,appointment,request,leftoffset,rightoffset)</Body>
<Parameters>
<Parameter name=""factory"" />
<Parameter name=""resource"" />
<Parameter name=""appointment"" />
<Parameter name=""request"" />
<Parameter name=""leftoffset"" />
<Parameter name=""rightoffset"" />
</Parameters>
<Properties EvaluationInterval=""P0D"" evaluationcost=""0"" />
</Expression>",
RequiredCount = 1,
Name = "Test Spec",
GroupObjectId = _groupId
};
Guid _specId = _serviceProxy.Create(spec);
// Create the plumber required resource object.
RequiredResource plumberReq = new RequiredResource
{
ResourceId = userResponse.UserId, // assume current user is the plumber
ResourceSpecId = _specId
};
// Create the service for the equipment.
Service plumberService = new Service
{
Name = "Plumber 1",
Duration = 60,
InitialStatusCode = new OptionSetValue(1),
Granularity = "FREQ=MINUTELY;INTERVAL=15;",
ResourceSpecId = new EntityReference(ResourceSpec.EntityLogicalName, _specId)
};
Guid _plumberServiceId = _serviceProxy.Create(plumberService);
// Create the van required resource object.
RequiredResource vanReq = new RequiredResource
{
ResourceId = _vanId,
ResourceSpecId = _specId
};
// Create the appointment request.
AppointmentRequest appointmentReq = new AppointmentRequest
{
RequiredResources = new RequiredResource[] { vanReq },
Direction = SearchDirection.Backward,
Duration = 60,
NumberOfResults = 10,
ServiceId = _plumberServiceId,
// The search window describes the time when the resouce can be scheduled.
// It must be set.
SearchWindowStart = DateTime.Now.ToUniversalTime(),
SearchWindowEnd = DateTime.Now.AddDays(7).ToUniversalTime(),
UserTimeZoneCode = 1
};
// Verify that the resources have been scheduled
SearchRequest search = new SearchRequest
{
AppointmentRequest = appointmentReq
};
SearchResponse searched = (SearchResponse)_serviceProxy.Execute(search);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.