Saturday, March 10, 2012

Updating Database in EntityFramework "Code-First"

Today i have used EntityFramework "CodeFirst" for the first time.
I think it is a very interesting approach but the VisualStudio Integration could be more intuitive.

I have integrated it in an ASP.NET MVC Application. This task is described perfectly in a short video from Scott Hanselman.

What is missing in the video and took me some time, is the update of the database structure if the model has been changed (Well it is a MVC-Video, not an EF-Video). If you are using "Database First", you can simply update your .edmx file. In the "Code First" scenario we have to do some manual stuff. 

After you have created the application in the video (or your own EF-Based Application), run it and create some entries in the database. Than add an additional Property to one of your Model-Classes. For udpating the database structure run the following in the Package Manager Console.

PM> Enable-Migrations
PM> Add-Migration AddAppointmentSubject
PM> Update-Database

The "AddAppointmentSubject" can be named by your own. Here the structure is <task><class><property>.

There seems to be some issues/changes with the Version 5 (PreRelease) of EntityFramework. This stuff is running great on 4.3.1.

More details about updating the Model (e.g. new classes) you can find here.

Kind Regards,
Markus

Saturday, February 25, 2012

CRM 2011 - Create Custom Workflow Activities

Because the Release Preview Guide February 2012 contains the following statement,


CUSTOM WORKFLOW ACTIVITIES IN CRM ONLINE
With the  Microsoft Dynamics CRM Q2 service update, developers can build  custom  .NET  Workflow activity assemblies for Microsoft Dynamics CRM Online. These custom assemblies can be developed and deployed as part of a Microsoft Dynamics CRM solution package. Once a custom workflow activity has been deployed, it can then be utilized within the workflow engine for business process management. These new capabilities ensure parity in the developer experience between Microsoft Dynamics CRM Online  and on-premises. It also empowers organizations to bring external data, custom actions and integrations into their standard CRM business processes.


the Custom Workflow Activities could be interesting, also for product development in CRM 2011. Today, the Custom Workflow Activities are running in OnPremise Deployments but not in Online Systems. Because the Microsoft SDK Example is a little too confusing for a first approach, i have decided to create this small examle (based on the SDK-Stuff).

The Problem:
Create a Custom Workflow Activity, that creates a (follow up-)Task after an Account was created. (This is just an example. You can use default workflow functionality to reach this target.)


1. Create Solution with Project
The first step is to create a Visual Studio 2010 Project.
  • Select Visual C# -> Workflow -> Activity Library
  • Select .NET Framework 4
  • Select Name for Solution and Project
  • Select Location
Visual Studio Project
2. Check Project Settings
Check that the Framework-Type is NOT .NET Framework Client Profile. The correct one is .NET Framework 4.

Target framework (should be .NET Framework 4)
3. Create the Class
Delete the automatically created class Activity1.xaml and create a class called CreateTask.cs.

The new class

4. Adding References and Usings
Add references to the following assemblies (Rightclick References in the project and add...).
  • microsoft.xrm.sdk.dll
  • microsoft.xrm.sdk.workflow.dll
Add the reference for System.Runtime.Serialization, which is part of the .NET Framework.

Serialization
(Hint: The Add References window here is not the default VS2010 one. This one is part of the Productivity Power Tools. A (MUST HAVE-) Addon for Visual Studio 2010!)

Add the following using-Statements to your class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;


References and Usings
5. Inheritance
Make our class inherit from CodeActivity (System.Activities) and implement the method Execute. You also have to make the class public.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
 
namespace MK.CRM2011.CustomWorkflowStep {
    public class CreateTask : CodeActivity {
    }
}

6. Input Parameters
We would like to put a parameter in our Workflow. So every customer can define the subject of the task by it's own.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
 
namespace MK.CRM2011.CustomWorkflowStep {
    public class CreateTask : CodeActivity {
 
        #region Input Properties
        [Input("Task Subject")]
        [Default("Empty Subject")]
        public InArgument<string>TaskSubject{ get; set; }
        #endregion
 
        protected override void Execute(CodeActivityContext context) {
            throw new NotImplementedException();
        }
    }
}

-------------------
The following code has to be added into the Execute-Method.

-------------------

7. Create Context and Service
The context we need for retrieving the ID of the current record, the service is required for communication with CRM-Webservice (IOrganizationService).

//Create the IWorkflowContext and the
//IOrganizationService for communication with CRM
IWorkflowContext workflowContext = 
    context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = 
    context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = 
    serviceFactory.CreateOrganizationService(workflowContext.UserId);


8. Business Logic
Now we have to add the logic which does the following.
  • Read Text from "TaskSubject" Input Parameter
