Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Monday, January 27, 2014

Why Is CRM Displaying A Section That Has Been Defined As Hidden?

After being head down in CRM for the last 2 years, there are few things that surprise me, but the situation I’m about to describe, threw me for a loop:Huh

Our client had an entity with multiple forms.  All the forms looked virtually identical with the 40 same basic fields, split up among 5 different sections.  4 of the sections were defined as visible, but one wasn’t.  It contained the name of the entity (which was programmatically calculated from various fields, which explains why it was on the form but hidden) along with a couple other fields that were required by the JavaScript of the form, in order to process the business rules of the entity, but the actual business user wouldn’t care about.

Everything had been working fine until we upgraded the on-premise version of CRM from rollup 11 to rollup 15.  After the rollup, we found a bug where on one of the forms, for a particular subset of data, the hidden section was mysteriously being displayed.  I tried everything I could think of, defaulting the section to being hidden, setting it to being hidden as the last function call of the onLoad JavaScript, even setting all of the fields within the hidden section as being hidden.  All to no avail. 

Finally, in a desperate attempt to determine why the section was being displayed, I searched for “setVisible” in the JavaScript of the page, and placed a break point everywhere in the source code.  20 breakpoints later one of the breakpoints was triggered and I finally had a call stack to point me to the problem.  Although it was all minified JavaScript, chopped full of 3 character variables and functions containing only a ‘$’ a letter and a number, one of the function’s names was in English and caught my eye, “setFocus”.  Why was Microsoft setting the focus on a random field on my custom form?  A few debugging steps later, and I had my answer:

CRM has JavaScript that runs after the onLoad function of the form to find the first field on the form that is not read-only, and then sets the focus on that field.  As a part of the setFocus function, it ensures the field is visible by not only setting the field as visible, but setting it’s parent section as visible as well.  So what was happening on this one particular form was all of the fields on the form before the hidden section had been marked as read-only, and when Microsoft’s JavaScript searched for the first field that was not read-only, it found the name field in the hidden section, set the focus on it, which made the section visible.

The solution was relatively simple, make all the fields in the section read-only, and update any JavaScript that updated them to always force submit.  Making the fields read-only removed them from being valid focusable candidates, and adding force submit to each field ensured that any updates the JavaScript made would still get posted back and updated in CRM.  Problem Solved.

Debugging Microsoft’s minified JavaScript is never fun or easy, but sometimes it’s a necessity.  Happy debugging!

Monday, September 15, 2008

Creating a VS.Net 2008 Addin

In trying to take the RTF Converter to a new level, I decided to create it as an Addin. As this is my first time creating an add in, I decided to share what I have learned.

VS 2008 has a Project wizard that is used to create the project that works quite well when trying to debug your add in, but when trying to actually run it, doesn't work at all. Some of the Enumerations that happen when running the addin in debug mode, never happen when trying to run it for real.

Some other issues that I ran into was a complete lack of examples for creating a link on the right click menu of the code. I did find a website and was able to add mix and match it with the auto generated code from the wizard to come up with a workable solution.

The first step in creating an Add in, is to Implement the Extensibility.IDTExtensibility2 interface and IDTCommand Target. There are three main functions that you need to be concerned about

1. OnConnection()
- This function gets called either when VS starts, or when the app is loaded. This is where code that is needed to either add your add int to the tool bar, or (as in my case) add it as a CommandBarPopup( Basically a menu that has children commands that pop out to the side). You have to find the "Code Window" Command bar, then add a CommandBarPopup, then add any commands that you want to have displayed.
I refactored a new AddContextMenuChildItem method to handle adding the new commands that I wanted to implement to my context menu. On of the most confusing parts was trying to determine what I call the IconId to use. I ended up having to create a tempory project that created every single icon that was possible, to go through and select the ones that made the most sense. I know you can add your own personal icons, but I didn't have the time or the patience.
I also ran into an issu with the command already existing if I opened up another version of VS, but not being added to the PopupBar. So I had to add extra logic if it already exists, to just add it. I guess I should probably change this even further to check to see if it exists first, not the other way around.

2. QueryStatus()
- This function is used to hide or show items. I wanted to show the Convert Selected option, only if something was selected, so I had to add logic for this. If you always wanted them to be displayed, I think you could always just return true.

3. Exec()
- This is the function that gets fired when the command gets clicked. It takes some simple logic to determine what got clicked, and then I'm on my way.

