Convergence 2013 Recordings

As Girish says in his post:

“If you didn’t attend Convergence 2013, you can still watch the recording of the keynotes and breakout sessions by registering for the Virtual Convergence. Best part is that it is completely free!”

More details here:

http://blogs.msdn.com/b/girishr/archive/2013/04/17/watch-convergence-2013-session-recordings-for-free.aspx

@devkeydet

CRM and SQL 2012 AlwaysOn Availability Groups

I’ve spent a fair amount of time lately talking to people about the benefits of using SQL Server 2012 AlwaysOn Availability Groups in your CRM infrastructure.  I just came across a great video that’s a great primer on AlwaysOn Availability Groups:

https://channel9.msdn.com/posts/SQL-2012-AlwaysOn-Availability-Groups/player?w=512&h=288

Here is a link to the SQL documentation as well:

http://msdn.microsoft.com/en-us/library/hh510230

AlwaysOn is already used in CRM Online for both local failover/redundancy and geographically distributed failover/redundancy for disaster recovery.  As of Update Rollup 12 for Microsoft Dynamics CRM 2011, the implementation guide has been updated with AlwayOn guidance:

http://msdn.microsoft.com/en-us/library/jj822357.aspx

One of the nice features of AlwaysOn is Active Secondaries: Readable Secondary Replicas (AlwaysOn Availability Groups).   You can configure custom reports to use the readable replica.  Here’s a good blog post on the topic:

http://blogs.msdn.com/b/thecrmguys/archive/2012/08/10/crm-reporting-and-sql-2012-alwayson-availability-groups-better-together.aspx

Make sure you read the limitations of this approach stated in the blog post.  You should evaluate the feasibility of using this approach to determine whether the benefits are applicable to you. 

@devkeydet

TOOL HIGHLIGHT: CRM 2011 Web Resource Linker/Publisher

I’ve blogged previously about why I like the approach CRM Solution Manager uses for authoring web resources in Visual Studio.  While I think CRM Solution Manager is a fantastic tool, there are ways to get some of the benefits of it’s approach to web resource authoring with a couple other free Visual Studio addins.  The first tool is CRM 2011 Web Resource Linker/Publisher.  This tool provides the ability to map a file in a Visual Studio project to a web resource in CRM.  Because of this, I can organize my scripts in a folder structure which aligns with the relative path naming convention that’s recommended in the CRM SDK.  See http://msdn.microsoft.com/en-us/library/gg309536.aspx for more details.  In the picture below, I just started with an empty web app and added the dkdt_ structure below:

image

Then, I simply link/publish the files:

image

Here’s an example of what initial linking looks like:

image

Notice that you can also create a new web resource right from the linker dialog.  Once you’ve linked the files, you can do things like multi-select in Solution Explorer and deploy/publish just the files you care about:

image

But wait, there’s more.  Since this is just a standard web app project, I can take advantage of all the goodness that Visual Studio 2012 has to offer.  For example, I can use NuGet to add the CRM 2011 Client Side VSDoc to get JavaScript Intellisense for Xrm.Page and simply drag and drop the file from Solution Explorer to the open JavaScript file that needs Intellisense and Visual Studio takes care of adding the reference:

image

I can also take advantage of addins which are designed to work with web projects such as Web Essentials 2012.  One example is the JavaScript Minification feature:

image

…which will give me a production optimized version of my JavaScript file.  Because these are just files, I can just relink if I want to test the minified version.  To hit home the “because they are just files” point, I’ll give you another example.  I’ve grown fond of TypeScript.  There’s even an Xrm.Page TypeScript definition file.  Because TypeScript ultimately produces JavaScript files, those files can simply be linked/published.  I think you are starting to see the “it just works” pattern here.

