Monday, December 19, 2011

Dynamics CRM 2011 : C# Code for Retrieving Organization Deployment Information

In Dynamics CRM 2011 we can use Deployment Service Retrieve Request to retrieve the Organization deployment information i.e. SSRS Server URL, SQL Server Name, Organization Name, Organization State etc... You can use the code below to retrieve the organization deployment information in C#.

 [C# Code]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Deployment;
using System.Net;

namespace wod.xRM.Deployment
    class OrganizationDeployment
        private void main()
            //Instantiating deployment service client object, make sure to replace Server Name and Port Number
            DeploymentServiceClient wod_DeloymentService = Microsoft.Xrm.Sdk.Deployment.Proxy.ProxyClientHelper.CreateClient(new Uri("http://ServerName:PortNumber/XRMDeployment/2011/Deployment.svc"));

            //Setting deployment service client credentials, make sure to replace user name, password & domain
            wod_DeloymentService.ClientCredentials.Windows.ClientCredential = new NetworkCredential("UserName", "Password", "Domain");

            //Instantiating request for retrieving organization deployment information
            RetrieveRequest wod_OrgRetRequest = new RetrieveRequest();

            //Filtering request to retrieve only organization deployment information
            wod_OrgRetRequest.EntityType = DeploymentEntityType.Organization;

            wod_OrgRetRequest.InstanceTag = new EntityInstanceId();

            //wod_OrgRetRequest.InstanceTag.Name contains organization name of organization
            wod_OrgRetRequest.InstanceTag.Name = "OrgName";

            //Passing request object to service execute method
            RetrieveResponse wod_Response = (RetrieveResponse)wod_DeloymentService.Execute(wod_OrgRetRequest);

            //Getting SSRS URL address
            string wod_SSRSUrl = ((Organization)wod_Response.Entity).SrsUrl;

            //Getting SQL Server name
            string wod_SqlServerName = ((Organization)wod_Response.Entity).SqlServerName;

            //Getting Database Name
            string wod_DatabaseName = ((Organization)wod_Response.Entity).DatabaseName;

            //Getting Organization state
            OrganizationState wod_OrganizationState = ((Organization)wod_Response.Entity).State;



Sunday, December 18, 2011

Dynamics CRM 2011 : C# and JScript Code for Executing CRM Workflow

You can use the code below to execute workflows in C# plugin and JScript.
[C# Code]

private void ExecuteWorkflow(Guid prmEntityId, Guid prmWorkflowId)
    ExecuteWorkflowRequest wod_ExecuteWorkflowRequest = new ExecuteWorkflowRequest();

    //wod_EntityId contains the Id (GUID value) of record for which the workflow is required to execute
    wod_ExecuteWorkflowRequest.EntityId = new Guid("{7EC1E3A2-6B8C-E011-A88A-E5EF0498D042}");

    //wod_WorkflowId contains the Id (GUID value) of the workflow record (Should have On Demand option checked)
    wod_ExecuteWorkflowRequest.WorkflowId = new Guid("{813B029F-D702-45E1-A6C6-05901413B859}");

    ExecuteWorkflowResponse wod_ExecuteWorkflowResponse = (ExecuteWorkflowResponse)wod_CrmService.Execute(wod_ExecuteWorkflowRequest);

[JScript Code]

function RunMyWorkflow() {

    var wod_EntityId;
    var wod_WorkflowId;

    //wod_EntityId contains the Id (GUID value) of record for which the workflow is required to execute
    //; functions return the current id (GUID value) of record
    wod_EntityId =;

    //wod_WorkflowId contains the Id (GUID value) of the workflow record, make sure that On Demand workflow option is checked
    //You can retrieve the workflow Id (GUID value), open workflow record and copy the URL address.
    //(If you are using CRM application mode then press F11 to make address bar visible)
    //1. In my case URL is: http://localhost:4444/DevelopmentDeployment/sfa/workflow/edit.aspx?id=%7b813B029F-D702-45E1-A6C6-05901413B859%7d
    //2. Decode CRM URL address, open web address
    //3. Paste the URL address into the URL Decoder/Encoder text area
    //4. Click two times on Decode button to decode Url address completely.
    //Decoded URL: http://localhost:4444/DevelopmentDeployment/sfa/workflow/edit.aspx?id={813B029F-D702-45E1-A6C6-05901413B859}
    //5. Copy Id= value in my case it is {813B029F-D702-45E1-A6C6-05901413B859} and use in default price list JScript.
    wod_WorkflowId = "{813B029F-D702-45E1-A6C6-05901413B859}";

    ExecuteWorkflow(wod_EntityId, wod_WorkflowId);

function ExecuteWorkflow(prmEntityId, prmWorkflowId) {

    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"\"" +
    " xmlns:xsi=\"\" xmlns:xsd=\"\">" +
    Xrm.Page.context.getAuthenticationHeader() +
    "<soap:Body>" +
    "<Execute xmlns=\"\">" +
    "<Request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<EntityId>" + prmEntityId + "</EntityId>" +
    "<WorkflowId>" + prmWorkflowId + "</WorkflowId>" + "</Request>" +
    "</Execute>" +
    "</soap:Body>" +

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xmlHttpRequest.setRequestHeader("SOAPAction", "");

    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);


    var resultXml = xmlHttpRequest.responseXML;

    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0) {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
        alert("Error Message : " + msg);
    else {
        return resultXml;