Email Templates and MailTo Syntax

The mailto prefix in hyperlinks.  Everyone knows about it, everyone uses it.  Not very many people are aware however that it can do more than just pass an email address to your user’s favorite email program.

I was in a situation the other day where I needed to prompt a user for specific information, but I did not want / need to go to the effort of writing a contact form.  Yes, I realize writing a contact form would have only taken a few minutes, but sometimes being a little bit lazy can work out well.

So, just to remind everybody and give us a starting place, let’s create a basic mailto link

<a href="mailto:youremail@domain.com">Contact Us</a>

First, let’s look at adding in more than just s single address.  There are two options here.  We can simply add multiple addresses divided by commas, or we can specify CC and BCC.

<a href="mailto:youremail@domain.com, [youremail2@domain.com">Contact](mailto:youremail2@domain.com”>Contact) Us</a>

<a href="mailto:youremail@domain.com?cc=yourccemail@domain.com">Contact Us</a>

<a href="mailto:youremail@domain.com?bcc=yourbccemail@domain.com">Contact Us</a>

Now, let’s take a look at passing through a subject.  It is essentially the same principle as the CC and BCC options.

<a href="mailto:youremail@domain.com?subject=Please write me back">Contact Us</a>

Finally, the body of the email.  This is where my unwillingness to write a contact form comes into play.

<a href="mailto:youremail@domain.com?body=Sample Body Content">Contact Us</a>

I can hear you now, that’s great, but what if I want more complicated substance to my email body?  What if I need multiple paragraphs.  Well, you can do that, but it is not completely supported in every email client yet.  All of the more recent iterations of the email clients should have no issues however.  The trick to passing paragraph information is obviously URL encoding a line break.  You can accomplish this by placing %0A where you would like a line break.  Extrapolating that, %0A%0A would give us a paragraph break.  Let’s look at an example.

<a href="mailto:youremail@domain.com?body=Sample Paragraph One%0A%0ASample Paragraph Two">Contact Us</a>

Ok.  We’ve looked at all the options, so, now we can chain all of these together into our Contact Form link.  To chain multiple items together, utilize the & character just like you would in a URL.

<a href="mailto:youremail@domain.com?cc=yourccemail@domain.com&subject=Please write me back&body==Sample Paragraph One%0A%0ASample Paragraph Two">Contact Us</a>

There you have it.

Remote Reboot of Windows 7

I ran into an interesting problem recently where I was (admittedly) too lazy to walk upstairs and reboot my desktop that I use as a home server.

I connected via remote desktop only to discover that I was not able to reboot the machine from the start menu via a RDC.

Curiosity, persistence (and laziness) prevailed and I spent some time researching to figure out how to get around this pesky issue.

Being a lover of most things related to the command prompt, I fired one up in admin mode (since like a good user, my account is not an administrator by default) and tried to reboot using

shutdown /r /t 0 only to be thwarted with aAccess is Denied(5). message.

Researching this told me what was already plainly obvious, it was a permissions issue and I did not have permissions to do what I wanted.

Digging a little further, I came across the solution.

  1. Open the policy editor (secpol.msc)
  2. Expand Local Policies
  3. Select User Rights Assignment
  4. Find the Force shutdown from a remote system policy and add your account.
  5. Once this is done, go to a command prompt and run gpupdate

Now, when you run shutdown /r /t 0, your remote computer will promptly heed your wishes and restart.

Sorting a .Net list with lambdas

I love linq.  I would wager that most developers who have done much with it share a similar love.

It makes our lives easier.  It eliminates writing copies amounts of loops, parsing xml, interacting with databases, you name it.  It’s great.

When I run into a situation where linq does not have an obvious tie in, I start to get a little anxious,  (pathetic I realize).

Today I was faced with a situation where I had a list of custom classes that I needed to sort.

This being a list, OrderBy was nowhere to be found.

So, I returned to my roots and started writing a compare for my class so that I could use the Sort command that *is *part of the List generic type.  That is when it struck me that I could probably accomplish this using a lambda.  It turns out that I was correct.  Lambda’s were a perfect fit for this scenario.  The code remained clean and concise and I was saved having to write additional methods just to accomplish a one-off scenario on what was a otherwise complete project.

Lamdas are a progression in C# past anonymous methods.  In the code below, I show how the code would be implemented with separate function, an anonymous method and finally, with a Lambda expression.

First, the test class that we’ll be working with:

public class MyClass { 
     public int propA { get; set; } 
     public int propB { get; set; } 
     public int propC { get; set; } 
}

Our test class:

public class Tester { 
     public void Main() { 
          List list = new List(); 
          list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); 
          list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); 
          list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); 
          list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); 
     }
}

