Wednesday, 22 August 2018


Error connecting to Online Dynamics 365 {Unable to Login to Dynamics CRM OrganizationWebProxyClient is null}


While connecting to online dynamics CRM 365 (Version 1710 (9.0.2.1465)) through  "Microsoft.Xrm.Tooling.Connector"
I got the above error. I  was trying to connect using visual studio 2017.

Below was my code  behind.

             
   var connectionString = ConfigurationManager.ConnectionStrings["Xrm"].ConnectionString;
 CrmServiceClient _crmSvc = new CrmServiceClient(connectionString);
 IOrganizationService crmservice = _crmSvc.OrganizationServiceProxy;

Following is app.conconfig setting

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
  </startup>

  <connectionStrings>
    <add name="Xrm" connectionString="Url=https://buildme.crm8.dynamics.com;
AuthType=Office365;
Username=my@buildme.onmicrosoft.com; Password=pass@word1"/>
  </connectionStrings>


</configuration>
I used following approaches to resolve the issue.But nothing worked and error 
continued to occur.
1. I used latest SDK dll
2. I used (ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;)
 before instantiating CrmServiceClient .
3.I used unique organization name in connection string.
4.I moved to .Net framework Version=v4.6.2.
Error Resolution:
I tried connecting CRM with Xrmtoolbox and i saw below error in log file.
"Error : The security timestamp is stale because its expiration time 
('2018-08-22T15:59:40.256Z') is in the past.
 Current time is '2018-08-24T18:52:45.924Z' and allowed clock skew is '00:05:00'"
Solution:
I have updated my system time which was ahead with the CRM online user Timezone.
This fixed the crm connection issue and i was able to connect.
Hope it helps.


Monday, 31 October 2016

Multiselect Picklist control for MS CRM 2016/2015/2013

This is the feature which is missing in CRM. Althogh  CRM has  this  inbuilt feature and they use this in Advanced find.
Now they are planning to introduce this option in crm 365 .
I am working on a HTML 5 control which imitates CRM multi-select control.

What it does?

It takes a option set field and renders it in the control.User can select the values from the available values.Selected values are shown in custom field.Just save the CRM form. Thats All  :)

Demo is shown below.


Wednesday, 17 February 2016

Get All fields available on MS CRM form using c#

If you want to get all the fields available on a MS CRM form.
You can use below C# code to get  all attributes.

Below code requires entity logical name and formid.

To get the formid open the record in MS CRM form editor and press  F12 (developer tools) use Chrome for better experience, Search formId in Elements tab.

You will get following results.

<input name="formId" type="hidden" value="{b053a39a-041a-4356-acef-ddf00182762b}">

OR

Mscrm.FormEditorVariables.currentFormId = '\x7bb053a39a-041a-4356-acef-ddf00182762b\x7d'

Take this FormId as you wish :) and proceed for the our custom application.

           // Obtain an organization service proxy.
           .
            using (var _orgService = new OrganizationService(connection))
            {

                string formId = "{b053a39a-041a-4356-acef-ddf00182762b}";

                XmlDocument form = RetrieveEntityForms("account", formId, _orgService);

                XmlNodeList nodeList = (form.SelectNodes("//control"));

                StringBuilder sbAttributes = new StringBuilder();;
                                 

                foreach (XmlNode elem in nodeList)
                {
                    if (elem.Attributes["datafieldname"] != null)
                    {
                        string strValue = elem.Attributes["datafieldname"].Value;

                        sbAttributes.AppendLine(strValue);
                    }


                }                

                Console.WriteLine(sbAttributes.ToString());
                
                Console.Write("DONE!!");
                Console.ReadKey();
                

            }

          
 public static XmlDocument RetrieveEntityForms(string logicalName,string formid, IOrganizationService oService)
        {
            QueryByAttribute qba = new QueryByAttribute("systemform");
            qba.Attributes.AddRange("objecttypecode", "type","formid");
            qba.Values.AddRange(logicalName, 2, formid);
            qba.ColumnSet = new ColumnSet(true);

            EntityCollection ec = oService.RetrieveMultiple(qba);

            StringBuilder allFormsXml = new StringBuilder();
            allFormsXml.Append("");

            foreach (Entity form in ec.Entities)
            {
                allFormsXml.Append(form["formxml"]);
            }

            allFormsXml.Append("");

            XmlDocument docAllForms = new XmlDocument();
            docAllForms.LoadXml(allFormsXml.ToString());

            return docAllForms;
        }

Friday, 15 January 2016

MS CRM 2016 Web API - Retrieve