So, for me, I’ve stopped using the Developer Toolkit for Microsoft Dynamics CRM to Create and Deploy Web Resources because many of these things I talk about don’t work with the supplied template.  Furthermore, the Developer Toolkit forces you to deploy all of your web resources every time and it doesn’t publish.  You have to do that manually.  I’ve found the approach I lay out in this post to be the most productive way of using Visual Studio to develop web resources.  However, I still use the Developer Toolkit to Create and Deploy Plug-ins and Create and Deploy Workflow Libraries.

HTH

@devkeydet

Authenticating to an Azure hosted web service from CRM Online Sandbox code

UPDATE (14MAR2013): Uploaded a modified sample which addresses storing credentials in secure config and adds comments around how to encrypt those credentials.  While I show a synchronous plugin in the demo for simplicity of the walkthrough, you should always evaluate whether your plugin logic can fire asynchronously if the action doesn’t require that the user see the result of the action immediately after the record is saved.  Calling an external system from a synchronous plugin can have negative side effects for both user experience and tight coupling with the external system.  When you absolutely must use a synchronous plugin, make sure your code is short lived and try to put your azure web role in a data center as close to your CRM Online data center as possible to minimize network latency.  You should also weigh the pros / cons of using this approach over the inherent Azure Service Bust capabilities Dynamics CRM provides which I talk about in my Integrating with systems behind a firewall from CRM Online using Windows Azure Service Bus Relay post.  While the context of my post is talking to systems behind a firewall, you can also use Azure Service Bus as a mechanism to integrate with a web service running in Windows Azure.  As of the CRM Online December 2012 Service Update and Update Rollup 12, Dynamics CRM includes new support for Windows Azure persistent queues, use of the updated Windows Azure portal to configure ACS, and synchronous plug-in support for posting to the service bus. 

Scenario:

“I need to execute some code from a plugin which requires full trust.  CRM Online doesn’t give me the ability to run full trust code so I want to put my full trust code behind a web service running in a Windows Azure Web Role.  However, every approach I attempt to use to authenticate from the plugin to the web service fails due to sandbox issues.”

While I thought I covered this in my Design for Online post, I learned that my assumptions were incorrect.  I was under then impression that the approach I mention explained in the SharePoint Online and Windows Azure: Developing Secure BCS Connections using WCF Services post which uses UserNamePasswordValidator class would work in CRM Online.  I swear I had it working at one point.  That’s what they all say:).  I recently tried it again and discovered that the client code (the code running in the plugin) required full trust.  I couldn’t figure out a way to get this working in full trust.  I still don’t know why this requires full trust and my attempts at finding an explanation internally at Microsoft came up short.  If someone else has gotten this approach to work, then I would love to know what I am missing. 

Anyway, I had originally been going down a SOAP based approach.  I actually had a sample working from a sandboxed plugin using custom SOAP headers similar to what’s described here.  If you are adamant about wanting to use SOAP, then take a look.  If there’s sufficient comments asking for a SOAP based example, then I will try to find the time to write a post about it.  However, what I realized after I got it all working was that the aforementioned approach is complex and hard to follow.  I saw this as an opportunity to take advantage of the ASP.NET Web API.  I think you’ll find the ASP.NET Web API approach more optimal for this scenario because:

  • We want as efficient communication as possible between CRM Online and the Windows Azure web role.  REST+JSON is the most efficient way I know.
  • ASP.NET Web API makes it really easy to use JSON as the serialization format.
  • It’s pretty easy to implement basic authentication for ASP.NET Web API based services.
  • Basic authentication can be used from a sandbox plugin

Before watching this walkthrough, make sure you go through or at least understand the following ASP.NET Web API walkthroughs:

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

http://www.asp.net/web-api/overview/security/basic-authentication

Watch the following video once you’ve gotten up to speed using ASP.NET Web API:

The video won’t make much sense if you don’t spend the time understanding ASP.NET Web API.  The code sample I show in the video is available here:

http://sdrv.ms/15LCSax

@devkeydet

Microsoft CRM Enterprise Academy