The source code is listed below. Hopefully this will be helpful for others.


using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;
namespace CodeToHtml

/// <summary>The object for implementing an Add-in.</summary>
/// <seealso class='IDTExtensibility2' />
public class Connect : IDTExtensibility2, IDTCommandTarget

private const string ConvertSelection = "ConvertSelectionToHtml";
private const string OpenWindowForConvert = "OpenConversionWindow";
private bool hasBeenLoaded = false;

/// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
public Connect()



/// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
/// <param term='application'>Root object of the host application.</param>
/// <param term='connectMode'>Describes how the Add-in is being loaded.</param>
/// <param term='addInInst'>Object representing this Add-in.</param>
/// <seealso class='IDTExtensibility2' />
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)

try {
_applicationObject = (DTE2)application;
_addInInstance = (AddIn)addInInst;
//if (connectMode == ext_ConnectMode.ext_cm_UISetup) { //For some Reason, this never gets called, so I've changed it
if (connectMode != ext_ConnectMode.ext_cm_CommandLine) {
object[] contextGUIDS = new object[] { };
Commands2 commands = (Commands2)_applicationObject.Commands;

string toolsMenuName;

try {
//If you would like to move the command to a different menu, change the word "Tools" to the
// English version of the menu. This code will take the culture, append on the name of the menu
// then add the command to that menu. You can find a list of all the top-level menus in the file
// CommandBar.resx.
string resourceName;
ResourceManager resourceManager = new ResourceManager("CodeToHtml.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);

if (cultureInfo.TwoLetterISOLanguageName == "zh") {
System.Globalization.CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, "Tools");
}
else {
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
}
toolsMenuName = resourceManager.GetString(resourceName);
}
catch {
//We tried to find a localized version of the word Tools, but one was not found.
// Default to the en-US word, which may work for the current culture.
toolsMenuName = "Tools";
}

//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
Microsoft.VisualStudio.CommandBars.CommandBar codeWindowContextCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["Code Window"];

CommandBarPopup toolsPopup = (CommandBarPopup)codeWindowContextCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
toolsPopup.Caption = "Code To Html";
//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// just make sure you also update the QueryStatus/Exec method to include the new command names.
AddContextMenuChildItem(ref contextGUIDS, commands, toolsPopup, ConvertSelection, 223, "Convert Selection To HTML", "Converts the selected code to html");
AddContextMenuChildItem(ref contextGUIDS, commands, toolsPopup, OpenWindowForConvert, 178, "Open HTML Convert Window", "Opens a window to paste text in to convert");
hasBeenLoaded = true;
}

}
catch(Exception ex) {
MessageBox.Show(ex.ToString(), "Code To HTML");
}


private void AddContextMenuChildItem(ref object[] contextGUIDS, Commands2 commands, CommandBarPopup toolsPopup, string name, int iconId, string buttonText, string helpText) {
try {
//Add a command to the Commands collection:
Command command = commands.AddNamedCommand2(_addInInstance, name, buttonText, helpText, true, iconId, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//Add a control for the command to the tools menu:
if ((command != null) && (toolsPopup != null)) {
command.AddControl(toolsPopup.CommandBar, toolsPopup.CommandBar.accChildCount + 1);
}
else if(command == null){
MessageBox.Show("Command Was Null", "Code To HTML");
}
else
MessageBox.Show("Tools Popup Was Null", "Code To HTML");
}
}
catch (System.ArgumentException) {
if(!hasBeenLoaded){
// For some reason this was having issues adding the command, so if it already exists, I need to re-add it to the Command Bar.
foreach(Command command in commands){
if (command.Name == "CodeToHtml.Connect." + name) {
command.AddControl(toolsPopup.CommandBar, toolsPopup.CommandBar.accChildCount + 1);
return;
}
}

MessageBox.Show("Shows exists, when it hasn't been loaded", "Code To HTML");
}
}
}

/// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
/// <param term='disconnectMode'>Describes how the Add-in is being unloaded.</param>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)



/// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnAddInsUpdate(ref Array custom)



/// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnStartupComplete(ref Array custom)



/// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
/// <param term='custom'>Array of parameters that are host application specific.</param>
/// <seealso class='IDTExtensibility2' />
public void OnBeginShutdown(ref Array custom)



