Dynamic Link to Internet Calendars

I’m really not sure how I missed this little trick all these years. Sometimes I spend significant bits of time doing something the hard way before I just take 20 minutes out of my day to ask or research the easy way.

I’ve known about .ics internet calendars for a long time. I’ve clicked on many. I have chosen to open them and that’s been fine, but only today I learned that I can add a reference to an .ics file in Outlook and from that point forward have a dynamic link to that internet calendar instead of a one-shot static view of those events.

I used this trick to bring all of my Meetup groups into my Outlook as a calendar that I can overlay with my Exchange and Live calendars. Now I’m sittin’ pretty.

Here are the steps…

  1. Get the URL to your online .ics file on your clipboard
  2. In Outlook right click on Other Calendars and Add Calendar
  3. Choose From Internet…
  4. Now just paste in your URL and the rest should be self-explanatory

Ready, go!

Dynamic Link to Internet Calendars

I’m really not sure how I missed this little trick all these years. Sometimes I spend significant bits of time doing something the hard way before I just take 20 minutes out of my day to ask or research the easy way.

I’ve known about .ics internet calendards for a long time. I’ve clicked on many. I have chosen to open them and that’s been fine, but only today I learned that I can add a reference to an .ics file in Outlook and from that point forward have a dynamic link to that internet calendar instead of a one-shot static view of those events.

I used this trick to bring all of my Meetup groups into my Outlook as a calendar that I can overlay with my Exchange and Live calendars. Now I’m sittin’ pretty.

Here are the steps…

  1. Get the URL to your online .ics file on your clipboard
  2. In Outlook right click on Other Calendards and Add Calendar
  3. Choose From Internet…
  4. Now just paste in your URL and the rest should be self-explanitory

Ready, go!

Selecting Elements in a Windows 8 HTML App

Windows 8 Logo
In antiquity, I selected DOM objects on my HTML pages by using the document.getElementById method, and I always hated it. I hated it because it was one of my very few options for find the element I wanted and it was so narrow. So it was a great breakthrough to me to see how it worked in the jQuery library. CSS selectors are an awesome way to specify exactly which elements I want whether it’s one or many.

So when I saw my first Windows 8 HTML/JS example and it was using getElementById I panicked some. I didn’t want to go back in time! My first thought was “I don’t want to use this.” My second thought was “Actually I do, but can I use jQuery?” (the answer to which is yes). And finally, after some research, I realized that even without the aid of jQuery we’re in good shape thanks to some functions built into WinJS. There are a few though so I want to just enumerate what we’ve got.

The JavaScript Way

Of course, you can still use document.getElementById, but that always makes me feel like I’m regressing. :)

So first, we have the JavaScript functions querySelector and querySelectorAll. These are part of the W3C recommendations, and show up as part of the JavaScript language. You can read extensively about the W3C recommendation here.

querySelector is the single and querySelectorAll is the plural. If you know that you want a single element (even if there are multiple that match your query) then use the former, and if you know your query will be returning more than one then use the latter.

One thing to note about these JS functions is that they exist both on the document object as well as on the element object. So, we can query the document to find all matching results in the entire document, or we can just query a single element to find all objects under it. Keep in mind also, that you can enter multiple CSS selectors in the query that you pass in. Just separate them with commas and the query will be performed with a logical “or” operation to give you the union of all of your queries.

var myDiv = document.querySelector('#myDiv'); var allDivs = document.querySelectorAll('div');

The result of the singular querySelector function is a DOM element. The result of the plural querySelectorAll function is a staticNodeList.

The WinJS Way

Next, we have the methods that WinJS provides for selecting elements. They are in the WinJS.Utilities namespace and they are id() and query(). These WinJS functions actually just wrap the formerly mentioned querySelector and querySelectorAll functions, so keep this in mind. You might ask why we would use them if they are just wrappers for the JS functions. The answer is that their return result is a QueryCollection object that has all sorts of friendly functions hanging off making certain operations on a collection of elements quite easy.

The id and query functions work mostly as you might expect. id selects an element by its id, but you do not specify a hash symbol on the query. The query function, then, takes a query that you’re expecting to return multiple elements and the query syntax for it is the same syntax that querySelectorAll uses, so anything that works in one works in the other.

var u = WinJS.Utilities; var myDiv = u.id('myDiv'); var allDivs = u.query('div');

The result in both cases here is a QueryCollection.

The jQueryWay

Finally, if you want to bring a jQuery library into your project then the terse $('<query>') selector syntax will also do just fine.

var myDiv = $('#myDiv'); var allDivs = $('div')

The result of these jQuery function calls is (like practically all jQuery functions) another jQuery object. If you want the actual DOM element for the singular call, use the get function, and if you want an array of all of the DOM elements for the plural call, use the toArray function.

Conclusion

So you’ve seen that there are, as always, many ways to skin a cat. The deciding factors regarding which to use in my opinion are:

  • What result type works best for you? Do you want an actual DOM element (or list of DOM elements) or would a QueryCollection or jQuery object give you more functionality?
  • Is your app exclusively on the Windows 8 client platform? If so then you’re certainly going to want a dependency on the WinJS library and thus the WinJS method might be your best bet.
  • Have you already decided to take a dependency on the jQuery library? Are you already really familiar with using jQuery?

It’s good to have choices. Happy selecting.

vNext in Fremont

Is there anything better than joining a bunch of other people that like writing software to talk about writing software? Yes. There’s beer and sandwiches for one, and for two there’s conjoining on the subject of Windows 8 which is an exciting new software development opportunity.

Alex Golesh (@DevCorner) is a Microsoft MVP and did a smash bang job of presenting on even some of the less beginner and more intermediate parts of Windows 8 development (which is new to us all by the way). Alex’s experience includes a ton of XAML and C# development and he was able to bring all of that into Windows 8.

Next month at vNext (April 10). My colleague, MJ (@mjconnection), and I are going to continue the discussion with more of a HTML5/CSS3/JavaScript view of Windows 8 development. If you’re in the area, you should totally stop by.

Local Context and Web Context in Windows 8

When you’re developing a JavaScript app for Windows 8, you need to know about the local context and the web context, and this table (from here) does a great job of summing up the technical differences between the two.

As you can see, some of the things we as web developers have come to accept as restricted (I’m thinking of windows.close and cross-domain XHR requests) are allowed in the local context.

Other things like the ability to reference external script is not allowed from the local context. It’s easy, though, to just import your library of choice into your project and then reference it internally.

Take note of the difference in behavior between how WinJS behaves when it’s local versus when it’s in the web context (see same link as above).

For more information on the differences between the local and web contexts in Windows 8, see this MSDN article.

Permission to Get Excited

Anticipation. It’s good to wait. It’s good to hold on and hold out for what’s coming down the road.

I’m talking now about the Windows 8 Consumer Preview that is released tomorrow February 29, 2012. Along with Visual Studio 11, this new version of Windows is entirely exciting. If you weren’t at the //build conference and haven’t seen the keynote by Steven Sinofsky, watch it now. If you were there or you’ve already watched it online, then watch it again just like you watch your favorite movie again just before the sequel is released in the theater.

Windows 8 is a no-brainer in my book. It provides developer joy as well as user joy. It’s compatible with a plethora of computers already in the hands of computers as well as new and exciting devices on the shelf at your local tech store. It runs everything that ran before. It looks great and has a robust design language ready to support extend and launch your own personal or corporate brand. It is smaller in memory and CPU footprint and it appears to be entirely fast and fluid.

In short, it’s not a lateral step to move to a new device with Windows 8. It’s not a new device with a new OS and a new login. It’s an extension. A leap forward. It’s just plain exciting.

So let’s get excited, and let’s start building great apps for Windows 8! Let me know if you need any resources for getting started or for getting integrated into your local developer community for education and support in the process. Follow me on Twitter at @codefoster or email me at jeremy dot foster at microsoft dot com.

Refresh My Entire Viewmodel, Please

In case you didn’t know (as I didn’t until moments ago), if you throw a PropertyChanged event like this:

PropertyChanged(this, new PropertyChangedEventArgs(""));

…it means “all of my properties have changed”.

The empty string (or Nothing in VB) is responsible for this effect. I have added a Refresh method to my BaseViewModel that does this. Now a simple call to MyViewModel.Refresh() will tell my WP7 view that all of its underlying fields have been updated. This is going to come in quite handy for certain cases. I hope this helps you as much as it has me.

Inline Count in the OData Spec

When it comes time to tackle a new (to you) technology, how do you begin? Do you read others’ experiences from blogs? Wait for screencast instructions from a site like Pluralsight?

There are a lot of ways to learn, but I’m reminded of the value of just going back to the spec. Sometimes the author of the blog article you’re reading is in your same boat trying to learn, and going directly to the spec, you might get to bypass a lot of wasted trial and error cycles.

I’ve spent a lot of time in the HTML5 spec the last while, and somewhat recently also the OData spec (which you can find at odata.org). I’ve read a lot of blogs and seen a lot of videos on the subject of OData, but when I finally found my way to the actual spec, I was pleased to find a very concise and obviously thorough coverage of the topic.

One of the little things I learned which has big implications for me is the ability to specify a query option called $inlinecount. With this query option specified, an OData query will bring back a count of all entities in the queried collection (applied after any filters) even if some limiting options such as paging or $top are included.

Take, for example, the following query…

http://services.odata.org/OData/OData.svc/Products?$inlinecount=allpages&$top=10&$filter=Price gt 200

The query should find the first 10 products whose price is greater than 200. If you’re retrieving these products to be displayed in a web page, however, and you need to worry about paging and you need to tell the pager how many pages to render, you’re going to need to know the **total **number of products that with a price greater than 200 even though this query only asks for the top 10 (don’t use $top for your paging, BTW, there’s a better way). The inclusion of inlinecount, in this query dictates the inclusion of the following element in the response…

<m:count>24</m:count>

With this additional information about the submitted query’s results, your pager now knows to render 3 pages (of 10).

Notice that the Price gt 200 filter did get applied before the result for inlinecount was calculated, so we did not receive back an inline count of all product entities.

This is certainly just a shallow glance at the topic. If you want more in-depth information about OData or about the inlinecount query option, you should go straight to the spec :)

Striving

I heard a long time ago that there are three types of people: apathetic, mediocre, and excellent. I would rather say there are two types: those who are striving for excellence and those who are not striving.

Of the folks striving for excellence, there are again two types: those doing it for themselves and those doing it for others.

Those striving for excellence for themselves are motivated by something like fame or power or acceptance, but those striving for excellence for the sake of others realize that their hard work, dedication, education, and persistence can make a real difference in helpingothersto grow.

Whatever your place in life, whatever your training or your role or your level, you most certainly have opportunity to apply effort for the sake of others.

Troubleshooting Your EF Code First Mapping

Here’s the scenario. You’re a developer and a you create a great set of objects and relationships between them that perfectly expresses the structure of your prospective application. You want to just tell your code to go ahead and create a new database (or map to an existing one) that matches your model and allows you to store the data?

So you do this with EFCF and all is well. Except that occasionally you tweak your model and run into a mapping error and all is not well. EF has a hard time communicating to you what exactly is wrong and can give some pretty obscure exception messages, and you can spend a lot of time (personal experience) tracking down what you’ve done wrong.

The error I just ran into is this one: “Type Address_Country is not defined in namespace PEP.CIM.Entities.Mapping (Alias=Self).” Huh? I know that I just tried to create a Country navigation property on my Address entity so I have a general idea of where my error might be, but after a double check of my code I can’t see how it’s any different from my other relationships that are working fine. So what’s the problem?

I stumbled upon a great way to troubleshoot these kinds of issues, though… the Entity Framework Power Tools (at CTP1 as I type). I have used this tool countless times to reverse engineer a database into good-lookin’ C# code, but today I used it for something else. When the tool is installed (and you have EF referenced in your project!), you can right click on your context and get a few options under the Entity Framework menu item.

In my case, I was able to use View Entity Data Model XML and then look through the resulting XML for the Address_Country relationship. I fairly quickly discovered that I had NavigationProperty elements for my Address_Country but I didn’t have a coresponding Association (as I did for my working relationships). This lead me back to my fluent mapping statement…

HasRequired(t => t.Country).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId); HasRequired(t => t.State).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId);

where I discovered that I had made a grievous copy paste error. As you can see, I’m attempting to add mappings for Country and State, but it’s all wrong. Each is mapping AddressesByAdrCountryCdvId and AdrStateProvinceCdvId. Here’s the way it should look…

HasRequired(t => t.Country).WithMany(t => t.AddressesByAdrCountryCdvId).HasForeignKey(t => t.AdrCountryCdvId); HasRequired(t => t.State).WithMany(t => t.AddressesByStateProvinceCdvId).HasForeignKey(t => t.AdrStateProvinceCdvId);

Now that’s better! Now not only does my build succeed, but my unit tests all pass as well.

It also may help to use the same technique to View Entity Data Model DDL SQL and see what SQL would be generated to create your tables and constraints.

I hope this helps you find mapping issues in your code first projects. Happy coding.