I work with enterprise customers and have been involved in a number of enterprise implementations of Dynamics CRM.  One of the #1 things I get asked all the time is “where is the enterprise CRM training?”  The Microsoft CRM Enterprise Academy is a 5-day instructor-led training which provides 300 level Advanced Development and Advanced Infrastructure content for Microsoft Dynamics CRM 2011 to help address the unique needs of enterprise customers.  Go to http://regonline.com/CRM2013 for full details and registration information.

@devkeydet

Virtual Convergence 2013

Two days ago, the Convergence team announced Virtual Convergence 2013:

https://twitter.com/MSFTConvergence/status/310740576477782016

If you aren’t familiar with Convergence, it is similar to the likes of past technical conferences like PDC/MIX or current ones like TechEd and //build/, but exclusively focused on the Dynamics business at Microsoft.  I am delighted to see that people who couldn’t attend the conference in person can get access to the content for FREE.  Mark your calendars and follow the link to register.

@devkeydet

Calling CRM from ASP.NET using impersonation to ActOnBehalfOf the logged in user

UPDATE: Please review the comments for this post.  There is a better, easier way to do this using OAuth that works with both the SOAP and REST/OData service.

Sometimes you need to run ASP.NET code outside of Dynamics CRM to achieve your goals.  This usually manifests itself either as a page embedded in CRMs main content area which is accessible via a link in the sitemap similar to the following:

 

image

Another place this is often used is embedding external content through an IFrame in a CRM form.  The general approach is covered in the SDK:

Implement Single Sign-on from an ASPX Webpage or IFRAME

Walkthrough: Single Sign-on from a Custom Web Page

Of course, your code will usually need to call back into Dynamics CRM through the organization (web) service to do things like CRUD on CRM data, etc.  In this scenario, you want CRM to execute code under the context of the logged in user.  The CRM SDK covers how to do this here:

Impersonate Another User

Sample: Impersonate Using the ActOnBehalfOf Privilege

See my CRM Online & Windows Azure Series post for a walkthrough of the Single Sign On (SSO) configuration.  The goal of this post is to bring all of these concepts together in as simple of a “hello world” style code sample as possible.  The sample code is actually the code for the embedded page in the screenshot above (called ActOnBehalfOf.aspx).  The solution is made up of an ASP.NET web form, some code behind the web form, and a helper class I built.  In order to get this code to compile, you are going to have to add the necessary .NET assembly references and fix some of the namespaces.  I’ll leave that exercise to you.

ActOnBehalfOf.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ActOnBehalfOf.aspx.cs" Inherits="AdfsEnabledReportViewerWebRole.ActOnBehalfOf" %>

 

<html>

    <head runat="server">

        <title></title>

    </head>

    <body>

        <form id="form1" runat="server">

            <div>

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

                    <Columns>

                        <asp:BoundField DataField="Id" HeaderText="Id" />

                        <asp:BoundField DataField="Name" HeaderText="Name" />

                    </Columns>

                </asp:GridView>

            </div>

        </form>

    </body>

</html>

ActOnBehalfOf.aspx.cs:

 1: using System;

 2: using System.Linq;

 3: using Microsoft.Xrm.Client;

 4: using Microsoft.Xrm.Sdk.Client;

 5:  

 6: namespace AdfsEnabledReportViewerWebRole

 7: {

 8:     public partial class ActOnBehalfOf : System.Web.UI.Page

 9:     {

 10:         protected void Page_Load(object sender, EventArgs e)

 11:         {

 12:             var contextConnection = ActOnBehalfOfHelper.CreateContextAndConnection();

 13:             CrmConnection conn = contextConnection.Connection;

 14:             OrganizationServiceContext ctx = contextConnection.Context;

 15:  

 16:             // CallierId is what forces CRM to execute the API calls within the security context of the CRM User

 17:             conn.CallerId = ActOnBehalfOfHelper.GetCallerId();

 18:  

 19:             var accountQuery = from a in ctx.CreateQuery<Account>()

 20:                         select new Account

 21:                         {

 22:                             Id = a.Id,

 23:                             Name = a.Name

 24:                         };

 25:  

 26:             var accounts = accountQuery.ToList();

 27:  

 28:             GridView1.DataSource = accounts;

 29:             GridView1.DataBind();

 30:         }

 31:     }

 32: }