Now then, we write a custom comparer.  Not a lot of code, but you can imagine how this extrapolates over various properties for large classes.

public class Tester { 
    private static int CompareByPropA(MyClass a, MyClass b) { 
        return a.propA.CompareTo(b.propA); 
    } 
    public void Main() { 
        List list = new List(); 
        list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); 
        list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); 
        list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); 
        list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); 
        //Using the comparer built into the MyClass method
        list.Sort(CompareByPropA); 
    } 
}

This time, we drop the custom comparer and implement the sort using an anonymous method.

public class Tester { 
    public void Main() { 
        List list = new List(); 
        list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); 
        list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); 
        list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); 
        list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); 
        //Using an anonymous method 
        list.Sort(delegate(MyClass a, MyClass b) { 
             return a.propA.CompareTo(b.propA); }); 
    } 
}

Finally, we rewrite the anonymous method into a Lambda.  It is still easy to read and much more concise.

public class Tester { 
    public void Main() { 
        List list = new List(); 
        list.Add(new MyClass() { propA = 1, propB = 2, propC = 3 }); 
        list.Add(new MyClass() { propA = 2, propB = 3, propC = 4 }); 
        list.Add(new MyClass() { propA = 3, propB = 4, propC = 5 }); 
        list.Add(new MyClass() { propA = 4, propB = 5, propC = 6 }); 
        //Using a Lambda 
        list.Sort((a, b) => a.propA.CompareTo(b.propA)); 
    } 
}

So, next time you go to write custom compare code, step back and see if you can do it a little more simply.  If you are going to be using your comparison code in more than one place, then perhaps it is still a better idea to implement it using that route vs the lambda.  If it is a one-off thing, then a lambda and some linq can be a beautiful thing!

Review of Flex 4 in Action

As someone who has relatively little experience with Flex, I found this book to be a great asset in furthering my understanding of how and why things work the way they do not only in MXML, but also on the ActionScript side.

The tone of the book is very friendly.  As a general rule, I did not find myself bogged down in technical terminology.  A good balance was struck between keeping things simple for the average reader, yet still technical enough to hold the attention of somebody with more in depth knowledge of previous versions of Flex.

The book also starts out with a nice section on the benefits of Flex and RIA’s in general along with a few pointers on how to sell upper management on its use.  From there they move on to cover the basics of ActionScript and Flex and show how MXML layout works, how MXML and AS work with one another, some basics on how to work with data inside of your application and also detail the differences between the old Halo controls and the new Spark controls.

The second section of the book delves into more complex topics like the event model, view states, writing custom components and more.  There is a small jump in the assumed capabilities of the reading audience at this point.  Readers who are new to Flex and who have worked through the first part of the book should still be able to follow along at this point.

The only negative that struck me as I was reading the book was the code samples.  While they do an excellent job of demonstrating the topic that they are associated with, I would have preferred if by the time I reached the end of the book I had a fully working reference application vs a series of smaller individual apps.

Overall, I enjoyed reading the book and learned quite a lot, not only about Flex 4, but Flex as a whole.  The authors did a good job of keeping the subject matter entertaining and making their various contributions flow seamlessly together.

I would recommend this book both to people who are staring out in Flex development as well as people who are already familiar with Flex and looking to find out what is new in Flex 4.

You can pre-order your copy on Amazon

OData = Hotness

Microsoft has officially announced OData.  If you are not aware of what this is, then in a sentence: OData is a queryable REST based interface that exposes your data via AtomPub.

To publish a feed, you have to use .Net.  However, they have provided client sdk’s for a variety of languages to allow for simple querying of the exposed services and they are working on several more.

I strongly encourage you to check it out.

Partly to give an idea of what is possible, and partly for my own reference, I am going to repost a “cheat sheet” that I found online at Meta-Me

The Service:

It all starts with a Data Service hosted somewhere:

http://server/service.svc
Basic queries:

You access the Data Service entities through resource sets, like this:

http://server/service.svc/People

You request a specific entity using its key like this:

http://server/service.svc/People(16)

Or by using a reference relationship to something else you know:

http://server/service.svc/People(16)/Mother

This asks for person 16’s mother.

Once you have identified an entity you can refer to it’s properties directly:

http://server/service.svc/People(16)/Mother/Firstname
$value:

But the last query wraps the property value in XML, if you want just the raw property value you append $value to the url like this:

