Friday, April 5, 2013

Scribe CRM2011 adapter and Office 365

So I was banging my head off the wall for about 30 minutes trying to figure this one out.

If you are trying to connect Scribe to an online instance of CRM2011 and you are using an Office365 account make sure that you put this in as the URL:

https://disco.crm.dynamics.com

Otherwise you'll get a connection error.

Tuesday, November 20, 2012

Simple CRM connection example

This post is to provide you with a simple platform for creating CRM connections and using these features of the CRM SDK within your applications.

Things you'll need to follow this post:
  1. Some IDE (I prefer VS2010)
  2. You'll need to generate your class entity files using crmSvcUtil.  I did a post on how to use this tool earlier this year
  3. You'll need the CRM2011 SDK.  This can be found here.
Assemblies you'll need to reference in your project.  (should all be included with the CRM2011 SDK)
  1. microsoft.crm.sdk.proxy
  2. microsoft.xrm.client
  3. microsoft.xrm.sdk
These are the namespaces I commonly use in dynamics CRM projects.

using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Messages;


Now that we have those. Here is the instance of the organization service proxy we need to build first.
internal static OrganizationServiceProxy GetProxy()
        {
            ClientCredentials creds = new ClientCredentials();

            creds.Windows.ClientCredential.UserName =  ConfigurationSettings.AppSettings["username"];
            creds.Windows.ClientCredential.Password = ConfigurationSettings.AppSettings["password"];
            creds.Windows.ClientCredential.Domain = ConfigurationSettings.AppSettings["domain"]; ;
            string url = ConfigurationSettings.AppSettings["orgserviceurl"];
            return new OrganizationServiceProxy(new Uri(url), null, creds, null);
        }
Now, within the methods you want to use this connection for you will need to add or have access to the following.
static void Main(string[] args)
        {
            OrganizationServiceProxy myProx = GetProxy();
            myProx.EnableProxyTypes();
            IOrganizationService myOrg = (IOrganizationService)myProx;
            //orgServiceContext from GeneratedEntities using the CRMServiceUtil
            ExampleContext orgService = new ExampleContext(myOrg);
        }
Don't forget to add this to your app.config or web.config!

  <add key="Username" value="yourUsername"/>
  <add key="Password" value="yourPassword"/>
  <add key="Domain" value="yourDomain"/>
  <add key="OrgServiceURL" value="http://MyURL/MyOrName/XrmServices/2011/Organization.svc"/>



From this point you can use LINQ to do most of the CRUD operations you want to do.
For example, you could get all the account records...
List <Account> accounts = (from a in orgService.AccountSet 
                          select a as Account).ToList();
I'll add a post later for more examples of how to use this for CRUD operations with CRM.


Monday, November 19, 2012

Some useful formatting scripts

Some scripts you can add to your library of useful CRM items!

Derived from this post at StackOverflow