If you’ve reviewed the resources in this post, then ActOnBehalfOf.aspx and ActOnBehalfOf.aspx.cs should be pretty self explanatory.  It’s a page with a GridView.  The code behind queries CRM for data using the organization service.  Note that Account from line 19 comes from a class file where I used crmsvcutil.exe to generate the class.  I always use Erik Pool’s approach to only generate classes I need in my code.  I digress.  The code sets the CallerId property of the CrmConnection object instance before executing the code.  By doing this, CRM will execute all calls made to through the OrganizationServiceContext instance as the CRM user based on the CallerId value passed in.  CallerId is the GUID of the CRM user who needs to be impersonated.  The ActOnBehalfOfHelper does the real work to get the proper GUID based on the claims available to the ASP.NET page.  Specifically, it uses the UPN claim value to find the CRM user.  Once the CRM user is found, the code returns the Id of the CRM user as a GUID. 

ActOnBehalfOfHelper.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Microsoft.IdentityModel.Claims;

using Microsoft.Xrm.Client;

using Microsoft.Xrm.Client.Services;

using Microsoft.Xrm.Sdk.Client;

 

namespace AdfsEnabledReportViewerWebRole

{

    public static class ActOnBehalfOfHelper

    {

 

// ReSharper disable InconsistentNaming

        private const string CRM_CALLERID = "CRM_CALLERID";

// ReSharper restore InconsistentNaming

 

        public static ContextConnection CreateContextAndConnection()

        {

            var contextConnection = new ContextConnection();

            // Connect to CRM with a single named user (i.e. system account / trusted subsystem model) who has the ActOnBehalfOf privelege

            contextConnection.Connection =

                CrmConnection.Parse("Url=[YOUR_ORG_URL];Username=[YOUR_USERNAME];Password=[YOUR_PASSWORD]");

            contextConnection.Context =

                new OrganizationServiceContext(new OrganizationService(contextConnection.Connection));

 

            return contextConnection;

        }

 

        public static Guid GetCallerId()

        {

            Guid callerId;

            var contextConnection = CreateContextAndConnection();

            var ctx = contextConnection.Context;

 

            // NOTE: I am caching the CallerId to minimize calls to the CRM Organization Service.

            // For production code, you should not store the CallerId in plain text in a cookie.

            // Malicious code, once authenticated, can change the cookie value and execute as another caller.

            // You could apply encryption when creating the cookie and decryption when reading 

            // the cookie value:

            // http://msdn.microsoft.com/en-us/library/windowsazure/hh697511.aspx

            // You could even encrypt/decrypt the cookie name to obfuscate the purpose of the cookie.

            // Alternatively, find a different approach to cache the CallerId value (ASP.NET Session for example)

            // or simply don't cache the CallerId.

 

            HttpCookie callerIdCookie = HttpContext.Current.Request.Cookies[CRM_CALLERID];

 

            // If the cookie exists, reuse the Guid string value to execute the call as the current user

            // If not, then query CRM to get the Guid of the authenticated user based on the upn claim

            if (callerIdCookie == null)

            {

                ClaimCollection claims = ((IClaimsIdentity) HttpContext.Current.User.Identity).Claims;

 

                IEnumerable<Claim> claimQuery = from c in claims

                                                where

                                                    c.ClaimType ==

                                                    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn"

                                                select c;

 

                Claim upnClaim = claimQuery.FirstOrDefault();

 

                var userQuery = from user in ctx.CreateQuery<SystemUser>()

                                where user.DomainName == upnClaim.Value

                                select user.SystemUserId.Value;

 

                callerId = userQuery.FirstOrDefault();

                if (callerId == Guid.Empty)

                {

                    // Send HTTP status code of 403

                    // See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

                    HttpContext.Current.Response.StatusCode = 403;

                    HttpContext.Current.Response.End();

                }

 

                string callerIdString = callerId.ToString();

                HttpContext.Current.Response.Cookies.Add(new HttpCookie(CRM_CALLERID, callerIdString));

            }

            else

            {

                callerId = new Guid(callerIdCookie.Value);

            }

 

            return callerId;

        }

    }

 

