Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

August 11, 2018

Display a Two Options field as checkbox inside a Quick View Form

The other day I was working on a project involving a Quick View Form, nothing complicated but in the end it required some additional steps to achieve the desired result.
The purpose of this Quick View Form (for the Account entity) was to show a single Two Options field, so the users don't need to open the related account record to see the value of this boolean field.
As usual I created the Quick View Form, added the Two Options Field, save & publish, added the Quick View Form inside the other entity, save & publish. All was working except the Two Options Field was displaying as a label showing Yes or No:



As I need to show this field as a checkbox, first thing I thought was: "no big deal, I back to the form editor and I change the formatting to show a checkbox", the only problem is that the "Formatting" tab inside the field properties is missing when you work with a Quick View Form:



So it is not possible to set the Control Formatting style to display the field as a checkbox, as you can do inside a standard or a Quick Create Form:



A workaround is to manually edit the solution XML and change the classid attribute of the control to a specific value.

Beware: you should do this kind of editing only if you know what you are doing, I don't encourage to go and edit files without having a proper knowledge of the XML structure.

First step I did is to create a new solution containing just the Quick View Form I needed to edit, this is possible with the "Select Entity Assets" wizard of the latest versions of Dynamics, if you are on an older version you need to add the entire entity (with all the forms, views and fields) to the new solution.



After I exported the solution as Unmanaged, I extracted the zip file to a folder and I edited the file customizations.xml. In order to find the right place to edit I searched for the logical name of the field (in my case marketingonly) and I was in front of the definition:
<cell id="{32660749-eb65-6c8d-208c-664929db91db}" showlabel="true" locklevel="0">
  <labels>
    <label description="Marketing Only" languagecode="1033" />
  </labels>
  <control id="marketingonly" classid="{67FAC785-CD58-4F9F-ABB3-4B7DDC6ED5ED}" datafieldname="marketingonly" disabled="false" uniqueid="{b751c953-ac42-7723-a3c5-d4c7b0514c0b}" />
</cell>
As I mentioned before the attribute to update is classid, so I changed the guid value to {B0C6723A-8503-4FD7-BB28-C8A06AC933C2}, if someone is curious where this specific value comes from, it is listed inside the FormXml definition as the value for CheckBoxControl, or you can export another form with a checkbox and you will find the same guid value.

Done with the edit I saved the file, zipped the solution files, imported and published, and finally the Quick View Form now shows a checkbox:



Hope it helps!

April 2, 2015

Simplified Connection with complicated passwords

CRM 2011 introduced a very easy way to connect to Dynamics CRM instances: the Simplified Connection (MSDN: https://msdn.microsoft.com/en-us/library/gg695810.aspx).
Basically it's necessary to build a connection string instead of dealing with the specific deployment type (OnPremise, IFD or Online).

The downside of using a Simplified Connection is its weakness management of passwords containing special characters like double quotes, single quotes, ampersands.

Considering the MSDN example for a CRM Online connection:
Url=http://222.178.203.72:19005/whst/63/=bnmsnrnzbqlzcxmZlhbrzbnl:/ Username=jsmith@contoso.com; Password=passcode;
If the password is ;abc123 (note the semicolon) an exception will be thrown with the following message:
Format of the initialization string does not conform to specification starting at index 102. The solution for this problem is to include the password inside single quotes, the following connection string will work:
Url=http://222.178.203.72:19005/whst/63/=bnmsnrnzbqlzcxmZlhbrzbnl:/ Username=jsmith@contoso.com; Password=';abc123';
Assuming the connection string is builded dynamically the following code can be used:
string connectionString =
    String.Format("Url={0}; Username={1}; Password='{2}';", url, username, password);
What if our complicated password contains single quotes as well? Let's consider for example the following password: ;a''bc'123
In this case the previous exception (Format of the initialization string) will be thrown again. This issue can be solved "escaping" the single quotes using a Replace:
string connectionString =
    String.Format("Url={0}; Username={1}; Password='{2}';",
    url, username, password.Replace("'","''"));
Please note that the escape must be done also if your connection string is stored inside your app/web.config:
<add key="CRM"
value="Url=http://222.178.203.72:19005/whst/63/=bnmsnrnzbqlzcxmZlhbrzbnl:/ Username=jsmith@contoso.com; Password=';a''bc'123';"/>
But in this case our replace method will not work, because it will replace also the single quotes delimiting the password. In this scenario I suggest to put inside the app/web.config a placeholder instead of the delimiting single quotes that will be replaced after (for example #XYZ#):
<add key="CRM"
value="Url=http://222.178.203.72:19005/whst/63/=bnmsnrnzbqlzcxmZlhbrzbnl:/ Username=jsmith@contoso.com; Password=#XYZ#;a''bc'123#XYZ#;"/>
Then after the connection string is loaded we do the escape and the replace:
string connectionString = ConfigurationManager.ConnectionStrings["CRM"].ConnectionString;
// escape the single quotes inside the password
connectionString = connectionString.Replace("'","''");
// replace the placeholder with single quotes
connectionString = connectionString.Replace("#XYZ#","'");
Of course this will not work if the password contains the placeholder as well, so it's better to choose a long placeholder.

When the password is stored inside the app/web.config it's necessary to deal with another problem, the case that our password contains XML special characters (mostly double quotes) because this file is an XML.

If it's necessary to encode the password the following .NET method can be used:
string xmlPassword = System.Security.SecurityElement.Escape(password);
The result for the password ;a''b"c'123 (note the double quote between b and c that will create problems if not encoded) will be ;a&apos;&apos;bc&apos;123, a valid string to be written inside the app/web.config.