/// <summary>Implements the QueryStatus method of the IDTCommandTarget interface. This is called when the command's availability is updated</summary>
/// <param term='commandName'>The name of the command to determine state for.</param>
/// <param term='neededText'>Text that is needed for the command.</param>
/// <param term='status'>The state of the command in the user interface.</param>
/// <param term='commandText'>Text requested by the neededText parameter.</param>
/// <seealso class='Exec' />
public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)

try {
//if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone) {
if (commandName == "CodeToHtml.Connect." + OpenWindowForConvert || commandName == "CodeToHtml.Connect." + ConvertSelection && UserHasTextSelected(false)) {
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
//}
//MessageBox.Show(commandName + " not supported\n" + neededText.ToString());
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), "Code To HTML");
}


/// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
/// <param term='commandName'>The name of the command to execute.</param>
/// <param term='executeOption'>Describes how the command should be run.</param>
/// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
/// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
/// <param term='handled'>Informs the caller if the command was handled or not.</param>
/// <seealso class='Exec' />
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)

handled = false;
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)

switch (commandName.Substring(commandName.LastIndexOf('.') + 1))
{
case OpenWindowForConvert:
RTF2HTMLConverter.RTF2HTMLForm form = new RTF2HTMLConverter.RTF2HTMLForm();
form.Show();
handled = true;
break;
case ConvertSelection:
if(UserHasTextSelected(true)){
((
EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection).Copy();
string text = Clipboard.GetText(System.Windows.Forms.TextDataFormat.Rtf);
Clipboard.SetText(RTF2HTMLConverter.ConvertLogic.GetFormattedHtml(text));
}
break;
}



private bool UserHasTextSelected(bool assertError) {
string error = string.Empty;
if(_applicationObject.ActiveDocument == null){
error = "No Document Selected";
}
else if (_applicationObject.ActiveDocument.Selection == null) {
error = "Nothing Selected";
}
else {
try {
EnvDTE.TextSelection text = (EnvDTE.TextSelection)_applicationObject.ActiveDocument.Selection;
if (text.Text == string.Empty) {
error = "No Text Selected";
}
}
catch {
error = "No Text Selected";
}
}

if(error != string.Empty){
if(assertError){
MessageBox.Show(error, "CodeToHtml");
}
return false;
}
else
return true;
}
}
private DTE2 _applicationObject;
private AddIn _addInInstance;




Friday, September 5, 2008

Overcoming Displaying Code as HTML

On my previous post, I spent about an hour just getting the formatting to look right. I ended up pasting it into word, and fooling around with the formatting till I got it just right, and saving it, then e-mailing it to myself in Gmail so I could view it directly from there, and it looked sort of ok, except that it had some extra lines in it, so I had to work directly with the HTML to get it to look the way I wanted.

Because computer programmers like myself are lazy, and if we have to do something twice, that's once to much, I wrote a program to do this for me. Right now it consists of Two RichTextBoxes and a WebBrowser thrown onto a windows from. Paste RTF code (like the code from VS) into rtfTextBox, and press the convert button and presto, you're done. The Html is outputed in another RichTextBox, and copied to the Clipboard, while the Webrowser gives you a preview. No CSS are needed.

In the near future I'd love to turn this into a VS add in so I can just select some code, right click, and select Convert.

You'll fine the source code posted below, which was generated using itself of course. :)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RTF2HTMLConverter {
public partial class Form1 : Form {
const string BackwardsSlash = "&bws;";
const string OpenCurlyBracket = "&ocb;";
const string CloseCurlyBracker = "&ccb;";
const string DoubleQuote = "&quot;";
const string GreaterThan = "&gt;";
const string LessThan = "&lt;";
const string Ampersand = "&amp";
bool _inColorSpan;

public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
string[] rtf = rtfTextBox.Rtf.Replace("&", Ampersand).Replace("\\\\", BackwardsSlash).Replace("\\}", CloseCurlyBracker).Replace("\\{", OpenCurlyBracket).Replace("\"", DoubleQuote).Replace(">", GreaterThan).Replace("<", LessThan).Split('\n');
string rtfLine;
_inColorSpan = false;
Dictionary<string, string> colors = new Dictionary<string, string>();
StringBuilder output = new StringBuilder();
output.Append("<pre class=\"code\"><span style=\"font-size:85%;\">");

for (int i = 1; i < rtf.Length; i++) {
rtfLine = rtf[i];
if(i == 1){
GetColors(rtfLine, colors);
}
else
output.Append(GetOutputLine(rtfLine, colors));
}
}
output.Append("</span></pre>\n");
htmlTextBox.Text = output.ToString().Replace(BackwardsSlash, "\\").Replace(CloseCurlyBracker, "}").Replace(OpenCurlyBracket,"{");
webBrowser1.DocumentText = htmlTextBox.Text;
Clipboard.SetText(htmlTextBox.Text);
}