A script for formatting zipcodes (yes I know you probably don't need to format a 5 digit code of number!)
function OnZipFieldChange(context)
{
    var value = context.getEventSource().getValue();
    if (typeof(value) != "undefined" && value != null)
    {
        value = formatZipNumber(value);   
    }
    context.getEventSource().setValue(value);
}

function formatZipNumber(inputValue) {
    var scrubbed = inputValue.toString().replace(/[^0-9]/g, "");

    var fiveDigitFormat = /^\(?([0-9]{5})$/;
    var nineDigitFormat = /^\(?([0-9]{5})\)?[-. ]?([0-9]{4})$/;
    
    if (fiveDigitFormat.test(scrubbed)) {
        return scrubbed.replace(fiveDigitFormat, "$1");
    }
    else if (nineDigitFormat.test(scrubbed)) {
        return scrubbed.replace(nineDigitFormat, "$1-$2");
    }
    
    return inputValue;
}


A script for formatting SSN's

function OnSSNFieldChange(context)
{
    var value = context.getEventSource().getValue();
    if (typeof(value) != "undefined" && value != null)
    {
        value = formatSSN(value);   
    }
    context.getEventSource().setValue(value);
}

function formatSSN(inputValue) {
    var scrubbed = inputValue.toString().replace(/[^0-9]/g, "");

    
    var ssn = /^\(?([0-9]{3})\)?[-. ]?([0-9]{2})[-. ]?([0-9]{4})$/;
    
    if (ssn.test(scrubbed)) {
        return scrubbed.replace(ssn, "$1-$2-$3");
    }
    return inputValue;
}


A script for formatting phone numbers

function OnPhoneFieldChange(context)
{
    var value = context.getEventSource().getValue();
    if (typeof(value) != "undefined" && value != null)
    {
        value = formatPhoneNumber(value);   
    }
    context.getEventSource().setValue(value);
}

function formatPhoneNumber(inputValue) {
    var scrubbed = inputValue.toString().replace(/[^0-9]/g, "");

    var sevenDigitFormat = /^\(?([0-9]{3})[-. ]?([0-9]{4})$/;
    var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    var extDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})?([0-9]*)$/;
    if (tenDigitFormat.test(scrubbed)) {
        return scrubbed.replace(tenDigitFormat, "($1)-$2-$3");
    }
    else if (sevenDigitFormat.test(scrubbed)) {
        return scrubbed.replace(sevenDigitFormat, "$1-$2");
    }
    else if (extDigitFormat.test(scrubbed)) {
        return scrubbed.replace(extDigitFormat, "($1)-$2-$3 x$4");
    }
    return inputValue;
}




Thursday, August 16, 2012

CrmSvcUtil Tutorial

First, what is this handy utility?

The crmsvcutil is a tool to help generate the early bound entities while developing projects for your CRM2011 environment.  The primary advantages I find of using this are

  • Enabling intellisense for your crm entities
  • Allows you to easily use the CRM2011 API rather than being a bad person and updating the database directly.  I know it's tempting to do, but take it from a guy who has had to go back and rewrite whole applications, just don't.
Disadvantages:
  • Regenerating classes to keep up with changes the target CRM2011 environment.  However with this guide you shouldn't have much trouble doing this quickly if need be.

_________________________________________________________________________________
You're preflight checklist....


  1. Do you have WIF (Windows Identity Foundation) installed?  If not, go here.
  2. Do you have the CRM2011 sdk installed?  If not, go here.
  3. (Optional?) Do you have an environment to compile the tool?  I've only ever used VS2010 to do this, I'm sure later versions will work as well.  I had to compile this tool once or twice for a client but I think it comes compiled already with the CRM2011 sdk.  It can be found in the /bin folder where you extract the SDK files.  You will also need this to compile the deviceregistration tool if needed.
_________________________________________________________________________________
Have you got everything you need?  Great!  Now let's get on to some of the steps you may need to take before executing this utility.  Let's answer a question or two.

  • Are you developing for a CRMOnline environment?  Then you will need to run the deviceregistration tool.
    • First compile the tool (in the \tools\deviceregistration directory where you extracted the SDK files)
    • Now open you command prompt and navigate to that directory, use this command line
      • deviceregistration.exe /operation:Register
    • Copy the device id and device password given, you will need these later.
  • Do you want a C# or VB output?  The tool defaults to C#.
Now that we have those common questions out of the way using the tool is pretty straightforward.  Just navigate a command prompt to the directory where the tool is located (usually /bin in the sdk folder) and follow this quick template for an IFD/Local CRM environment.

CrmSvcUtil.exe /url:"http://yourCRMAddress/CRM/XRMServices/2011/Organization.svc" /out:"GeneratedEntities.cs" /servicecontextName:"yourServiceContextName" /username:"domain\username" /password:"******" /namespace:"XrmEntities" 

A more extensive list of parameters can be found here.

Obviously you will need to know the domain name and username you wish to use, this user has to be able to login to CRM.  I usually have some sort of high privileged developer account within any CRM environment I am working on.

The ServiceContextName can be found in your CRM environment by going to the settings page (bottom left) within CRM , then navigating to Customization on the left hand menu, click on Developer Resources and use the "Organization Unique Name" here.

If all is well you should have a new file to include in your development project that will make your life much easier by letting you use all the tools of your IDE with this new class file.