string newSubject = TaskSubject.Get<string>(context);
  • Create a new task object
Entity newTask = new Entity("task");
  • Set subject of the task to TaskSubject value
newTask["subject"] = newSubject;
  • Set regardingobjectid of task to PrimaryEntityId of context (this is the id of the account).
newTask["regardingobjectid"] = 
    new EntityReference("account", workflowContext.PrimaryEntityId)
  • Create the new task in CRM
Guid taskId = service.Create(newTask);



9. Sign the assembly
First of all, go to project settings and sign your assembly.
Right Click the Project -> Properties -> Signing -> Sign the assembly -> New -> Enter Key file name "testkey" -> no password (for testing purposes).

Signing
10. Compile and deploy the assembly
Rebuild your solution and deploy the assembly to the CRM System. This can be done by the PluginRegistrationTool of microsoft. You can find this as part of the current CRM-SDK. You have to compile it by your own. Choose the following settings.

Deployment
11. Create a Workflow with the new step
Create a default Workflow in CRM. Choose the following settings.
Workflow basic configuration

Add the new step 
Push the SetProperties-Button of the step and select a default name for the Input Parameter (TaskSubject). Because the Owner of the new task should see which account was created, we add the name of the account as input parameter.

Set input parameter
Now save and don't forget to activate  the Workflow.
12. Testing
Create a new Account in CRM.
Create Account
Processing the workflow can take some seconds. Open the Activity List of the account and select Filter on "All". You should see our created task. The subject contains some static text and the name of the created account.
Activity View
13. Code Overview
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
 
namespace MK.CRM2011.CustomWorkflowStep {
    public class CreateTask : CodeActivity {
 
        #region Input Properties
        [Input("Task Subject")]
        [Default("Empty Subject")]
        public InArgument<string> TaskSubject { get; set; }
        #endregion
 
        protected override void Execute(CodeActivityContext context) {
 
            //Create the IWorkflowContext and the
            //IOrganizationService for communication with CRM
            IWorkflowContext workflowContext =
              context.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory =
              context.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service =
              serviceFactory.CreateOrganizationService(workflowContext.UserId);
 
            //Retrieve data from InputParameter 
            string newSubject = TaskSubject.Get<string>(context);
 
            //Create the new task object (in memory)
            Entity newTask = new Entity("task");
            newTask["subject"] = newSubject;
            newTask["regardingobjectid"] =
              new EntityReference("account", workflowContext.PrimaryEntityId);
 
            //Create task in CRM
            Guid taskId = service.Create(newTask);
        }
    }
}

-------------------

Ok, this is more detailed than i have planned. Hope it helps!
Regards,
Markus

Thursday, December 1, 2011

CRM 2011 - Retrieving Organization Information

There are many ways to retrieving Organization-Information in code. Some of them are explained in this post.
  • Javascript
  • Organization-Webservice
  • Discovery-Webservice 
  • ...
1.) Javascript (Only Unique-Name)
The XRM-Context offers the a function for retrieving the org unique name.
Xrm.Page.context.getOrgUniqueName()

 This function can be used in Javascript/HTML- and Silverlight-Webresources.

2.) Organization-Webservice (Only Display-Name)
The SOAP-Endpoint of the CRM-System (http://crm:5555/XRMServices/2011/Discovery.svc) offers the default request-functionality for the "organization" entity. The record contains the field "name" which holds the display-name.

Important: If the system is created with import organization and the display-name has been changed in this process, the name in the organization-table of the _MSCRM Database is NOT changed. It still contains the name, selected generating the organization the first time.


3.) Discovery-Webservice (Display & Unique)

The discovery-endpoint also contains a functionality to retrieve organization data. This can be achieved by using the RetrieveOrganizationRequest.

Two remarks if you are using this approach:
  • There is no documentation available, how you can use discovery-endpoint in Silverlight-Webresources
  • The code will not run in offline-mode (SDK: This message works only when connected to the server.)
Update November 2012: The RetrieveOrganizationRequest is not working correctly in online systems for this purposes. Thanks to Mitch Milam.

Cheers,
Markus

Wednesday, November 30, 2011

CRM 2011 - "HTTP 500 - Internal Server Error" using SOAP Endpoint in Silverlight Webresources

Requesting data over organization endpoint results in the following error.
HTTP 500 - Internal Server Error

The problem may be a missing provider-setting in the IIS configuration.


Solution:
  • Open IIS Manager
  • Select "Microsoft Dynamics CRM" Site
  • Doubleclick "Authentication"
  • Right click "Windows Authentication"
  • Select "Providers"
  • Add "Negotiate"
  • IIS-Reset


 There should be two "Provider" entries.
  • Negotiate
  • NTLM
Cheers,
Markus