This example returns data for an account entity with the primary key value equal to 9C33D98E-2FAD-E511-80DE-6C3BE5A8380C. This query also expands data from the related opportunities of Account and related tasks using the respective single-valued and collection-valued navigation properties: opportunity_parent_account and Account_Tasks. To identify the appropriate properties and navigation property names to use for the account entity, see the account EntityType

function RetrieveAccountData()
{
var organizationUrl = Xrm.Page.context.getClientUrl();

var query = "accounts(9C33D98E-2FAD-E511-80DE-6C3BE5A8380C)?$select=accountcategorycode,accountnumber,creditonhold,createdon,numberofemployees,name,revenue&$expand=opportunity_parent_account($select=createdon,name),Account_Tasks($select=subject,scheduledstart)";
var req = new XMLHttpRequest();
req.open("GET",organizationUrl + "/api/data/v8.0/" + query, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
    if (this.readyState == 4) {
        req.onreadystatechange = null;
        if (this.status == 200) {
  
    var RetrievedAccount = JSON.parse(this.response);
    console.log(RetrievedAccount);
          alert(RetrievedAccount.name);
        } else {
            var error = JSON.parse(this.response).error;
            alert(error.message);
        }
    }
};
req.send();
}

Monday, 4 January 2016

MS CRM 2016 Web API Operations - Delete a single property value

Delete a single property value:

To delete the value of a single property use a DELETE request with the property name appended to the Uri of the entity.

The following example deletes the value of the fax property of an account entity.
function UpdatAccountSingleValueUsingDELETE() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

//Single Property value  which you want to delete
var Property ="fax";

UpdatAccountSingleValue(AccountId,clientURL,Property);


}
 

function UpdatAccountSingleValue(AccountId,clientURL,Property)
{
 
var req = new XMLHttpRequest();
req.open('DELETE', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")/"+Property, true);
req.setRequestHeader("Content-type","application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
  req.onreadystatechange = null;
  if (this.status == 204) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};

req.send();
 

}

MS CRM 2016 Web API Operations - Update a single property value


Update a single property value :

When you want to update only a single property value use a PUT request with the property name appended to the Uri of the entity.

Whenever you want to update single attribute of an entity it can be done by using PUT verb.

Note : while creating object use "value"  property to accommodate the Updated value of attribute.


function UpdatSinglePropertyAccountByPUT() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

//Attribute which you want to update e.g Name
var Property ="name";

var AccountTobeUpdated ={};
AccountTobeUpdated["value"] ="My first Update On Account by API using PUT";

UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)


}

 

function UpdatAccountByPUT(AccountId,clientURL,AccountTobeUpdated,Property)
{
 
var req = new XMLHttpRequest();
req.open('PUT', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")/"+Property, true);
req.setRequestHeader("Content-type","application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
  req.onreadystatechange = null;
  if (this.status == 204) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};
req.send(JSON.stringify(AccountTobeUpdated));
 

}


Thursday, 31 December 2015

MS CRM 2016 Web API Operations - Basic Update



Basic Update:

In the last post I have created an Account using Web API call. Here we will update the created Account. Update operations use the HTTP PATCH verb. Pass a JSON object containing the properties you want to update to the URI that represents the entity. A response with a status of 204 will be returned if the update is successful.

Note: 

When updating an entity, only include the properties you are changing in the request body. Simply updating the properties of an entity that you previously retrieved, and including that JSON in your request, will update each property even though the value is the same. This can cause properties to appear to have been updated in auditing data when in fact they haven’t actually changed.


function UpdatAccount(AccountId,clientURL,AccountTobeUpdated)
{
 
var req = new XMLHttpRequest();
req.open('PATCH', clientURL + "/api/data/v8.0/accounts(" + AccountId + ")", true);
req.setRequestHeader("Content-type","application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
  req.onreadystatechange = null;
  if (this.status == 204) {   
   console.log("Updated account with ID: "+ AccountId)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};
req.send(JSON.stringify(AccountTobeUpdated));
 

}

//This method will update an existing record.
function UpdateAccountUsingWebAPI() {

debugger;
var clientURL = Xrm.Page.context.getClientUrl();
var AccountId =  "B3A2B300-A8AF-E511-80DD-6C3BE5A878BC";

var AccountTobeUpdated ={};
AccountTobeUpdated["name"] ="My first Update On Account by API";
AccountTobeUpdated["accountnumber"] = "841843";
AccountTobeUpdated["fax"] ="9738310781";


UpdatAccount(AccountId,clientURL,AccountTobeUpdated)


}