Tuesday, April 8, 2014

SSIS Script Task using KingswaySoft for CRM 2011

Hi Guys,
kingswaysoft components allow simple operations like Retrieve, Update or Create, but when you need to do something more complex, there is no kingswaysoft components. But kingswaysoft provides two useful assemblies that you can uses them in a Script Task:
  • KingswaySoft.DynamicsCrmServices
  • KingswaySoft.IntegrationToolkit.DynamicsCrm
To do it follow this short toturial:
You need to create a Script Task in your Data Flow Process.
And to add your kingswaysoft crm connection manager to Connection Managers List:

After that you should to add kingswaysoft assemblies in reference like this :
Then, You should add kingswaysoft namespaces like this:

  1. #region Namespaces
  2. using System;
  3. using System.Data;
  4. using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
  5. using Microsoft.SqlServer.Dts.Runtime.Wrapper;
  6. using System.ServiceModel.Description;
  7. using KingswaySoft.IntegrationToolkit.DynamicsCrm;
  8. using KingswaySoft.DynamicsCrmServices.Soap2011;
  9. using KingswaySoft.DynamicsCrmServices.Soap2011.OrganizationService.Query;
  10. using KingswaySoft.DynamicsCrmServices.Soap2011.OrganizationService;
  11. #endregion
So, Now you need to create a IOrganizationService Object
  1. [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
  2. public class ScriptMain : UserComponent
  3. {
  4.  
  5.     KingswaySoft.DynamicsCrmServices.Soap2011.OrganizationService.IOrganizationService orgService;
  6.     ...

In PreExecute Methode, intialize your IOrganizationService Object using the AcquireConnection methode:

  1. public override void PreExecute()
  2. {
  3.     base.PreExecute();
  4.     var conn = (CrmConnection)Connections.CrmConnection.AcquireConnection(null);
  5.  
  6.     orgService = (KingswaySoft.DynamicsCrmServices.Soap2011.OrganizationService.IOrganizationService)conn.GetCrmService();
  7.     
  8. }
After that you can use all needed methods like RetrieveMultiple, Execute, Update etc...
Good Luck

N.JL

Thursday, April 3, 2014

How to Close Incident using SOAP on CRM 2011

Hi Guys,

The IncidentResolution entity is a particular entity that we can not modify or form neither fields.

So when you need to put an automatic value in Resolution field (subject) you cannot customize form by adding jscript code.

The only way to do it, is by hiding the Resolve Case button:on Incident form/home page Ribbon, and to create your own custom resolution window or dialog and should send a SOAP request to the CRM Server to close the incident.

There is the most complete source code that you allow to close your incident by sending : Subject, TimeSpent and Description data. This code is synchronous.

Source Code
  1. if (typeof (Sdk) == "undefined")
  2. { Sdk = { __namespace: true }; }
  3. //This will establish a more unique namespace for functions in this library. This will reduce the
  4. // potential for functions to be overwritten due to a duplicate name when the library is loaded.
  5. Sdk.Tools = {
  6.     _getServerUrl: function () {
  7.         ///<summary>
  8.         /// Returns the URL for the SOAP endpoint using the context information available in the form
  9.         /// or HTML Web resource.
  10.         ///</summary>
  11.         var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
  12.         var serverUrl = "";
  13.         if (typeof GetGlobalContext == "function") {
  14.             var context = GetGlobalContext();
  15.             serverUrl = context.getServerUrl();
  16.         }
  17.         else {
  18.             if (typeof Xrm.Page.context == "object") {
  19.                 serverUrl = Xrm.Page.context.getServerUrl();
  20.             }
  21.             else { throw new Error("Unable to access the server URL"); }
  22.         }
  23.         if (serverUrl.match(/\/$/)) {
  24.             serverUrl = serverUrl.substring(0, serverUrl.length - 1);
  25.         }
  26.         return serverUrl + OrgServicePath;
  27.     },
  28.     CloseIncidentRequest: function (incidentId, subject, resolutionTypeValue, durationValue, description) {
  29.  
  30.         var requestMain = ""
  31.         requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
  32.         requestMain += "  <s:Body>";
  33.         requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
  34.         requestMain += "      <request i:type=\"b:CloseIncidentRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
  35.         requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
  36.         requestMain += "          <a:KeyValuePairOfstringanyType>";
  37.         requestMain += "            <c:key>IncidentResolution</c:key>";
  38.         requestMain += "            <c:value i:type=\"a:Entity\">";
  39.         requestMain += "              <a:Attributes>";
  40.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  41.         requestMain += "                  <c:key>incidentid</c:key>";
  42.         requestMain += "                  <c:value i:type=\"a:EntityReference\">";
  43.         requestMain += "                    <a:Id>" + incidentId + "</a:Id>";
  44.         requestMain += "                    <a:LogicalName>incident</a:LogicalName>";
  45.         requestMain += "                    <a:Name i:nil=\"true\" />";
  46.         requestMain += "                  </c:value>";
  47.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  48.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  49.         requestMain += "                  <c:key>subject</c:key>";
  50.         requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + subject + "</c:value>";
  51.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  52.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  53.         requestMain += "                  <c:key>description</c:key>";
  54.         requestMain += "                  <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + description + "</c:value>";
  55.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  56.         requestMain += "                <a:KeyValuePairOfstringanyType>";
  57.         requestMain += "                  <c:key>timespent</c:key>";
  58.         requestMain += "                  <c:value i:type=\"d:int\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + durationValue + "</c:value>";
  59.         requestMain += "                </a:KeyValuePairOfstringanyType>";
  60.         requestMain += "              </a:Attributes>";
  61.         requestMain += "              <a:EntityState i:nil=\"true\" />";
  62.         requestMain += "              <a:FormattedValues />";
  63.         requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
  64.         requestMain += "              <a:LogicalName>incidentresolution</a:LogicalName>";
  65.         requestMain += "              <a:RelatedEntities />";
  66.         requestMain += "            </c:value>";
  67.         requestMain += "          </a:KeyValuePairOfstringanyType>";
  68.         requestMain += "          <a:KeyValuePairOfstringanyType>";
  69.         requestMain += "            <c:key>Status</c:key>";
  70.         requestMain += "            <c:value i:type=\"a:OptionSetValue\">";
  71.         requestMain += "              <a:Value>" + resolutionTypeValue + "</a:Value>";
  72.         requestMain += "            </c:value>";
  73.         requestMain += "          </a:KeyValuePairOfstringanyType>";
  74.         requestMain += "        </a:Parameters>";
  75.         requestMain += "        <a:RequestId i:nil=\"true\" />";
  76.         requestMain += "        <a:RequestName>CloseIncident</a:RequestName>";
  77.         requestMain += "      </request>";
  78.         requestMain += "    </Execute>";
  79.         requestMain += "  </s:Body>";
  80.         requestMain += "</s:Envelope>";
  81.         var req = new XMLHttpRequest();
  82.         req.open("POST", Sdk.Tools._getServerUrl(), false)
  83.         // Responses will return XML. It isn't possible to return JSON.
  84.         req.setRequestHeader("Accept", "application/xml, text/xml, */*");
  85.         req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  86.         req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
  87.         req.send(requestMain);
  88.  
  89.         //work with response here
  90.         return req.responseXML.xml;
  91.     },
  92.     __namespace: true
  93. };

Enjoy yourself,

N.JL

Open a html webresource as a window or a dialog box in CRM 2011.

Hi Guys,

If you need to open a web resource as a window or as a dialog in Dynamics CRM 2011 you can use these methods. But you need to know somethings:
- When you open a dialog you cannot use anymore F12 key to debug javascript code in your called webresource.
- An opened dialog cannot refresh or close a parent window. If you need to refresh the parent entity form you need to use openWebResourceAsWindow() insteed of openWebResourceAsDialog(). because, in the window mode you can use :

window.opener.location.reload();


Source Code
  1.  
  2. /// <summary>
  3. /// Allow to open a html webresource as a dialog box
  4. /// </summary>
  5. /// <param name="webResourceName"><b>string</b>: HTML web resource logical name like (new_webresource.html)</param>
  6. /// <param name="webResourceData"><b>string</b>: URI Encoded web resource data using encodeURIComponent() that begins with "?"</param>
  7. /// <param name="width"><b>int</b>: the width of the window without (px)</param>
  8. /// <param name="height"><b>int</b>: the height of the window without (px)</param>
  9. /// <param name="viewScrollbars"><b>bool</b>: allow to view the scrollbars (true or false)</param>
  10. /// <param name="viewToolbar"><b>bool</b>: allow to view the toolbar (true or false)</param>
  11. /// <param name="viewMenubar"><b>bool</b>: allow to view the menubar (true or false)</param>
  12. /// <param name="viewLocation"><b>bool</b>: allow to view the location (true or false)</param>
  13. function openWebResourceAsDialog(webResourceName, webResourceData, width, height, viewScrollbars, viewToolbar, viewMenubar, viewLocation) {
  14.     var $v_0 = Mscrm.CrmUri.create(String.format("$webresource:{0}", webResourceName));
  15.  
  16.     if (!isNullOrEmptyString(webResourceData)) {
  17.         $v_0 = $v_0 + webResourceData;
  18.     }
  19.     var parameters = "";
  20.     if (viewScrollbars !== undefined && viewScrollbars == true) {
  21.         parameters = "scrollbars=1,";
  22.     }
  23.     else {
  24.         parameters = "scrollbars=0,";
  25.     }
  26.     if (viewToolbar !== undefined && viewToolbar == true) {
  27.         parameters += "toolbar=1,";
  28.     }
  29.     else {
  30.         parameters += "toolbar=0,";
  31.     }
  32.     if (viewMenubar !== undefined && viewMenubar == true) {
  33.         parameters += "menubar=1,";
  34.     }
  35.     else {
  36.         parameters += "menubar=0,";
  37.     }
  38.     if (viewLocation !== undefined && viewLocation == true) {
  39.         parameters += "location=1";
  40.     }
  41.     else {
  42.         parameters += "location=0";
  43.     }
  44.  
  45.     return openStdDlg($v_0, null, width, height, true, false, parameters);
  46. }
  47.  
  48. /// <summary>
  49. /// Allow to open a html webresource as a window
  50. /// </summary>
  51. /// <param name="webResourceName"><b>string</b>: HTML web resource logical name like (new_webresource.html)</param>
  52. /// <param name="webResourceData"><b>string</b>: URI Encoded web resource data using encodeURIComponent() that begins with "?"</param>
  53. /// <param name="width"><b>int</b>: the width of the window without (px)</param>
  54. /// <param name="height"><b>int</b>: the height of the window without (px)</param>
  55. /// <param name="viewScrollbars"><b>bool</b>: allow to view the scrollbars (true or false)</param>
  56. /// <param name="viewToolbar"><b>bool</b>: allow to view the toolbar (true or false)</param>
  57. /// <param name="viewMenubar"><b>bool</b>: allow to view the menubar (true or false)</param>
  58. /// <param name="viewLocation"><b>bool</b>: allow to view the location (true or false)</param>
  59. function openWebResourceAsWindow(webResourceName, webResourceData, width, height, viewScrollbars, viewToolbar, viewMenubar, viewLocation) {
  60.     var $v_0 = Mscrm.CrmUri.create(String.format("$webresource:{0}", webResourceName));
  61.  
  62.     if (!isNullOrEmptyString(webResourceData)) {
  63.         $v_0 = $v_0 + webResourceData;
  64.     }
  65.     var parameters = "";
  66.     if (viewScrollbars !== undefined && viewScrollbars == true) {
  67.         parameters = "scrollbars=1,";
  68.     }
  69.     else {
  70.         parameters = "scrollbars=0,";
  71.     }
  72.     if (viewToolbar !== undefined && viewToolbar == true) {
  73.         parameters += "toolbar=1,";
  74.     }
  75.     else {
  76.         parameters += "toolbar=0,";
  77.     }
  78.     if (viewMenubar !== undefined && viewMenubar == true) {
  79.         parameters += "menubar=1,";
  80.     }
  81.     else {
  82.         parameters += "menubar=0,";
  83.     }
  84.     if (viewLocation !== undefined && viewLocation == true) {
  85.         parameters += "location=1";
  86.     }
  87.     else {
  88.         parameters += "location=0";
  89.     }
  90.     return openStdWin($v_0, null, width, height, parameters)
  91. }
  92.  
  93. /// <summary>
  94. /// Allows to open the custom Discount Dialog
  95. /// </summary>
  96. function openDiscountDialog() {
  97.     if (Xrm.Page.data.entity.getIsDirty()) {
  98.         alert("One or more fields are modified. Please save the form.");
  99.         return;
  100.     }
  101.     var entityId = Xrm.Page.data.entity.getId();
  102.     var entityName = Xrm.Page.data.entity.getEntityName();
  103.     var customParameters = encodeURIComponent("?EntityName=" + entityName + "&EntityId=" + entityId);
  104.     openWebResourceAsDialog("njl_discountDialog.html", customParameters, 280, 130, false, false, false, false);
  105. }
  106.  
  107. /// <summary>
  108. /// Allows to open the custom Incident Resolution Window
  109. /// </summary>
  110. function openIncidentResolutionWindow() {
  111.     if (Xrm.Page.data.entity.getIsDirty()) {
  112.         alert("One or more fields are modified. Please save the form.");
  113.         return;
  114.     }
  115.     var entityId = Xrm.Page.data.entity.getId();
  116.     var entityName = Xrm.Page.data.entity.getEntityName();
  117.     var customParameters = encodeURIComponent("?EntityName=" + entityName + "&EntityId=" + entityId);
  118.     openWebResourceAsWindow("njl_incidentResolutionWindow.html", customParameters, 400, 280, true, false, false, true);
  119. }

Enjoy yourself,
N.JL