http://server/service.svc/People(16)/Mother/Firstname/$value
```
###### $filter:

You can filter resource sets using `$filter`:
```
http://server/service.svc/People?$filter=Firstname  eq ‘Fred’
```
Notice that strings in the filter are single quoted.

Numbers need no quotes though:
```
http://server/service.svc/Posts?$filter=AuthorId eq 1
```
To filter by date you have identity the date in the filter, like this:
```
http://server/service.svc/Posts?$filter=CreatedDate eq DateTime’2009-10-31′
```
You can filter via reference relationships:
```
http://server/service.svc/People?$filter=Mother/Firstname eq ‘Wendy’
```
The basic operators you can use in a filter are:

<table class="table table-striped"><tbody><thead><tr><th>Operator</th><th>Description</th><th>C# equivalent</th></tr></thead></tbody><tbody><tr><td>eq</td><td>**eq**uals</td><td>==</td></tr><tr><td>ne</td><td>**n**ot **e**qual</td><td>!=</td></tr><tr><td>gt</td><td>**g**reater **t**han</td><td>></td></tr><tr><td>ge</td><td>**g**reater than or **e**qual</td><td>>=</td></tr><tr><td>lt</td><td>**l**ess **t**han</td><td><</td></tr><tr><td>le</td><td>**l**ess than or **e**qual</td><td><=</td></tr><tr><td>and</td><td>and</td><td>&&</td></tr><tr><td>or

</td><td>or</td><td>||</td></tr><tr><td>()

</td><td>grouping</td><td>()</td></tr></tbody></table>There are also a series of functions that you can use in your filters if needed.

###### $expand:

If you want to include related items in the results you use $expand like this:
```
http://server/service.svc/Blogs?$expand=Posts
```
This returns the matching Blogs and each Blog’s posts.

###### $select:

Some Data Services allow you to limit the results to just the properties you require – aka projection – for example if you just want the Id and Title of matching Posts you would need something like this:
```
http://server/service.svc/Posts?$select=Id,Title
```
You can even project properties of related objects too, like this:
```
http://server/service.svc/Posts?$expand=Blog&$select=Id,Title,Blog/Name
```
This projects just the Id, Title and the Name of the Blog for each Post.

###### $count:

If you just want to know how many records would be returned, without retrieving them you need `$count`:
```
http://server/service.svc/Blogs/$count
```
Notice that `$count` becomes one of the segments of the URL – it is not part of the query string – so if you want to combine it with another operation like `$filter` you have to specify `$count` first, like this:
```
http://server/service.svc/Posts/$count?$filter=AuthorId eq 6
```
This query returns the number of posts authored by person 6.

###### $orderby:

If you need your results ordered you can use `$orderby`:
```
http://server/service.svc/Blogs?$orderby=Name
```
Which returns the results in ascending order, to do descending order you need:
```
http://server/service.svc/Blogs?$orderby=Name%20desc
```
To filter by first by one property and then by another you need:
```
http://server/service.svc/People?$orderby=Surname,Firstname
```
Which you can combine with **desc** if necessary.

###### $top:

If you want just the first 10 items you use `$top` like this:
```
http://server/service.svc/People?$top=10
```
###### $skip:

If you are only interested in certain page of date, you need `$top` and `$skip` together:
```
http://server/service.svc/People?$top=10&$skip=20
```
This tells the Data Service to skip the first 20 matches and return the next 10. Useful if you need to display the 3rd page of results when there are 10 items per page.

**Note:** It is often a good idea to combine `$top` & `$skip` with `$orderby` too, to guarantee the order results are retrieved from the underlying data source is consistent.

###### $inlinecount & $skiptoken:

Using `$top` and `$skip` allows the client to control paging.

But the server also needs a way to control paging – to minimize workload need to service both naive and malicious clients – the OData protocol supports this via [Server Driven Paging](http://blogs.msdn.com/astoriateam/archive/2009/03/19/ado-net-data-services-v1-5-ctp1-server-driven-paging.aspx).

With Server Driven Paging turned on the client might ask for every record, but they will only be given one page of results.

This as you can imagine can make life a little tricky for client application developers.

If the client needs to know how many results there really are, they can append the `$inlinecount` option to the query, like this:
```
http://server/service.svc/People?$inlinecount=allpages

The results will include a total count ‘inline’, and a url generated by the server to get the next page of results.
This generated url includes a $skiptoken, that is the equivalent of a cursor or bookmark, that instructs the server where to resume:

http://server/service.svc/People?$skiptoken=4

Sometime you just need to get the urls for entities related to a particular entity, which is where $links comes in:

http://server/service.svc/Blogs(1)/$links/Posts

This tells the Data Service to return links – aka urls – for all the Posts related to Blog 1.

$metadata

If you need to know what model an OData compliant Data Service exposes, you can do this by going to the root of the service and appending $metadata like this:

http://server/service.svc/$metadata

This should return an EDMX file containing the conceptual model (aka EDM) exposed by the Data Service.