Query MS CRM XML Entity with XQuery and LINQ

January 27th, 2012

In our company we have been working on a large MSCRM 2011 project. Last week, I was asked to create a report listing all the visible fields for each entity in the solution. During my search, I found a few different ways to do it, but the most fun way was querying the XML.
The XML is created when the solution is exported. It’s normally several levels deep and even when the structure would depend on the solution the XML listed below is an example of how it would looks like.

<Entities>
    <Entity>
      <Name LocalizedName="Account" OriginalName="Account">Account</Name>
      <ObjectTypeCode>1</ObjectTypeCode>
      <EntityInfo>
        <entity Name="Account">
          <LocalizedNames>
            <LocalizedName description="Account" languagecode="1033" />
          </LocalizedNames>
          <LocalizedCollectionNames>
            <LocalizedCollectionName description="Accounts" languagecode="1033" />
          </LocalizedCollectionNames>
          <Descriptions>
            <Description description="Business." languagecode="1033" />
          </Descriptions>
   <attributes>
		<attribute PhysicalName="Category">
			…
	<attribute>
   </attributes>
</entity>
      </EntityInfo>
   <FormXml>
        <forms type="main">
          <systemform>
            <formid>{b053a39a}</formid>
            <form>
              <hiddencontrols>
                <data id="addressid" datafieldname="addressid" classid="{}" />
                <data id="address2” datafieldname="address2" classid="{}" />
              </hiddencontrols>
		  <tabs>
                <tab name="general" verticallayout="true" id=”" IsUserDefined="0">
                  <labels>
                    <label description="General" languagecode="1033" />
                  </labels>
                  <columns>
                    <column width="100%">
<sections>
  <section name="account information" showlabel="false" showbar="false" id="{}" IsUserDefined="0">
                          <labels>
                            <label description="Information" languagecode="1033" />
                          </labels>
                          <rows>
                            <row>
                              <cell id="{}">
                                <labels>
                                  <label description="Name" languagecode="1033" />
                                </labels>
                                <control id="name" classid="{}" datafieldname="name" />
                              </cell>
                              <cell id="{}">
                                <labels>
                                  <label description="Phone" languagecode="1033" />
                                </labels>
                                <control id="telephone1" classid="{}" datafieldname="telephone1" />
                              </cell>
                            </row>
                            <row>
                              <cell id="{}" showlabel="false" >
                                <labels>
                                  <label description="Contact" languagecode="1033" />
                                </labels>
                                <control id="primarycontactid" classid="{}" datafieldname="primarycontactid">
                                  <parameters>
                                    <FilterRelationshipName>customer_accounts</FilterRelationshipName>
                                    <DependentAttributeName>parentcustomerid</DependentAttributeName>
                                    <DependentAttributeType>account</DependentAttributeType>
                                    <AllowFilterOff>true</AllowFilterOff>
                                  </parameters>
                                </control>
                              </cell>
                              <cell id="{}" showlabel="true" >
                                <labels>
                                  <label description="Other Phone" languagecode="1033" />
                                </labels>
                                <control id="telepho" classid="{}" datafieldname="telep" />
                              </cell>
                            </row>
                            <row>
                              <cell id="{}">
                                <labels>
                                  <label description="Parent" languagecode="1033" />
                                </labels>
                                <events>
                                  <event name="setparams" application="true" active="true">
                                    <InternalHandlers>
                                      <Handler handlerUniqueId=" " enabled="true" />
                                    </InternalHandlers>
                                  </event>
                                </events>
                                <control id="parentaccountid" classid="{}" datafieldname="parentaccountid" />
                              </cell>
                              <cell id="{ }">
                                <labels>
                                  <label description="Fax" languagecode="1033" />
                                </labels>
                                <control id="fax" classid="{}" datafieldname="fax" />
                              </cell>
                            </row>
                            <row>
                              <cell id="{ }">
                                <labels>
                                  <label description="E-mail" languagecode="1033" />
                                </labels>
                                <control id="emailadd" classid="{}" datafieldname="emailadd" />
                              </cell>
                              <cell id="{ }">
                                <labels>
                                  <label description="Web Site" languagecode="1033" />
                                </labels>
                                <control id="websiteurl" classid="{}" datafieldname="websiteurl" />
                              </cell>
                            </row>
                          </rows>
                        </section>
             </sections>
                    </column>
                  </columns>
                </tab>
</tabs>
</form>
     </systemform>
        </forms>
      </FormXml>
    </Entity>
</Entities>

'

Take into consideration that we have several entities and those entities have multiple tabs, sections, controls and labels. The listing above is just an example of what MS CRM exports in XML format. It shouldn’t be consider as a real entity. Having said that, lets continue with the real fun.

My task was to find all the visible and enabled fields in the solution. I also needed to know, if the fields belong to the Account entity or any other entity and I just needed to retrieve the fields from the Main form not the Mobile form.