    public class ContextConnection

    {

        public CrmConnection Connection { get; set; }

 

        public OrganizationServiceContext Context { get; set; }

    }

}

Note the comments in the code.  I am doing some caching of the user GUID in a cookie.  Right now, the cookie and cookie value is in plain text.  As I state, this is done for simplicity of the sample.  Make sure you read the comments and make the proper adjustments to protect access to the CallerID GUID from malicious code/callers.

@devkeydet

A primer in Extended CRM Development

UPDATE (06MAY2013): If you like the idea of implementing more functionality using point and click configuration (like in Part II) without having to write code (like in Part III), then you might want to have a look at North 52 Formula Manager.  I have a blog post about it here.  Like I say in the post, I get nothing from the makers of the addon by making you aware of it.  However, I am a big fan of it.  I think it’s a significant time saver enabling you to quickly implement functionality that would otherwise require code.  You can decide for yourself after you have evaluated it.

Dynamics CRM 2011 offers a powerful platform for building line of business applications.  These applications are often referred to as Extended CRM applications or XRM Framework applications because they leverage Dynamics CRM 2011 capabilities beyond the typical customer relationship management (CRM) scenarios.  Since I work with US Government, Healthcare, and Education customers and partners, I spend the majority of my time focused on XRM.  I’ve used My “crash course” in Dynamics CRM 2011 Development post as starter resource for folks who want to learn more.  However, sometimes I get the following feedback: “That’s great, but can you give me the short version?  I want the primer, not the deep dive.”  This post is my attempt to do just that.  To learn the fundamentals of XRM, do the following:

  • Watch my XRM in a Nutshell series:
    • Part I: Defining the Data-centric Business Application built using Dynamics CRM (22:16)
    • Part II: Building the Data-centric Business Application using Dynamics CRM (53:24)
    • Part III: Extending the Data-centric Business Application with the Dynamics CRM SDK (51:20)

After watching these videos, the next step would be to read the Building Business Applications with Microsoft Dynamics CRM 2011 whitepaper.  Next, you can dig deeper using my My “crash course” in Dynamics CRM 2011 Development post. 

In Part II, I show modifying the Site Map and Ribbon using my favorite community tools:

http://dkdt.me/TxS9XB

In Part III, I talk about the integrated external wed application with SSO scenario.  However, I don’t show a demo of it.  I cover examples of this scenario as part of my CRM Online & Windows Azure Series.   Specifically in the Configuring Single Sign On (SSO) and Calling CRM from ASP.NET using impersonation to ActOnBehalfOf the logged in user posts.

Finally, if you like these kinds of videos, then you might like How to Customize Microsoft Dynamics CRM 2011 Online which is in a similar vein as my videos.  There’s a little bit of overlap, but I think you’ll still find the content useful. 

@devkeydet

WEBCAST FOLLOW UP: Unit Testing Dynamics CRM Plugin and JavaScript code

Thanks to everyone who attended my presentation on Unit Testing Dynamics CRM Plugin and JavaScript code hosted by http://www.xrmvirtual.com/.  I’ll update this post when the recording is available.  However, the webcast was basically a shortened walkthrough of these two posts:

http://blogs.msdn.com/b/devkeydet/archive/2012/10/31/how-i-develop-and-unit-test-crm-2011-plugins.aspx

http://blogs.msdn.com/b/devkeydet/archive/2012/11/27/unit-testing-crm-2011-javascript-web-resources.aspx

…with some additional “what is and why unit testing” commentary.

Here’s the presentation:

@devkeydet