private string GetOutputLine(string rtfLine, Dictionary<string, string> colors) {
string outputLine = string.Empty;
string[] rtfLineSegments = rtfLine.Split('\\');
string firstWord;
string currentColor = string.Empty;
foreach (string segment in rtfLineSegments) {
if(segment == string.Empty){
continue;
}
firstWord = GetFirstWord(segment);

if (firstWord.IndexOf(' ') == 0) {//Trailing Spaces, just add it
outputLine += segment;
}
else if (colors.Keys.Contains(firstWord.Trim())) {//Change the Color
if (segment == firstWord) { // No Other Text, just change the color
currentColor = firstWord;
outputLine += GetColorSpan(colors[segment]);
}
else if(segment.Substring(firstWord.Length).Trim() != string.Empty) { // Change color and Add the rest of the segment if there is something to update
if (currentColor != string.Empty && currentColor == firstWord.Trim()) { // Color hasn't changed, skip adding new color span
outputLine += segment.Substring(firstWord.Length);
}
else {
currentColor = firstWord.Trim();
outputLine += GetColorSpan(colors[currentColor]) + segment.Substring(firstWord.Length);
}
}
else//all Blank Spaces, don't worry about the color
outputLine += segment.Substring(firstWord.Length);
}
}
else if (!segment.Contains(' ') && rtfLineSegments.Length > 2) { // Control word, skip it
continue;
}
else if (!segment.Contains(' ') && rtfLineSegments[0] == segment && rtfLineSegments.Length == 2) {// There is no Color for this segment, so use everything
outputLine += segment;
}
else
//First word is control word, skip it and add the rest
outputLine += segment.Substring(firstWord.Length);
}
}
return outputLine += "\n";
}

private static string GetFirstWord(string segment) {
string firstWord;
if (!segment.Contains(' ')) {
firstWord = segment;
}
else {
firstWord = segment.Substring(0, segment.IndexOf(' ') + 1);
}
return firstWord;
}

private static void GetColors(string rtfLine, Dictionary<string, string> colors) {
string[] colorValues = rtfLine.Substring(1, rtfLine.Length - 2).Split(';');
string rtfColor;
string htmlColor;
for (int k = 1; k < colorValues.Length; k++) {
rtfColor = colorValues[k];
if (rtfColor == "}") {
continue;
}
if (rtfColor.Contains("\\red")) {
string[] hexColors = rtfColor.Split('\\');
htmlColor = "#" + GetHexValue(hexColors[1], "red") + GetHexValue(hexColors[2], "green") + GetHexValue(hexColors[3], "blue");
htmlColor = ConvertColor(htmlColor);

htmlColor = "color: " + htmlColor;
}
else {
throw new Exception("Unrecognized Color " + rtfColor);
}

colors.Add("cf" + k.ToString(), htmlColor);
}
}

private static string ConvertColor(string htmlColor) {// Convert the Colors from a Dark Scheme, to a White Scheme
switch (htmlColor) {
case "#ff8000":
htmlColor = "blue"; // Reserved Words
break;
case "#fefffe":
htmlColor = "#010001"; // Regular Text
break;
case "#808080":
htmlColor = "green"; // Comments
break;
case "#ffffff":
htmlColor = "black"; //;
break;
case "#ffff00":
htmlColor = "#2b91af"; //class Names
break;
case "#2892b0":
htmlColor = "red"; // Integers
break;
case "#00ff00":
htmlColor = "#a31515"; // Strings
break;

}
return htmlColor;
}

private static string GetHexValue(string hexColor, string colorName){
return System.Convert.ToString(int.Parse(hexColor.Substring(colorName.Length, hexColor.Length - colorName.Length)), 16).PadLeft(2, '0');
}

private string GetColorSpan(string color){
if (_inColorSpan) {
return string.Format("</span><span style=\"{0}\">", color);
}
else {
_inColorSpan = true;
return string.Format("<span style=\"{0}\">", color);
}
}
}
}