Before we go further, is important to say that I had to retrieve the Label description, but I had to check the cell element, “Showlabel” attribute which controls the visibility as you can see on the next example.

<cell id="{}" showlabel="true" >
  <labels>
    <label description="Other Phone" languagecode="1033" />
  </labels>
  <control id="telepho" classid="{}" datafieldname="telep" />
 </cell>

'

Like I said before, there are different ways to do this. Here are a couple examples.
The first I used was XQuery and FLWOR expressions.

Basically it searches through the XML verifies if the “showlabel” attribute is either not there or set to true and retrieves the names.


SELECT ent.vals.value('@OriginalName', 'varchar(max)') AS Entity, ent.vals.value('@datafieldname', 'varchar(max)') FieldName
FROM @xml.nodes('for $en in //Entity
return ($en/Name,(for $q in ($en/FormXml/forms/systemform/form/tabs/tab/columns/column/sections/section/rows/row/cell)
where (empty($q/@showlabel) or ($q/@showlabel=&amp;quot;true&amp;quot;))
return $q/control)) ') ent(vals)

'

It returns the Entity name in one column follow by the Fields. It looks like this:

Another way to query is using Linq to XML. The statement looks like this:


var xrmEntities =   from e in doc.Elements(&amp;quot;Entity&amp;quot;)
                        from c in e.Elements(&amp;quot;FormXml&amp;quot;).Elements(&amp;quot;forms&amp;quot;).Elements(&amp;quot;systemform&amp;quot;).Elements(&amp;quot;form&amp;quot;)
                                    .Elements(&amp;quot;tabs&amp;quot;).Elements(&amp;quot;tab&amp;quot;)
                                    .Elements(&amp;quot;columns&amp;quot;).Elements(&amp;quot;column&amp;quot;)
                                    .Elements(&amp;quot;sections&amp;quot;).Elements(&amp;quot;section&amp;quot;)
                                   .Elements(&amp;quot;rows&amp;quot;).Elements(&amp;quot;row&amp;quot;)
                                    .Elements(&amp;quot;cell&amp;quot;)
                        where (string)c.Attribute(&amp;quot;showlabel&amp;quot;) == &amp;quot;true&amp;quot; || c.Attribute(&amp;quot;showlabel&amp;quot;) == null
                        from l in c.Elements(&amp;quot;labels&amp;quot;)
                        let formName = e.Element(&amp;quot;Name&amp;quot;).Attribute(&amp;quot;LocalizedName&amp;quot;).Value
                        let fieldName = l.Element(&amp;quot;label&amp;quot;).Attribute(&amp;quot;description&amp;quot;).Value
                        orderby formName, fieldName
                        select new {Entity = formName, Field = fieldName };
    xrmEntities.Count().Dump();
    xrmEntities.Dump();

//”doc”  variable loads the XML file.

The result returned by Linq is probably easier to understand. It looks like this.

Either way returns the Fields Names so, It’s up to you to decide what to use Linq or XQuery. Both of them are good things to learn as a developer.

CRM 2011 – Hiding a Form’s Header and Footer

January 20th, 2012

While Microsoft provides a button to minimize a form’s header, we had to fulfill a requirement to automatically minimize the header and completely hide the footer for several entities. The reasoning behind this was that most users weren’t going to be making changes to these records, so the client wanted to provide the maximum amount of viewable space. For instance, the Account form normally displays like this, but we wanted to reclaim at least that 90px of height from the ribbon:

Account Form

As it turned out, all it took was a couple simple JavaScript functions called in the form’s OnLoad event to make this happen. When looking into how to hide the ribbon, I came across this article, which lists code to hide different elements on the form and contained discussion regarding the ribbon’s minimize functionality and how to leverage it during the form’s OnLoad event. Hiding the ribbon was just a matter of firing the “onclick” event for the “minimizeribbon” button after it had rendered:

function Hide_Ribbon()
{
  setTimeout(function () { window.top.document.getElementById(&amp;quot;minimizeribbon&amp;quot;).fireEvent(&amp;quot;onclick&amp;quot;); }, 0);
}


And hiding the footer was even easier, since you could just set a “display: none;” for the footer’s parent:

function Hide_Footer()
{
  document.getElementById(&amp;quot;crmFormFooter&amp;quot;).parentElement.style.display = &amp;quot;none&amp;quot;;
}


The result looks something like this:

Account Form - No Header or Footer

The biggest downside to this approach, though, is that the header and footer will momentarily display before the JavaScript kicks in and hides them. Still, if your users don’t need to use the ribbon on a regular basis and are starved for screen space, this is a quick and easy way to give them a little more room.

CRM 2011: Dynamically Filter a Lookup Based on Multiple Joins

December 9th, 2011

There are times when you would like to filter a Lookup based on the selected value of another Lookup, but those entities do not have a direct relationship to each other. For instance, I had a situation where there was a Work Item Tracking entity that had Lookup fields for both a Project and Work Item Type entity. Based on what the user selected for the Project and the Work Item Type’s “Default” value, the Work Item Type Lookup needed to be filtered.

Work Item Type Form

However, there is no direct relationship between the two; Work Item Types are associated with Projects via a Program entity. So, I basically needed to create a view for the Work Item Type Lookup that only showed records with a Default of “Yes” and also joined to Projects through Programs. The problem I ran into, though, was that I mistakenly thought that I would need to set things up so the view would join from the Work Item Type to the Programs, then to the Projects where the Project was equal to the currently-selected Project, then back to the Programs, then finally back to the Work Item Type, where I could filter by Default.

That would’ve been fine if the result set was pulled from the Work Item Type join at the end, but it’s pulled from the original Work Item Type reference. So, a less convoluted approach was needed: I had to filter by Default, but also join from Work Item Type to Programs, then to Projects where the Project was equal to the currently-selected Project. Throwing it together in a Personal View, it looked like this:

Filtered View

From there, it was a matter of grabbing the FetchXML so I could use it in an OnLoad JavaScript function to dynamically replace the “projectid” value and name:

<?xml version="1.0"?>
-<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
	-<entity name="workitem">
		<attribute name="workitemid"/> <attribute name="name"/> <attribute name="createdon"/> <order descending="false" attribute="name"/> -<filter type="and">
			<condition attribute="default" value="1" operator="eq"/>
		</filter> -<link-entity name="program" alias="aa" to="programworkitemid" from="programid">
			-<link-entity name="project_program" to="programid" from="programid" intersect="true" visible="false">
				-<link-entity name="project" alias="ab" to="projectid" from="projectid">
					-<filter type="and">
						<condition attribute="projectid" value="{AB6FCB73-6F1C-E111-B786-005056BC4F66}" operator="eq" uitype="project" uiname="Test 4"/>
					</filter>
				</link-entity>
			</link-entity>
		</link-entity>
	</entity>
</fetch>


Regarding the specifics of actually creating the custom view, it’s basically just building the FetchXML string, getting the Lookup control, then using “addCustomView” to set it, but all of that is covered in a great article at Create a custom filtered lookup view in runtime – CRM 2011.

Disable copy and paste with jQuery

November 21st, 2011

Occasionally the need arise to prevent users from copying text from one field and pasting it in another on a Web form. Especially when dealing with confirmation fields such as passwords and email addresses where you want the user to retype the value previously entered to prevent typos.

Using jQuery it’s more intuitive and becomes a trival task with the bind event, which attaches one or more events to a given handler. You simple pass a string with the event types you’re interested in, space separated, to the bind call and include preventDefault to cancel the default behavior. For example, to prevent the cut, copy and paste operations on the Password field, you can use the following code.

$(document).ready(function() {
    $('#Password').bind('cut copy paste', function(event) {
        event.preventDefault();
    });
});

The event handler can take parameters, as in the case above, but it’s often not needed as sufficient context is usually available when the handler is bound. However, there are times when it’s necessary to gather additional information at the time the event was raised. For example, to determine the operation that the user attempted in the function about, you can use the event.type. See the event object documentation for more information.

Microsoft CRM 2011: Immediately Open a Record That Was Updated Using oData

November 4th, 2011

This is a quick modification to the Microsoft CRM 2011: Update oData Examples article we posted a while ago. In the event that you would prefer to just pop up a record that you’ve updated or created using oData instead of making your user find it on their own, you can use the AJAX “success” attribute to execute whatever code you want when the update/create is complete.

For instance, in the following “updateContact” function from the previous article, I’ve added code to build a URL, then open a window:

function updateContact(id, contactObject) {

  //Parse the entity object into JSON
  var jsonEntity = window.JSON.stringify(contactObject);

  //Asynchronous AJAX function to Update a CRM record using OData
 $.ajax({
    type: &quot;POST&quot;,
    contentType: &quot;application/json; charset=utf-8&quot;,
    datatype: &quot;json&quot;,
    data: jsonEntity,
    url: Xrm.Page.context.getServerUrl() + &quot;/XRMServices/2011/OrganizationData.svc/ContactSet(guid'&quot; + id + &quot;')&quot;,
    beforeSend: function (XMLHttpRequest) {
      //Specifying this header ensures that the results will be returned as JSON.
      XMLHttpRequest.setRequestHeader(&quot;Accept&quot;, &quot;application/json&quot;);

      //Specify the HTTP method MERGE to update just the changes you are submitting.
      XMLHttpRequest.setRequestHeader(&quot;X-HTTP-Method&quot;, &quot;MERGE&quot;);
    },
    success: function (data, textStatus, XmlHttpRequest) {
      if(Xrm.Page.getAttribute('new_opencontactonsave').getValue() == true) {
        var linkUrl = Xrm.Page.context.getServerUrl() + &quot;/main.aspx?etc=2&amp;extraqs=formid%3d894cc46a-b0cb-4ab0-8bf6-200544e46a2d&amp;id=%7b&quot; + id + &quot;%7d&amp;pagetype=entityrecord&quot;;

        var windowWidth = screen.width * .5;
        var windowLeft = windowWidth * .5;

        var windowHeight = screen.height * .75;
        var windowTop = (screen.height - windowHeight) * .5;

        var options = &quot;left=&quot; + windowLeft + &quot;,width=&quot; + windowWidth + &quot;,top=&quot; + windowTop + &quot;,height=&quot; + windowHeight;

        window.open(linkUrl,&quot;_blank&quot;,options);
      }
    }
  });
}

In this case, we’re using the GUID for the Contact we just updated when building the URL, but, if we inserted a Contact record, we could use the result data to find the GUID of the record with something like “data.d.ContactId”. The quickest way to get the values for the other querystring variables, “etc” and “extraqs”, is to open a record of whatever entity you’re interested in, click “Copy a Link”, then pull the values from there. All that’s left is to set the window options that you want and open it up!

Contact Header

The updated CRM 2011 solution file can be downloaded here!

CSS Menus and Unordered Lists

October 28th, 2011
 

Why use CSS Menus?

The short answer … because they are awesome. If you do much web programming, especially if you are more of a programmer than a designer, you have probably had the opportunity to sit in front of the computer and come up with some type of menu schema that will look nice and work properly across all browsers. This can often be a very frustrating experience, but help has arrived … I give you CSS and an unordered list. This concept is certainly not new, but it is powerful and worth reviewing.

The truth is that you can find many different controls that will produce a menu for you. Among them are:

There is even the Microsoft provided menu control, which you can find here:

Microsoft Menu Control

Now, it is not my intention to bash any of these controls. I actually think that they are all very beautiful and quite impressive. Here at TLS, I have used several Telerik controls with excellent results. Nevertheless, building a great looking menu is not anywhere near as difficult as it used to be and it can now be done using nothing but an unordered list and some CSS tags. If you want to spruce your menu up a little bit more, you can also include an image or two but that is not necessary.

Making a menu like this has several advantages.

  • Cross Browser Compatibility – Any browser that supports CSS (which by now should be all of them) will render your menu correctly. If you feel compelled to do so, you could add in some CSS for older IE versions, but I say …. If you are still using IE5 or IE6, that is your problem, not mine.
  • Code Simplicity – By this I mean that the amount of code on the page necessary to actually render the menu using an unordered list is much smaller than any other kind of menu you could use.
  • Graceful Failability – In other words, if for whatever reason, your styles are not working or javascript is turned off or the user has some ancient browser that doesn’t support modern CSS or Javascript, then the menu will still render as a normal Unordered List, which works perfectly fine as a menu, even if it is not the flashy affair that you envisioned.
  • Mobile Device Scalability – Lets face it, gone are the days in which you could plan on a user having either an IE or a Mozilla browser. Now a user could be on any number of browsers as well as an iPhone, an iPad, an Android Phone (all 2,394 models each with a different resolution), a Kindle Fire, a Windows Phone (I hope) and even an old non-smart phone with internet access. A menu done this way is far more likely to work correctly and look good on the lot of them.
 

Creating a CSS Menu

So … lets see some examples. First of all, we need to create our menu. This is very simple, just create an unordered list of some values. Mine will look like this:

&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Michael Westen&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Sam Axe&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Fiona Glenanne&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Madeline Westen&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Jesse Porter&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;

When actually rendered on a page, it will look like so (Bullets and all):

 

As I mentioned, while not pretty, this is certainly serviceable as a menu if the CSS wasn’t working for some reason. By contrast, go look at a website using a complex menu control and then disable javascript and css … the result will be ugly and generally not even recognizable as a menu. But to make ours into a pretty menu, we are first going to add some styles to our unordered list which can then be used to make the list look more like a classic webpage menu. The first thing that we have to do, is set up a style identifying this list as a menu so that it will behave differently than other unordered lists that we might have on our webpage. The first thing that we need to do is to encase the unordered list in a containing tag. I will use a “div” tag. This gives us a couple of advantages, such as making sure that it extends to 100% of the element that it is inside of and allowing us to set a background color for the menu as a whole. Then we will give the container a class name … I’ll call it CssMenu. Like so:

&amp;amp;amp;amp;lt;ul id=&amp;amp;amp;amp;quot;Menu&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Michael Westen&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Sam Axe&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Fiona Glenanne&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Madeline Westen&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
    &amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=&amp;amp;amp;amp;quot;#&amp;amp;amp;amp;quot;&amp;amp;amp;amp;gt;Jesse Porter&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
 

Now, this will not do anything to make the menu display different because we haven’t defined a CSS Style for “CssMenu” yet. So the next step is to style the unordered list so that it is starting to appear the way we want it to appear. To begin with, we will set the margins and paddings to 0 and then also set the height. Most importantly, we will set the “list-style-type” attribute to “None” so that it does not actually look like a list. The style tag would look like this:

        #Menu
        {
            margin: 0px;
            padding: 0px;
            list-style-type: none;
            height: 25px;
        }
 

Doing that gives us the following result:

If you wanted a vertical menu, then what we have right here would work pretty well, but I want our menu to be horizontal, so I will add additional style tags to make the “li” items in the list line up next to one another as opposed to on top of one another. The style for that looks like this:

        #Menu li
        {
            float: left;
        }
 

At this point, our list items are lining up one after the other like this:

 

There is nothing earth shattering here, I know, but I’m trying to go step by step. Believe it or not, we are almost there. From here, we just need to style the links on each list items so that they look pretty. Anyone with CSS experience should be able to do this with relative ease. In my case, I added things like a border, a background color, font family, font size etc. By adding padding also, you can add some space between the end of the word and the end of the link. By adding a right margin, you can separate each link into its own space. Also, by setting the display to “block” you can set the height or the width and have that value stick. Since block elements take up 100% of whatever they are inside of unless told otherwise, this means that the link will take the entire width of the list item. By adding in the following styles:

        #Menu a
        {
            border: solid 1px #000066;
            border-bottom: solid 1px #99000;
            background-color: #EDECD1;
            padding-right: 20px;
            padding-left: 20px;
            margin-right: 4px;
            display: block;
            line-height: 25px;
            font-family: Verdana, Tahoma, Arial;
            font-size: 10pt;
            color: #333333;
            text-decoration: none;
        }
 

My unordered list now looks like this:

 

To add some pizzazz, now, I am also going to add a style to the link so that it changes color when the mouse pointer hovers over it. Once again, this is very simple css code …. like this:

        #Menu a:hover
        {
            background-color: #990000;
            color: #ffffff;
            border-bottom: solid 1px #990000;
        }
 

Now when the mouse hovers over a menu item, it will change color.

 

At this point, it is quite easy to put a content div tag right below the menu and make the colors match so that the menu appears to work in conjunction with the are below it … like this:

 

Conclusion

From here, the possibilities are endless. You could do all kinds of things using a few more simple css concepts like adding images or wrapping the menu in other tags. I’m not going to go into that, but I will say that in just a few lines of code here (with no images or javascript) we have made a nice little menu. If you are interested in more ideas as to how you could use these concepts, click here … but prepare to have your mind blown when you see all that can be accomplished just using CSS.

Denali New T-SQL features.

October 17th, 2011

SQL Server Denali comes with a number of new features that will help to make our life easier or more complicated depending on your point of view. However, I think it will make our life a lot easier.

A few of the new features that I will talk about today are the Try/Catch block, execute a store procedure With Result Sets and the IIF function.

TRY/CATCH

It was originally introduced in SQL 2005. However it lacked flexibility and we were not able to re-throw the error like we do it in C# or VB.net. However, now we can using “THROW”. Here is an example of how it works:

DECLARE @REG VARCHAR(50);
DECLARE @MORE INT;

	BEGIN TRY
		SELECT @REG= RegionDescription FROM DBO.Region R
		INNER JOIN Territories T ON T.RegionID = R.RegionID	 

		SELECT @MORE= @REG

	END TRY

	BEGIN CATCH
		PRINT 'You need to cast the varchar before assign it to a int';
		THROW;

	END CATCH

This is just a simple example of how it works, however there are many different ways to format the message so you can display or return the right message for your code or UI. You can find additional information at Microsoft

WITH RESULT SETS

Another new feature that was called to my attention is the execution of stored procedures “WITH RESULT SETS”. With this new feature you won’t need the high cost efficient functions. Something else that is nice with this feature is that you can rename the columns returned by the stored procedure. Also if you were using a temp table in order to see the data you will not need it any more with the new “WITH RESULT SETS.” Here is an example:

-- Create the store procedure in the Northwind database
CREATE PROCEDURE [dbo].[spTerritories] (@Region int)
as
BEGIN
	SELECT TerritoryID, TerritoryDescription, RegionID FROM Territories
	WHERE RegionID = @Region
END
GO

EXEC spTerritories @Region = 1
WITH RESULT SETS
(
	(
		PlaceID varchar(100),
		PlaceDescription text,
		Region float
	)
)

Now, regarding cost, the new approach is a lot more efficient than the previous versions. In fact if you run something like this:

DECLARE @TestTable TABLE (TerritoryID varchar(MAX), TerritoryDescription text,
    RegionID float )

INSERT INTO @TestTable
EXEC spTerritories @Region = 1

SELECT * FROM @TestTable

It will cost you around 5 times more than just executing the store procedure With Result Sets. Take a look at the image below.

The blue square is the execution of the store procedure with the result sets that was 14% of the batch. The red squares make a total of 14 + 57 + 14 = 85% of the whole batch, so there is no doubt that using WITH RESULT SETS is more efficient that the old way.

IIF Function

Another new feature in SQL Denali are the built in functions. I will not write about all of them but you can find great examples of them at Pinal Dave’s blog. You can find a reference to his blog at the end of this post.

The one that caught my attention was the new IIF function. It basically replaces the Case statement for simple scenarios. Obviously if you have a long nested IIF function the CASE may be your best option. However the new IIF improves the code a lot.

Here are a couple of examples using tables from the Northwind database.

SELECT IIF(RegionID >= 2, 'Good Region', 'The best') as RegionScore,
		TerritoryID, TerritoryDescription, RegionID
FROM  dbo.Territories

Another example would be something like this. In this case I’m nesting the IIF function.

SELECT IIF(RegionID >=2,
		IIF(
			(SELECT e.FirstName
			FROM  Employees  e
			LEFT JOIN EmployeeTerritories et
                            on e.EmployeeID = et.EmployeeID
			WHERE et.TerritoryID =ttr.TerritoryID) is null, 'No Data',
			(
			        SELECT  e.FirstName
				FROM Employees  e
				LEFT JOIN EmployeeTerritories et
                                    on e.EmployeeID = et.EmployeeID
				WHERE et.TerritoryID =ttr.TerritoryID
		       )
			),'No Employee Assigned') as RegionScore,
		TerritoryID, TerritoryDescription, RegionID
FROM  dbo.Territories  ttr

Some of the other built-in functions are specified on Pinal’s website. You can take a look at them Clicking Here. Microsoft is another reference for any information I wrote on this post.

C# Regex Multiple Replacements

October 13th, 2011

As I was testing the XML request for web service, I would periodically get a response back: [400]: Error parsing xml. After some troubleshooting, I discovered that the error was caused by a few of the Predefined XML Entities included in the actual data. The most common issue was the ampersand “&” being embedded in the Address and Account data.

I needed a way to replace those xml entity characters, but didn’t want to replace each one separately, so I thought, great I’ll just use a Regular Expression.

The Regex.Replace method has 4 overloads, but the basic syntax in .NET is Regex.Replace(string input, string pattern, string replacement). That didn’t work for me because I needed the replacement value to vary, based on the pattern.

For example, this simple replacement…Regex.Replace(input, "&", "&")…wasn’t what I wanted as that would mean repeating it for the other replacements. Now the pattern can include multiple items, for example Regex.Replace(input, "&|\”|<|>|’", replacement), but the replacement would be the same for each one, so I needed to use the overload that accepts a MatchEvaluator delegate that is called each time a regular expression match is found during a Replace operation.

Regex.Replace(string input, string pattern, MatchEvaluator evaluator)

Given that my search and replace values were simple, I decided to use a generic Dictionary and store the entity and its replacement as name value pairs.

var xmlEntityReplacements = new Dictionary<string, string> {
    { "&", "&amp;" },
    { "'", "&apos;" },
    { "<", "&lt;" },
    { ">", "&gt;" },
    { "\"", "&quot;" }
};

Great, now I can revised the Regex like this:

Regex.Replace(input, "&|\"|<|>|'", delegate(Match m) {
    return xmlEntityReplacements[m.Value];
})

Not bad, but I am duplicating the items in the pattern (they are already in the dictionary) and why use the anonymous method approach when I’m using .NET 3.5 and have access to LINQ? So a little refactoring was in order.

Ok, but I need to get the dictionary Keys and return then as a pipe-delimited string. There is a CopyTo() method on the collection to convert the Keys or Values to an array, and from the array I can get the string I needed. That’s good, but I don’t want to have to declared and array to house the values and then use CopyTo, that’s an extra step. Hmmm, another great use for LINQ and its extension methods:

Regex.Replace(source, string.Join("|", xmlEntityReplacements.Keys
    .Select(k => k.ToString()).ToArray()), m => xmlEntityReplacements[m.Value])

Here’s the code converted to a method.

/// <summary>
/// Replaces the 5 predefined entities in XML with the appropriate
/// name/escape character.
/// </summary>
/// <param name="source">string to search</param>
/// <returns>source string with replaced value or original string</returns>
public static string ReplaceXmlEntity(string source)
{
      if(string.IsNullOrEmpty(source)) return source;

      // The XML specification defines five "predefined entities" representing
      // special characters, and requires that all XML processors honor them.
      var xmlEntityReplacements = new Dictionary<string, string> {
            { "&", "&amp;" },
            { "'", "&apos;" },
            { "<", "&lt;" },
            { ">", "&gt;" },
            { "\"", "&quot;" }
      };

      // Create an array and populate from the dictionary keys, then
      // convert the array to a pipe-delimited string to serve as the
      // regex search values and replace
      return Regex.Replace(source, string.Join("|", xmlEntityReplacements.Keys
          .Select(k => k.ToString()).ToArray()),
              m => xmlEntityReplacements[m.Value]);
}

Note, for more complex patterns/searches, the dictionary approach won’t work as the MatchEvaluator would expect to find the match as a Key in the Dictionary and it wouldn’t be there. For example, let’s say your match pattern was “T.p” (T followed by any character then a lowercase p), the replacement value was “Top” and the input string was “Just Tap and Tip.” The dictionary would have a Key of “T.p” and a Value of “Top” Now, the pattern “T.p” is going to match both “Tap” and “Tip” but those words aren’t Keys in the dictionary and you’d get the error “The given key was not present in the dictionary.”

I’ll leave that as an exercise for you to do if you so choose.

SQL 2005: Adding and modifying XML node attributes

September 30th, 2011

There’s a change we’re making to one of our projects that relies on a couple new XML attributes, however, we already have data in our development system that we want to keep. While the XML would be updated when the project saves it, nobody wants to manually open and click through the 2000+ forms we’ve generated over the past couple years, so I ended up creating a sproc that would do it for us.

My first thought was that I could simply cast the value from the XML column as a VARCHAR(MAX), fire off a couple global REPLACE commands, and be done with it, but things are never that easy. While one of the attributes we were adding could be inserted with the same value for all nodes, the other holds the ID of the tab the control the node represents is rendered on. This means I needed to figure out a way to select individual nodes and insert the attribute with the correct value, which means REPLACE was out of the question.

I settled on loading a cursor with the node ID’s and their corresponding tab ID’s, then using XQuery to modify individual nodes with the correct values. It’s not the quickest solution, but it did exactly what I needed; I could pick out a specific node, check if the attribute already existed, then add/modify as needed:


	DECLARE @xml XML
	DECLARE @Query VARCHAR(MAX)
	DECLARE @FVId INT
	DECLARE @FFId INT
	DECLARE @FVSId INT

	SELECT	 @xml = FIData
			,@FVId= FVId
	FROM FI
	WHERE FFId = @FFId

	DECLARE fvff_cursor CURSOR FOR
	SELECT fvff.FFId, fvff.FVSId
	FROM fvff
	INNER JOIN fvs ON fvs.FVSId = fvff.FVSId
	INNER JOIN fvt ON fvt.FVTId = fvs.FVTId
	WHERE fvt.FVId= @FVId
	AND fvff.Active = 1

	OPEN fvff_cursor

	FETCH NEXT FROM fvff_cursor
	INTO @FFId, @FVSId

	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF @xml.exist('/formData/FFValues/FF[@FFId=xs:string(sql:variable("@FFId")) and @FVSId]') = 0
		BEGIN
			SET @xml.modify('insert attribute FVSId{ xs:string(sql:variable("@FVSId")) } into (//formData/FFValues/FF[@FFId=xs:string(sql:variable("@FFId"))])[1]')
		END

		IF @xml.exist('/formData/FFValues/FF[@FFId=xs:string(sql:variable("@FFId")) and @IsEnabled]') = 0
		BEGIN
			SET @xml.modify('insert attribute IsEnabled{ "true" } into (//formData/FFValues/FF[@FFId=xs:string(sql:variable("@FFId"))])[1]')
		END
		ELSE
		BEGIN
			SET @xml.modify('replace value of (//formData/FFValues/FF[@FFId=xs:string(sql:variable("@FFId"))]/@IsEnabled[.="false"])[1] with "true"')
		END

		FETCH NEXT FROM fvff_cursor
		INTO @FFId, @FVSId
	END

	CLOSE fvff_cursor
	DEALLOCATE fvff_cursor

	UPDATE FI
	SET FIData = @xml
	WHERE FIId = @FIId

WCF, Entity Framework and N-Tier Solutions – Part 3

September 30th, 2011

Create the Silverlight Application

If you’ve read my two previous posts on the subject, (Part 1 and Part 2) you will know that I promised to do a part 3 to this series that will show how to wire up what we did in creating our N-Tier solution using WCF and Entity Framework to an actual application. In this case, we will create a Silverlight application that can consume our WCF services and display them on a page. This will not be as long as the other two posts, but it should give you a general idea of how this is to be done.

The first order of business is to create a new Silverlight application. In this case, we are going to add it to the solution that we created in the previous posts. It is called “BurnNotice”. Once again, if you do not know what Burn Notice is, my heard bleeds for you … check it out here … but it won’t affect your understanding of the example. To do this, right click on the solution itself and choose the option “Add > New Project” as shown in the screenshot.

 

The “Add New Project” dialog box will appear and you can select any kind of Silverlight application that you would like, but I generally like to choose the “Silverlight Navigation Application” as it comes pre-wired with some pages and navigation controls. You can make your own by adding additional controls in the same manner.

 

After selecting the type of Silverlight application that you want, you will be presented with the “New Silverlight Application” dialog. This will allow you to host the Silverlight application inside of a web application that Visual Studio will create for you. Assuming that you have not already added a web application to your solution, you can accept most of the default values. Of course you can name it whatever you want. My options were as follows:

 

After you have clicked the “OK” button, Visual Studio will add two new projects to your solution. The first of the two will be the Silverlight application from which you will connect to WCF Services created previously. The second will be the web application to host your Silverlight application so that it can be viewed in a browser. For the purposes of debugging, you will want to have the web application as the default project. If you selected the “Silverlight Navigation Project”, then you will also see a basic UI setup in the Design pane as shown in the following screenshot.

 

The web application project will have two pages designed to host your Silverlight application, one a web form and the other a standard html page like so:

 

The names of these two pages will be abnormally long as they include the name of the project along with “TestPage” added to the end. You don’t need to change them, but if you are going to be typing the address into a browser, it makes sense to change the name of the page to something more standard. I personally got rid of the html page altogether and then renamed the web form to “default.aspx” like this:

 

Add a Reference to the WCF Service

At this point we are almost home free. The last step to actually use the service that we created previously is to add a reference to the Silverlight project. In this case, instead of adding a standard reference, though, we want to add a reference to the service itself. To do this, right click on the project name and select “Add Service Reference…” as shown below:

 

This will bring up the “Add Service Reference” dialog box. In the Address box, enter the local URL of the service that you created, unless, of course, you have already deployed it somewhere in which case you can reference it there. After entering the URL, click on the “Go” button and any services discovered at the URL will be listed in the “Operations” box on the right. If none appear then either the URL you entered is incorrect or the service itself is not functioning properly as shown in the screenshot. You will also need to enter the Namespace which you want the reference to belong to. This can also be called whatever you want, but the default will have the word “service” in it so that later, while programming, you will know at a glance that any of the methods inside of it are from a service as opposed to another local project.

 

After adding the service, you will now see a new folder in your project called “Service References” and below it you will find the service that you have just created. It will be named whatever you called the namespace in the “Add Service Reference” dialog. It will look like so:

 

Call the service to populate a Silverlight control

At this point, the only task left is to call the service and use the data returned to populate a control. To do this, open one of the .xaml pages that were created when the Silverlight application was created. Since this is only an example it does not particularly matter which one. In my case, I will do it on the “Home.xaml” page. Once the page is opened in the design view, open the “Toolbox” on the left side of the page as shown in the screenshot. Under the sub heading “Common Silverlight Controls” you will see one called “DataGrid” (not to be confused with the “Grid” control which is used for layout purposes). Click on the DataGrid icon and drag it onto the designer surface.

 

This should end up giving you some code that looks basically like this:

<navigation:Page x:Class="BurnNotice.UI.Home"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    Title="Home"
    Style="{StaticResource PageStyle}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot">
        <ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
            <StackPanel x:Name="ContentStackPanel">
                <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" Text="Home"/>
                <TextBlock x:Name="ContentText" Style="{StaticResource ContentTextStyle}" Text="Home page content"/>
                <sdk:DataGrid AutoGenerateColumns=True" Name="dgSpies" />
            </StackPanel>
        </ScrollViewer>
    </Grid>

</navigation:Page>
 

Right now, I am not going to go into a tutorial on .xaml files or how to acutally create and lay out a page using silverlight, but I do want to show how easy it now is to call the actual service and populate the grid. You will notice that I am just letting Silverlight autopopulate everything for the sake of simplicity. Obviously all of that can be customized. So, in the code behind to the .xaml page, all you need to do is set up some code in the “OnNavigatedTo” event that will call the service. All calls to a service like this are done asynchronously so once the call is done, you also need to set up a delegate that will be invoked when the results from the service are returned. This is very easy to do and the events are built into the service that we have already created. The code to do this looks like so:

namespace BurnNotice.UI
{
    public partial class Home : Page
    {
        public Home()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            BurnNoticeServiceClient client = new BurnNoticeServiceClient();
            client.Characters_GetAllCompleted += new EventHandler<Characters_GetAllCompletedEventArgs>(Characters_GetAll_Completed);
            client.Characters_GetAllAsync();
        }

        private void Characters_GetAll_Completed(object sender, Characters_GetAllCompletedEventArgs e)
        {
            dgSpies.ItemsSource = e.Result;
        }
    }
}
 

Depending on the speed, you might notice a bit of a lag time between initially calling the service and having the grid populated, but after it is called and the delegate executed … Voila, you have a populated datagrid on a silverlight page that looks like this (notice that Tricia Helfer is mentioned in the grid … Ahhh Tricia).