Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Friday, May 03, 2013

Schedule a Resource

This sample code shows how to schedule a resource with the following scenario. A plumber and van need to be scheduled to investigate and fix a leak at a customer site. The earliest available appointment needs to be made. In order for this to be accomplished, all preliminary records must be created. The default 24-hour calendars will be used. No sites are created for this sample.



   1:  //# [Schedule a Resource ]

   2:  using System;

   3:  using CrmSdk;

   4:  using Microsoft.Crm.Sdk.Utility;

   5:   

   6:  namespace Microsoft.Crm.Sdk.HowTo

   7:  {

   8:     /// <summary>

   9:     /// This sample shows how to schedule a resource.

  10:     /// </summary>

  11:     public class ServiceManagement

  12:     {

  13:        public static bool Run(string crmServerUrl, string orgName)

  14:        {

  15:           // Set up the CRM Service.

  16:           CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

  17:   

  18:           #region Setup Data Required for this Sample

  19:           bool success = false;

  20:   

  21:           #endregion

  22:   

  23:           try

  24:           {

  25:              // Get the current user's information.

  26:              WhoAmIRequest userRequest = new WhoAmIRequest();

  27:              WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);

  28:   

  29:              // Create the van resource.

  30:              equipment van = new equipment();

  31:              van.name = "Van 1";

  32:              van.timezonecode = new CrmNumber();

  33:              van.timezonecode.Value = 1;

  34:              van.businessunitid = new Lookup();

  35:              van.businessunitid.type = EntityName.businessunit.ToString();

  36:              van.businessunitid.Value = user.BusinessUnitId;

  37:   

  38:              // Create the van object.

  39:              Guid vanId = service.Create(van);

  40:   

  41:              // Create the plumber resource group.

  42:              constraintbasedgroup group = new constraintbasedgroup();

  43:              group.businessunitid = new Lookup();

  44:              group.businessunitid.type = EntityName.businessunit.ToString();

  45:              group.businessunitid.Value = user.BusinessUnitId;

  46:              group.name = "Plumber with Van 1";

  47:              System.Text.StringBuilder builder = new System.Text.StringBuilder("<Constraints>");

  48:              builder.Append("<Constraint>");

  49:              builder.Append("<Expression>");

  50:              builder.Append("<Body>resource[\"Id\"] == ");

  51:              builder.Append(user.UserId.ToString("B"));

  52:              builder.Append(" || resource[\"Id\"] == ");

  53:              builder.Append(vanId.ToString("B"));

  54:              builder.Append("</Body>");

  55:              builder.Append("<Parameters>");

  56:              builder.Append("<Parameter name=\"resource\" />");

  57:              builder.Append("</Parameters>");

  58:              builder.Append("</Expression>");

  59:              builder.Append("</Constraint>");

  60:              builder.Append("</Constraints>");

  61:              group.constraints = builder.ToString();

  62:              group.grouptypecode = new Picklist();

  63:              group.grouptypecode.Value = 0;

  64:   

  65:              Guid groupId = service.Create(group);

  66:   

  67:              // Create the resource specification.

  68:              resourcespec spec = new resourcespec();

  69:              spec.businessunitid = new Lookup();

  70:              spec.businessunitid.type = EntityName.businessunit.ToString();

  71:              spec.businessunitid.Value = user.BusinessUnitId;

  72:              spec.objectiveexpression = @"

  73:                 <Expression>

  74:                    <Body>udf ""Random""(factory,resource,appointment,request,leftoffset,rightoffset)</Body>

  75:                    <Parameters>

  76:                       <Parameter name=""factory"" />

  77:                       <Parameter name=""resource"" />

  78:                       <Parameter name=""appointment"" />

  79:                       <Parameter name=""request"" />

  80:                       <Parameter name=""leftoffset"" />

  81:                       <Parameter name=""rightoffset"" />

  82:                    </Parameters>

  83:                    <Properties EvaluationInterval=""P0D"" evaluationcost=""0"" />

  84:                 </Expression>";

  85:              spec.requiredcount = new CrmNumber();

  86:              spec.requiredcount.Value = 1;

  87:              spec.name = "Test Spec";

  88:              spec.groupobjectid = new UniqueIdentifier();

  89:              spec.groupobjectid.Value = groupId;

  90:   

  91:              Guid specId = service.Create(spec);

  92:   

  93:              // Create the plumber required resource object.

  94:              RequiredResource plumberReq = new RequiredResource();

  95:              plumberReq.ResourceId = user.UserId;// assume current user is the plumber

  96:              plumberReq.ResourceSpecId = specId;

  97:   

  98:              // Create the van required resource object.

  99:              RequiredResource vanReq = new RequiredResource();

 100:              vanReq.ResourceId = vanId;

 101:              vanReq.ResourceSpecId = specId;

 102:   

 103:              // Create the service.

 104:              service plumberService = new service();

 105:              plumberService.name = "Plumber1";

 106:              plumberService.duration = new CrmNumber();

 107:              plumberService.duration.Value = 60;

 108:              plumberService.initialstatuscode = new Status();

 109:              plumberService.initialstatuscode.Value = 1;

 110:              plumberService.granularity = "FREQ=MINUTELY;INTERVAL=15;";

 111:              plumberService.resourcespecid = new Lookup();

 112:              plumberService.resourcespecid.type = EntityName.resourcespec.ToString();

 113:              plumberService.resourcespecid.Value = specId;

 114:              plumberService.strategyid = new Lookup();

 115:              // This is a known GUID for the default strategy.

 116:              plumberService.strategyid.Value = new Guid("07F7DC72-1671-452D-812C-7172D3CA881F");

 117:              

 118:              Guid plumberServiceId = service.Create(plumberService);

 119:   

 120:              // Create the appointment request.

 121:              AppointmentRequest appointmentReq = new AppointmentRequest();

 122:              appointmentReq.RequiredResources = new RequiredResource[] {vanReq};

 123:              appointmentReq.Direction = SearchDirection.Forward;

 124:              appointmentReq.Duration = 60;

 125:              appointmentReq.NumberOfResults = 10;

 126:              appointmentReq.NumberOfResults = 1;

 127:              appointmentReq.ServiceId = plumberServiceId;

 128:   

 129:              // The search window describes the time when the resouce can be scheduled.

 130:              // It must be set.

 131:              appointmentReq.SearchWindowStart = new CrmDateTime();

 132:              appointmentReq.SearchWindowStart.Value = DateTime.Now.ToUniversalTime().ToString();

 133:              appointmentReq.SearchWindowEnd = new CrmDateTime();

 134:              appointmentReq.SearchWindowEnd.Value = DateTime.Now.AddDays(7).ToUniversalTime().ToString();

 135:              appointmentReq.UserTimeZoneCode = 1;

 136:   

 137:              // Create the request object.

 138:              SearchRequest search = new SearchRequest();

 139:   

 140:              // Set the properties of the request object.

 141:              search.AppointmentRequest = appointmentReq;

 142:   

 143:              // Execute the request.

 144:              SearchResponse searched = (SearchResponse)service.Execute(search);

 145:   

 146:              #region check success

 147:   

 148:              if (searched.SearchResults.Proposals.Length > 0)

 149:              {

 150:                 success = true;

 151:              }

 152:   

 153:              #endregion

 154:   

 155:              #region Remove Data Required for this Sample

 156:   

 157:              service.Delete(EntityName.service.ToString(), plumberServiceId);

 158:              service.Delete(EntityName.equipment.ToString(), vanId);

 159:                 

 160:              #endregion

 161:           }

 162:           catch (System.Web.Services.Protocols.SoapException ex)

 163:           {

 164:              // Add your error handling code here.

 165:              Console.WriteLine(ex.Detail.InnerText);

 166:              success = false;

 167:           }

 168:   

 169:           return success;

 170:        }

 171:     }

 172:  }

No comments: