Tag Archives: Windows 8

Getting started with the CRM 2011 Windows Store app sample

UPDATE (18APR2013): Updated AuthenticateAsync() extension method to handle both IFD and CRM Online authentication.  Now, the code in HelloCRM will work regardless of whether you point it to an IFD or CRM Online Organization Service.  All you have to do is pass in the appropriate username/password combo to AuthenticateAsync().  Quick link to updated code: http://sdrv.ms/Qy0Wcv.

UPDATE (12MAR2013): Found another issue with my helper extension methods around when I was detaching the event handler.  Uploaded a zip file with the improved helper methods.

UPDATE: There was a bug in my extension methods.  I wasn’t detaching the event handlers after the event had been handled.  This caused issues with subsequent calls to the Async extensions methods.  I’ve fixed the code to detach the event handlers appropriately.

I’ve blogged about a hackish, but workable approach to building an Windows Store app here and here.  However, as I mentioned in the post, if you like it use it.  If not wait until there’s an official SDK sample.  Well, in early November, the CRM time published the official SDK sample:

http://blogs.msdn.com/b/crm/archive/2012/11/02/building-clients-for-windows-phone-and-windows-8-rt.aspx

The feedback I’ve gotten from a few folks who’ve looked at the official sample is:

  • “It’s not exactly the easiest sample to follow.  Can you give me a hello world example?”
  • “I can’t use crmsvcutil.exe generated code with the sample.”
  • “I have to use Event-based Asynchronous Pattern (EAP)?  It’s Windows 8!  Everyone expects async / await capable APIs.”

The goal of this post is to address these common responses with:

  • A simplified hello world example to get you started
  • How to use crmsvcutil.exe generated entities so you have early bound / strongly typed / data binding friendly classes
  • Starter extension method examples which wrap the EAP nature of the sample API and give you async / await friendly methods

 

 

You can grab the hello world example from the video here:

http://sdrv.ms/Qy0Wcv

In the video, I make reference to Erik Pool’s wonderful tool to control what gets generated from crmsvcutil.exe:

http://erikpool.blogspot.com/2011/03/filtering-generated-entities-with.html

The inspiration for the Extension methods comes from this Stack Overflow post:

http://stackoverflow.com/questions/12853445/convert-event-based-pattern-to-async-ctp-pattern

I blogged about using LINQPad to create FetchXml here:

http://blogs.msdn.com/b/devkeydet/archive/2012/04/22/getting-fetchxml-from-linqpad.aspx

@devkeydet

UPDATED SAMPLE: CRM 2011 Windows Store App OData helper

I worked with a partner, EastBanc Technologies, who used the approach I blogged about here and here to build a Windows Store App for a Dynamics CRM solution.  The folks at EastBanc Technologies found some bugs.  We worked together to fix the bugs and they were gracious enough to let me share their changes.  I’ve uploaded the updated sample here:

Visual Studio 2012 Source Code: http://sdrv.ms/S3MND1

CRM Unmanaged Solution: http://sdrv.ms/RYzVzj

You will need the WCF Data Services Tools for Windows Store Apps and Visual Studio 2012 RTM.

If you’ve followed the two previous posts, you’ll notice that I uploaded my samples to new location on my SkyDrive.  The reason is that due to the final terminology for Windows Store Apps, I needed to refactor the solution/project files and class names.  While I plan on updating the walkthrough post when I have a spare moment, I wanted to get the changes published.  Hopefully, the naming changes are obvious enough for folks until I get that post updated.

@devkeydet

UPDATED SAMPLE: CRM 2011 Metro style OData helper

I’ve updated the sample I started here by implementing Create Update and Delete methods.  The library is still not complete or well tested, but it is at least a head start to building a CRUD capable Windows 8 Metro style application using the CRM 2011 OData (aka Organization Data) service.  Both the helper web resource and the Windows 8 Metro style source code have been updated.  The Visual Studio solution now has a little sample Metro style app to quickly test out the functionality of the library.

http://sdrv.ms/NIiMY2

To get the sample to work, you will need to import the updated CRM solution package which updates the helper web resource. Also, make sure you change the CRM organization url in the sample app.  You can still follow the instructions from the first post since there are no breaking changes in the sample, just new functionality.

@devkeydet

Using the CRM 2011 OData service from a Metro style app

UPDATE: Fixed a few bugs in the downloadable sample including one where it wouldn’t work in Office 365 based CRM Online subscriptions.  Edited some text in the post for clarity.

UPDATE2: Added some text about how to get this working for Windows Authentication.

UPDATE3: I’ve just blogged about an update to the sample here.  The updates in the code might make following this post a little confusing.  The reason is that due to the final terminology for Windows Store Apps, I needed to refactor the solution/project files and class names.  While I plan on updating this post soon, I wanted to get the changes published.  Hopefully, the naming changes are obvious enough for folks until I update this post. 

Scenario:

“I want to build a Windows 8 Metro style application.  Since my app is going to just perform CRUD operations, I want to use the OData service using json so that I have the least amount of ‘data across the wire’ possible.”

DISCLAIMER: This a pretty hacky approach, but it works and is the only way I know of to do it.  If you like it, use it.  If not, wait until there’s an official sample in the SDK.  You’ve been warned.  I’m just a guy getting creative to solve a problemSmile.

First I will walk you through how to do it.  Then I will explain how it works.  To get started, download and import the following solution into you CRM 2011 organization:

http://sdrv.ms/S3MND1

Next, download the source code of my sample library:

http://sdrv.ms/RYzVzj

I say sample library very loosely.  This is FAR from being a complete, well tested library.  I have good intentions to make it one, but wanted to get a sample of the basic plumbing working for those of you who have asked me how to do it.  This sample should be enough to get you started, but you’ll have to finish out the rest of the functionality.  If anyone wants to take this sample and turn it into an open source project, YOU HAVE MY FULL PERMISSIONSmile.

USING THE SAMPLE

Create a new Metro style app.  I am going to use the Blank App template to keep things simple:

image

Add my sample library to the solution:

image

Don’t forget to right-click References nod of the Crm2011MetroStyleODataApp in Solution Explorer and a reference to the project you just added:

image

image

Find the Devkeydet.CrmMetroODataHelper.csproj file and add it.  Replace the default Grid in MainPage.xaml with the following:

<Grid x:Name="LayoutRoot" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel x:Name="MainUIStackPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">

        <TextBlock x:Name="myTextBlock" Text="TextBlock" TextAlignment="Center" />

        <Button x:Name="queryButton" Content="Button" />

    </StackPanel>

</Grid>

In MainPage.xaml.cs, paste the following code into the OnNavigatedTo method:

var oDataHelperWebResource = "https://devkeydet.crm.dynamics.com/WebResources/dkdt_/MetroODataHelper.htm";

 

if (!CrmMetroODataHelper.IsLoggedIn)

{

    CrmMetroODataHelper.SignIn(oDataHelperWebResource, LayoutRoot);

    CrmMetroODataHelper.SignInComplete = () => MainUIStackPanel.Visibility = Visibility.Visible;

}

Make sure to replace “https://devkeydet.crm.dynamics.com” with the right url for you.  You will have to add a using Devkeydet statement to the file.  Ok, now it’s time to “Add Service Reference” to the OData service.  Unless you are using Windows Authentication, you can’t point the “Add Service Reference” dialog to the data service url.  Instead, you need to download it and point to it locally, from Visual Studio:

image

image

Save it somewhere and point, then “Add Service Reference” by pointing Visual Studio to the file:

image

Add the following method to MainPage.xaml.cs:

private  async void ExecuteODataQueryAsync()

{

    var ctx =

        new devkeydetContext(new Uri("https://devkeydet.crm.dynamics.com/XRMServices/2011/OrganizationData.svc/"));

 

    // Use the generated DataServiceContext to compose the query using LINQ, 

    // but get the string of the query via ToString().

    // We still get to use the query composition capabilities of LINQ!

    var query = from a in ctx.AccountSet

                where a.Name.StartsWith("A")

                select new Account

                {

                    Name = a.Name

                };

 

    var accounts = await CrmMetroODataHelper.RetrieveMultipleRecordsAsync<Account>(query.ToString());

 

    myTextBlock.Text = accounts.First().Name;

}

Again, remember to replace “https://devkeydet.crm.dynamics.com” with the right url for you.  You will have to add a using statement based on the Namespace you gave the generated code and devkeydetContext will need to be replaced with your generated context name.  Now, wire up an event handler for the button (ex: double click the button on the design surface) and call the method:

ExecuteODataQueryAsync();

If your app is using Windows Authentication, then you need to double-click on the Package.appxmanifest and make sure you have the following Capabilities checked:

image

Go ahead and run the app.  You will be prompted for your credentials if necessary:

image

Sign in with your credentials, click the button, and the TextBlock should have the value of the Name property of the first result:

image

There you go!  You just issued an OData query from a C# Metro style app!

HOW THE SAMPLE WORKS

The sample wraps the WebView control.  The WebView control tries to load an html web resource from the CRM server (that’s what’s in the devkeydetMetroODataHelper_1_0.zip solution).  If you haven’t logged in, then the WebView is set to be visible and it will just present you with the web based login UI.  Once logged in, the WebView visibility is collapsed and the C# wrapper basically proxies OData calls through WebView via the web resource using a modified version of the SDK.REST.js file from the CRM 2011 SDK.  The major modification is that the default SDK.REST.js sample will turn the resulting json string into an JavaScript object.  Instead, I just pass the json string back to the C# wrapper and use JSON.NET to turn it into C# objects.

As I mentioned earlier, this is FAR from being a complete and reusable library.  I have good intentions to make it one, but wanted to get a sample of the basic plumbing working for those of you who have asked me how to do it.  This sample should be enough to get you started, but you’ll have to finish out the rest of the functionality.  I only implemented executing a query that returns multiple results.  Furthermore, I didn’t handle continuation tokens.  If anyone wants to take this sample and turn it into an open source project, YOU HAVE MY FULL PERMISSIONSmile.

@devkeydet

Getting a more Metro-like desktop

So I made the leap.  I am using the Windows 8 Consumer Preview as my primary laptop.  Given that I use a number of Hyper-V VMs, I am really diggin the Client Hyper-V feature of Windows 8.  No more reboot, boot to my Win2k8R2 base OS, spin up my VHD.  Now I can spin it up right from my primary client OS on my laptop.  But I digress…

I use the following settings for my Start screen color scheme:

image

…which gives me the following start screen look:

image

By default, when I switched to the Desktop, I had the Aero theme configured:

image

For me, the default Aero theme with with the the blue color hints and the rounded buttons with gradients is a bit too much of a contrast.  Have no fear, Windows 8 has all sorts of knobs to suit your preferences.  I reconfigured my desktop settings to look like this:

image

Notice the flat buttons and color scheme that matches my Start screen personalization a bit better.  The transition to the desktop just feels smoother for me this way.  Plus I kind like the “retro metro” look it gives the desktop experience.  A couple people who’ve seen this have asked me “Is that a newer build of Windows 8?”  Nope, I just changed the defaults.  If you like it, here’s how to do it yourself:

Right-click on the Desktop and select Personalize from the context menu:

image

    Select the Windows Basic theme:

    image

      Select Windows Color:

      image

        Select the color that closely matches your start screen and move the color intensity to match your preference:

        image

          Save changes.

          There you go.  You now have a desktop experience that, in my opinion, blends a little better with the start screen and overall Metro look and feel.  As they say, Your Mileage May Vary (YMMV).

          @devkeydet