Hosk’s Top CRM Articles of the week – 24th April

Article of the Week

New SDK Capabilities – CRM Online 2015 Update 1 release

Fantastic articles from CRM MVP Daniel Cai on the new features in CRM 2015 SP 1

Best of the Rest

CRM 2013 – How add a status reason using the CRM SDK

How to add a status reason using the CRM SDK so you can specify the state

Where to learn about data visualisation and building better charts

CRM MVP Adam Vero shares some great resources to for making graphs

CRM 2013 – Understanding Status and Status Reason – think before deleting them

Understanding status and status reasons in CRM and be careful when deleting them.

What’s new in CRM 2015 SP1 for developers, customizers and admins

Hosk takes a look at what’s new for CRM 2015 SP1

Microsoft Dynamics CRM Customization: Copy Address Dialog

Copy the address dialog

Building Rich UI Extensions for Dynamics CRM using SparkleXRM

two videos created to show yyou how to use SparkleXRM

How to gather and document a CRM requirements specification – The ebook

A good free ebook on gathering requirements for CRM projects

Microsoft.Xrm.Client Part 1: CrmOrganizationServiceContext and when should I use it?

An article from Scot Durow from a while ago but I only read this week

Dynamics CRM Spring Wave Update 1: Folder Level Tracking

How folder level tracking will work

How to “Convert” Checkboxes into a Percentage Value in 2015 Dynamics CRM #CRM2015

A neat solution to converting checkboxes into percentages

Understanding the new UI in Microsoft Social Engagement

A detailed post on the new UI for the social engagement functionality

Best Practice: Using Built-in Address versus Custom Entity in Dynamics CRM

CRM 2013 – How to add reports to the sitemap

How to add reports to the sitemap, use XRMToolbox

PROGRAMMING LINKS

Just Say No to More End-to-End Tests

Non Code

The Last Thing We Need Right Now is a Vision Statement

Miracle Grow for Your Brain

Defending Darwin – some students will never accept human evolution

Last Weeks Top CRM Articles

Hosk’s Top CRM Articles of the week – 17th April

Useful Hosk Links

Hosk list Of CRM 2013 Tools

A list and review of CRM 2013 tools, this will probably work in CRM 2015 as well

Hosk’s CRM Developer Articles

A collection of my favourite CRM Developer articles I have written

MB2-703 – CRM 2013 Customization and Configuration Certification Information

All the CRM 2013 content to help you pass the exam

HoskWisdom – Hosk Developer Quotes

 Words of Wisdom from the Hosk.  I have written over 900 articles, surely I should have said a few memorable things

 

CRM 2013 – How add a status reason using the CRM SDK

I recently had a tricky problem created by myself when I deleted a Status reason without fully engaging my brain.   Read more about it in the link below

CRM 2013 – Understanding Status and Status Reason – think before deleting them

Adding a Status Reason

If you try to add a status reason in CRM you will quickly find you can specify the label but not the value

optionSets

I’m not sure why it works like this but it was very frustrating when I was trying to add a Status Reason and wanted to specify the value.

Unsupported Database change

Someone did murmer the words

“Direct Database update”

My advice to CRM developers is to not even think about unsupported changes because Microsoft will un-support your CRM if they find out

Why you shouldn’t put unsupported customizations in Microsoft Dynamics CRM

Don’t do unsupported CRM customizations, 99.9 percent of the time there is another way and for the 0.1 percent of the time you should go back and tell the customer you can’t do it because it’s unsupported– Hosk Quotes

Start with the CRM SDK

When using the CRM SDK it usually a journey of discovery, reading one page, which leads you to another page, slowly pick up the clues, information and examples until you have enough information to tackle the problem.

I say it so often it’s made it to my Hosk Wisdom quotes page

Always start with the CRM SDK, you will find the answer to your current problem and solutions to your future CRM problems  – Hosk

 

I knew status reasons fields are option sets and option sets store information

Value (int number)

Text (description)

The description is held as Metadata.  If you want to learn more about metadata read my blog post Understanding CRM Metadata.

Using the Metadata services it’s possible to add, update and retrieve Metadata fields.

The Work with attribute metadata page had a section called Insert a new status value.  This was good but in the example it didn’t specify the value.  I needed to specify the value because I was trying to add back a previously deleted status reason, read this blog post to find out why I was trying to do that.

When using the CRM SDK you often need to earn the answer, this means you need to read, analyse and understand how parts of the CRM SDK work.   This is the sample code

InsertStatus

It’s adding a Statecode (e.g. Active/In Active) but I want the StatusCode.

I look at the msdn page for InsertStatusValueRequest Class, this links to the same code as above but on the left you can see more pages you can look at.

InsertStatus 1

It’s often worth looking at the related documentation for classes in CRM. In this case I look at the InsertStatusValueRequestProperties

InsertStatus 2

There it is Value, yippeee, that’s what I want to set. I can see from the documentation it’s optional, which would explain why it wasn’t included in the sample code.  Most people are not worried what the value of a new status reason is.

I have the theory, now it’s time to put it into practice.

CODE

Here is the code I used to create a status reason and specify the value of the status reason to be 100000001.  Quite how the number got created like that puzzled me, unless a previous developer used the same code!

 // Use InsertStatusValueRequest message to insert a new status 
 // in an existing status attribute. 
 // Create the request.
 InsertStatusValueRequest insertStatusValueRequest =
 new InsertStatusValueRequest
 {
 AttributeLogicalName = "statuscode",
 EntityLogicalName = Incident.EntityLogicalName,
 Label = new Label("Correspondence Withdrawn", 1033),
 StateCode = 1,
 Value = 100000001
 };

 // Execute the request and store newly inserted value 
 // for cleanup, used later part of this sample. 
 int _insertedStatusValue = ((InsertStatusValueResponse)this.crmService.Execute(
 insertStatusValueRequest)).NewOptionValue;

One last point of interest was I hard-coded the UK locale, Microsoft example just specified a variable but didn’t show where it was assigned.  I looked up the value on this page – Locale ID’s assigned by Microsoft.

As this was a one-off, I was worried about the hard coding

What’s new in CRM 2015 SP1 for developers, customizers and admins

When there is a new release for CRM the SDK publishes a what’s new.  I recommend CRM developers read this, I have written a blog post about the subject

Why developers should read the CRM SDK what’s new

Here is my quick review of the last one

CRM 2015 – Hosk takes a peak at the CRM 2015 SDK

CRM 2015 and this update and with this particular update Microsoft seem to be adding lots of useful functionality for developers rather than front end functionality.  This is one of the major development releases for Microsoft Dynamics CRM since I have been a CRM developer.

The consolidation of functionality CRM 2015 is a good idea and will be beneficial to CRM suppliers and CRM developers.

The documentation for CRM 2015 SP1 is excellent.  There are a lot of developer and subtitle updates and added features, all of which are well documentation so thumbs up Microsoft.

My goal of this blog is to summerize the information of the CRM 2015 SP1 update

  • Give useful links
  • condense information
  • Hosk thoughts on the functionality

Useful links for Administrator’s and Customizers

Below is a collection of links about CRM 2015 SP1 in general and not focusing on the Developer enhancements.

CRM Online 2015 Update 1: official pre-release content is here!

This what’s (new is good for non Developer related highlights)

CRM 2015 Update 1 videos

What’s new for administrators and customizers in Microsoft Dynamics CRM 2015 and CRM Online

What’s new for developers

There is so many juicy news bits of functionality, I recommend you read the what’s new for Developers in it’s full glory.

What’s new for Microsoft Dynamics CRM Online 2015 Update 1

Upgrade considerations

This technet article was interesting

Before you upgrade: issues and consideration

IE 10 and 11 are only supported in CRM 2015

Those upgrading from CRM 2013, in CRM 2013 CRM had base and extensionBase tables but these are merged.  There is a section called

Base and ExtensionBase table merge cannot be deferred (if you didn’t know Microsoft got rid of the ExtensionBase table and now only have one table for each entity)

To find out the amount of table data that will be merged during upgrade, see Determine how much ExtensionBase table data will need to merge.

This is a great example why you should never write customizations (web services, reports or plugins,  I’m sure there will be some developers who have written reports based on the extension and base table which will all break when upgrading to CRM 2015.

The page states in place upgrades are not supported for these products (which is worth keeping in mind)

  • Microsoft Dynamics CRM 2013 Reporting Extensions (on-premises only)
  • Microsoft Dynamics CRM 2013 Email Router
  • Microsoft Dynamics CRM List Component for SharePoint Server

Updating CRM 2015 online

I will admit to not knowing if this was changed or if I have just read about it.

Manage Microsoft Dynamics CRM Online updates

The technet article mentions the functionality of delaying and choosing a time to schedule your CRM update.  The bottom line is you have to approve the update before it is applied.

The article mentions you can choose not to deploy an update.  I have wondering what happened to CRM online instances which automatically updated and the chaos in panic testing this would bring about with a set deadline in place.  Some CRM organisation have lots of customizations need a lot of testing.  so it can be some time before a company wants to apply a CRM update

What’s new for developers

My initial plan was to review a lot of the changes in a series of blogs but I noticed today that CRM MVP Daniel Cai has already done it.  He has done a great job, awesome work.  So I changed my plan to build on his good work rather than duplicate it.

New SDK Capabilities – CRM Online 2015 Update 1 release

Daniel has written 9individual blog posts which go into detail about the new functionality and with the all important screen shots.  Daniel has done such a great job there isn’t any point in me doing it again.  I have linked to his blogs and given a quick summary of the functionality.

I recommend all CRM developers read Daniels blog links to get a good idea of the new functionality and don’t just read my quick summary

Fantastic functionality which allows you to create alternative Keys.  Alternative key doesn’t make it initially obvious what the functionality does (to me anyway).  It allows you to create a new unique identifier for a record made up of a combination of columns (e.g. think composite key maybe foreign key).

The columns used to create the alternate key must be either String/Integer/Decimal.

CRM will index these in the background and you can monitor the creation –  Monitor index creation for alternate keys

The result of this means it’s easier to create/update records in CRM and in applications integrating with CRM.

When I read about Upsert I thought it was a joke, Upsert.  The functionality although sounds funny is a great idea.  The premise is it’s an Update and insert.

When using an upsert the functionality checks to see if the record exists, if it does it updates the record. If the record doesn’t exist it creates the record.

Simple but very effective because for integrating applications it avoids the round trip of checking to see if records exist and then choose to create or update.

Another pragmatic piece of code.  This improved functionality allows standard calls to do things like assign values, setting state, before the update you need to use special messages.

This post will give you more details

Perform specialized operations using Update

CRM Online instances can turn on plugin tracing logging, which creates a trace file you can look at.  Very useful considering you couldn’t view any logs before (because you can’t get onto the server).  This would allow you to view the values used in your plugins.

Entity change tracking allows you to detect if the data you retrieve has changed.  This would enable you to do a retrieve and only return the data which has changed from your initial retrieve.

This page from Microsoft will give you more detail and the title explains it’s intended use

Use change tracking to synchronize data with external systems

More CRM integration changes.  This functionality allows you to batch a bunch of actions into one transaction.  You can roll the transaction back if any of them error.  Microsoft Calls this Multiple message execution

Very useful for code integrating with CRM.  Read more on the CRM SDK – Execute messages in a single database transaction

This is a biggie.  This functionality tells you if the data you are going to update has been changed in between you retrieving and updating the information.

It uses a RowVersion value to see if the value being updated is the latest and if so it will throw an error, which means you can retrieve the latest and then update.

CRM SDK link – Reduce potential data loss using optimistic concurrency.

You can now check what version the CRM server is running on.  This offers you the ability to create branches in the code to use different functionality.  This could be useful in commercial solutions or maybe creating solutions to work across different CRM versions.

Try the Web API preview

This is interesting, they are offering you a preview but you shouldn’t use it in production environment (It’s not finished yet I guess).  You might be thinking, why do I care about a preview.   The new web api uses OData v4, OAuth 2, and JSON

It’s this line which intrigues me

The preview release allows you to give feedback and get an understanding of what is coming in a future release. The Web API preview shouldn’t be used on production environments and no support is provided. For more information on the next endpoint,

Learn more here – Web API Preview.

Improved Sub Grid Javascript

You can change the selected view for the Javascript and get the entity references easier

Non developer improvements

A quick summary of what’s in here

What’s new

Improved CRM navigation

quick video

The change now shows all the potential links when you click on an area e.g. sales.

So where before you had to scroll along to see the options, now they are all displayed

The recently viewed is a global tool (e.g. available in many parts of CRM) allows you to view recently viewed records and pin items

Themes

You can change the colours and add a logo.  This is like changing your theme in windows.  It will change all the colours of the bar

Watch a short video (2:45) about themes

Theming post from powerobjects

Onenote integration

Track emails with folders

This is a good productivity improvement.  It allows you to track emails in CRM by copying them to a folder.

folder level tracking

Export to Excel

It shows how much CRM developers use excel to export and import data by the fact I was quite excited by this enhancement.

This has been completely rewritten and you can now import/export 100000 instead of the previous limit of 10000.  This is excellent but strikes me as one of those changes which should have be done a while ago but lets not complain because its in now

New export to excel functionality

CRM Gui Customization changes (non code)

This page is very useful

What’s new for administrators and customizers in Microsoft Dynamics CRM 2015 and CRM Online

This page above includes all the changes and updates for non code customizations, which I sometimes call GUI customizations.  It does also mention some config settings

Older than X

this was limited to months but now you can specify minutes, hours, day, week, years.

Older than 5 minutes

Older than 1 year

Older than 2 weeks

Daniel Cai has written a blog post on this if you want to learn more New Query Operators

Date only field

CRM 2015 SP1 can have Date only fields and Time zone indepdent.  This is different from creating Date and time fields but only displaying the date.  Why did it take so long to add this?

Clear field with Business rules

Microsoft keep adding improvements to business rules which is good and need.  The next improvement allows you to clear fields.

Auto Creating CRM records not just for cases

Previous versions of CRM you could create Cases using emails and other social posts.  The documentation says other social messages but for most people this is emails and perhaps phone activities.

This functionality has been extended to sales and marketing.  So emails and other activities can create System or custom entities.

Rollup fields can use AVG

Rollup field can use AVG in calcluations

Mobile phone and tablet apps have been improved and I will expect this to be mentioned in every new release for the forthcoming future.

Final thoughts

A theme of this update is synchronizing CRM with external systems (applications, web services, 3rd party software).  Microsoft have made it much easier and quicker to synchronise data between CRM and other systems whilst adding functionality for checking data (e.g. to avoid overwriting already changed data)

Microsoft shows you how the changes work together

Synchronize CRM data with external systems

These two images from the page above succinctly show you the potential improvements

Before CRM 2015 SP1

After

I can understand these improvements are very useful but what puzzles me slightly is the sudden motivation to add this functionality into CRM 2015.   If anyone knows the reason or has an idea please add a comment.

Most of the CRM projects I have worked on have been extremely customized and used Microsoft Dynamics CRM for XRM developer.  CRM was the framework to build a solution for the customer.  The new updates in CRM 2015 SP1 take this a step further by making it easier to not only create the main solution but create applications to integrate with the CRM solution.

This fits in with Microsoft’s recent tactics of acquiring applications/software and integrating them with Microsoft Dynamics CRM (Parature, Unified Service Desk, MDM, Social engagement).

CRM 2013 – Understanding Status and Status Reason – think before deleting them

Yesterday I did a complete brain fart.  I was an idiot, I made a mistake and I admit it.

“There’s no way that you can live an adequate life without many mistakes.  In fact, one trick in life is to get so you can handle mistakes. Failure to handle psychological denial is a common way for people to go broke.”

Charlie Munger on Mistakes

Here are some more Charlie Munger quotes on mistakes

Good advice is to admit your mistakes fast and move on.

The earlier you admit making a mistake the more time you have to fix it – HoskWidsom 

The Task – My Mission

The functionality I was going to change on the Incident entity.  It was to move a status reason from Resolved to Cancelled.

StateCode and StatusCode

To understand what needed to be done we need to understand Statecode and StatusCode.  It’s one of those areas which new CRM developers struggle to understand because statecode and statuscode sound similar, it gets a bit more confusing when the display names are different.

Lets clear it up

StateCode = State

StatusCode = Status Reason

The state/Statecode is the status of the record and holds its current state, most CRM entities have two status = Active/Inactive

The default/minimum

0:active

1:inactive

Many of the out of box entities have more states.  The states are commonly used to control the state of the record and to filter records in views in CRM e.g. Active contacts, Inactive contacts, Completed Tasks, Pending Emails

How is Statuscode linked

One State can have many statuscodes (status reasons) and they work like a linked/Dependant option set.

This article Status and Status Reason values in Dynamics CRM 2013 lists all the out of the box Status and Status Reason’s, I’m guessing this information is probably buried somewhere in the CRM SDK but I couldn’t find it.  The two tables below come from the article.

State Status Reason
0:Active 1:Active
1:Inactive 2:Inactive

Many entities in CRM have lots of status reasons.  Here are the

Status                                        Status Reason

Case 0:Active 1:In Progress
2:On Hold
3:Waiting for Details
4:Researching
1:Resolved 5:Problem Solved
1000:Information Provided
2:Canceled 6:Canceled

A few things we know

  • Status is the status of a record
  • Status Reason is the detailed reason or stage of the current status.
  • Status reason allows you to create different stages/reasons for a record.  In the case example a case record is created and I view the status reason as to the detailed explanation of status or the stage.

An active case (e.g. case with status of active) can have a status reason of either in progress, On Hold, Waiting for Details, Researching.

Using the status reasons as a filter you can create views/reports and dashboards showing the records in the different stages.

Option sets

An important point in my story is status and status reasons are special/linked option sets. Option sets in CRM hold two values

  • int value
  • Text value

The int value is held in the database and the text/string value is held as metadata.  I recently blogged about Metadata

CRM 2015 – Understanding CRM Metadata

If you look at the table of case again you can see the numbers which specify the int value of the Status and Status Reasons

Status and Status Reason act slightly differently from normal option sets.  When you add an Option set in CRM it lets you specify the text and the number.

optionSets 1

Status Reason doesn’t let you specify the number value

optionSets

Custom entities have two Status – Active/In Active

For Out of the box entities you cannot add additional Status values.

What I did

I was working on Case (incident schema name) and the task was to remove a status from resolved to cancelled.  The status reason (statuscode) was called Case Withdrawn.

I deleted the Status Reason from Resolved

Created a new Status Reason in Canceled to Case Withdrawn

Things quickly got bad, can you guess what mistakes I have made, here are a couple of things to think about

  • Code using the case status reasons
  • Data in CRM which was set to the Resolved – Case Withdrawn
  • How to set it back

Code

The current system I am working on has a lot of customizations.  Users cannot directly change the status\Status reason of a case record.  Code will be activated by completed tasks or other actions in the system and the status will automatically change.

When I tried to withdraw a case I got an error with this message

961080001 is not a valid status code for state code IncidentState.Resolved on incident with Id 00000000-0000-0000-0000-000000000000

The reason for this error is because the code was using closeIncidentRequest instead of the classic SetStateRequest.

The reason a closeIncidentRequest was being used is because this allows you to specify the values in the IncidentResolution.

If you have closed a case using the CRM GUI, it pops up a form which collects a few values.  If my memory serves me correctly, I believe there is an out of the box report which uses the incident resolution values (possibly calculating the time spent via the time on activities)

The change from of the Case Withdrawn status reason meant the code was now trying to set the case to resolved using a status reason from cancelled.  This didn’t work.

The reason why the code compiled was because I regenerated the Optionset enums file, so the name of the status reason was the same but the value was different.  This value difference would cause me more problems.

Data

The really stupid part of this story is I have done this exact thing before, I did find this problem quicker than last time.  The morale of the story is you have to respect the data

CRM 2013 – What happens when you delete a status reason currently in use

When you change a status reason or remove an option set value is you might have a lot of records who have an option set number value which doesn’t exist.  You must create a plan to Migrate the data.

Better choices than deleting

Rename the Status record and reuse.  Migrate the data as a manual step

rename the status record to DO NOT USE. Migrate the data as a manual step.

It’s important to understand you will need to migrate or at least identify the records which will need migrating before you import and publish your changes.

To find the data with Status reasons pointing to the deleted I had a bit of a tricky problem.  I didn’t have access to the SQL Server, so I instead I used

fxb FetchXML Builder created Jonas Rapp

I had been meaning to test it out and it worked very well.  To use it all you need to do is drop the dll in your XRMToolBox folder, awesome.

I used this to find the cases which I needed to do something with.

How to set it back

It turned out the bug was a bit bigger than initially anticipated and it was going to be fixed in this release, which mean I had to change everything back.

This proved to be trickier than I originally thought it would be due to the CRM GUI doesn’t let you specify the number for the Status Reason.

I could add in a Status Reason called Case Withdrawn but it would have a completely different int/value number, arghghg.

We were going to restore the database but then I worked out how to add a status reason using the CRM SDK which allow me to specify the number for the status reason.

Stop, think and then act

I often recommend CRM developers should stop and think before they code, this is useful advice before you make any customizations.

When making changes, explain the changes first to your Cardboard developer buddy (Why all developers should be friends with a cardboard developer) and work out the reasons why it might be a bad idea.

A major part of CRM development is avoiding mistakes, missing the gotchas (which is why experience is usually very useful read Why .NET developers struggle with CRM Development to understand why) and this was a classic case of act in haste and take half a day to tidy up CRM.

CRM 2013 – How to add reports to the sitemap

A customer wanted to add reports to the sitemap.

Actually this is wrong the customer said they wanted to add a reports button, I knew it was a change to the sitemap.

What is the sitemap?

I think of things from a CRM 2011 perspective because I have done most customization in CRM 2011 environments and that’s how it should be structured

When I say Sitemap this is the left hand menu in CRM 2011

CRM 2011 left hand menu

This page Change application navigation using the SiteMap

in CRM 2013 it draws the left hand menu up on the top but the underlying setup is still exactly the same (picture borrowed from Gareth Tucker’s great CRM blog

Above is the main menu and then if you click one like sales you then get another menu below

Sitemaps are a bit of an odd

Microsoft believe CRM Developers should modify the sitemap by editing XML files, this is tricky and prone to syntax errors.

This page has good detailed instructions –Change application navigation using the SiteMap if you wish to try it by hand

I don’t know any CRM Developer who has manually edited an XML file in versions of CRM 2011 and above.

The reason Microsoft can do this is because the CRM community have some fantastic CRM developers who have created Site Map editors.  There are probably  a few to choose from but my Site map editor of choice is

XrmToolbox for Dynamics CRM  2011/2013/2015

Not only does it have the sitemap editor but it has lots of CRM tools, if you haven’t downloaded it then I would recommend you do it NOW!

site map editor top

How to Add reports

Adding reports couldn’t be easier with the Site map tool (I sound like a salesman)

Open up the XRMToolBox

Connect to your CRM instance

Open SiteMap Editor

You get the screen below

site map editor

To understand what is what, it’s best to have CRM open and you can get a feel to how the names and groupings.

Find the area you want to add the report button

Right click and select – Add SubArea

Give it a name and select the reports entity

site map 2

Not to be confused with Ribbon editing, which is where you edit the ribbon on an individual record e.g. Contact ribbon, account ribbon

If you are going to edit the ribbon (also XML editing) then I would recommend you use the Ribbon Workbench written by Scott Durow

You can download it here

Hosk’s Top CRM Articles of the week – 17th April

Article of the Week

2013 CRM Field Guide now available!

Great post from CRM MVP Julie Yack telling people what the new 900 page CRM 2013 book written by CRM MVP’s contains.

CRM 2015 – Typescript is ready to go, definition files available on NuGet

Hosk article of the week, talking about TypeScript and CRM

Best of the Rest

CRM 2015 – Understanding CRM Metadata

An article about Metadata in CRM, what it’s used for and how to get it

What’s slowing down your CRM development?

What’s slowing down you CRM development?

New default Access Mode in Dynamics CRM Online

default access has changed you may need to give users more rights

Object Caching in .NET 4 and CRM

I take a look at Object caching and CRM

reattach

A very useful tool to quickly reattach to a debug process with this funky visual studio tool

Can I do that in Dynamics CRM?

CRM MVP Scott Durow creates this cool tool

New naming conventions for Microsoft Dynamics CRM updates

Updated Naming Conventions for Microsoft Dynamics CRM Updates

The ever confusing naming convention of CRM releases is explained

 Podcast and Overview: Microsoft Dynamics CRM 2015 Update 0.1 (Update Rollup 1)

CRM in the field podcast you up with the latest news on the new release of CRM

Passing MB5-705: Managing Microsoft Dynamics Implementations

Good post on passing Sure step

Microsoft Exam MB5-705: Preparation

it’s mentioned in Leon’s blog but it’s such a good resource it deserves its own mention

CRM 2013 – How to get rid of annoying pending email warning message

instructions on how to get rid of the pending email warning message

To Code or Not to Code in Dynamics CRM

When it’s time to create a customization in CRM you have the choice for a no code or code solution

CRM 2013 – Importing users using the Data Import Wizard

How to import users into CRM 2013 a step by step guide

PROGRAMMING LINKS

high Cohesion – Loose Coupling

great article on coupling and cohesion

Introducing the IE Diagnostics Adapter for third party developer tools

Interesting Microsoft

Best Practices in Asynchronous Programming

Great article on

Last Weeks Top CRM Articles

Hosk’s Top CRM Articles of the week – 10th April

Useful Hosk Links

Hosk list Of CRM 2013 Tools

A list and review of CRM 2013 tools, this will probably work in CRM 2015 as well

Hosk’s CRM Developer Articles

A collection of my favourite CRM Developer articles I have written

MB2-703 – CRM 2013 Customization and Configuration Certification Information

All the CRM 2013 content to help you pass the exam

HoskWisdom – Hosk Developer Quotes

 Words of Wisdom from the Hosk.  I have written over 900 articles, surely I should have said a few memorable things

 

Object Caching in .NET 4 and CRM

The CRM developer Sir Les told me how he used .NET 4 caching in some plugin code to cache some query data which was used by a plugin.

My first response was

What the heck is caching .NET4?

Quickly followed by

I want to have a look at the code and see how the magic works.

What is Object caching .NET 4?

Object caching is baked into the core .NET files and can be found in the namespace

System.Runtime.Caching

The classes in this namespace provide a way to use caching facilities like those in ASP.NET, but without a dependency on the System.Web assembly.

From a CRM point of view caching was brought in with version .NET 4.0 so this means you can use caching with CRM 2011, CRM 2013 and CRM 2015.

I will add at this point I haven’t really used any other .NET caching but from the link above it seems you could do caching using the System.Web but I don’t really know much about it and it doesn’t seem like it’s a good time to learn about what was but instead we shall learn about what is in

System.Runtime.Caching and the Object caching it offers.

This type of caching is InMemory caching using the object

MemoryCache

This offers a simple way to cache some object in the .NET memory, which sounds like a useful tool for plugins running on the CRM server when using static (ish) data.

The MemoryCache Constructor has some interesting points

MemoryCache Constructor

When this constructor is invoked, configuration settings are first retrieved from application configuration files. If no configuration entries exist in the application configuration file, only the settings provided in config are applied

There is no mechanism to enforce unique names for cache instances. Therefore, it is possible to have multiple cache instances with the same name.

An important point is it won’t stop you adding duplicate keys so you must monitor and keep track of this yourself.

The last class you need to know about is

ObjectCache 

Reading articles about ObjectCache and looking at code examples it seems to be an easy to use and not very complex method to cache objects.  This may mean you might run into problems with creating multiple objects cached with the same name, so I advise some caution.

Lets see the code

In this line, we are obtaining a reference to the default MemoryCache instance. Using the new .NET Caching runtime, you can have multiple MemoryCaches inside a single application.

You need to add this using

using System .Runtime .Caching ;

This line gets a reference to the default MemoryCache instance

ObjectCache Cache = MemoryCache .Default ;

To add something to the cache

const string key = “IncidentMetaData”;

List<string > testData = new List<string> { “Meta1″, “Meta2″, “Meta3″ };

Cache.Add (key, objectToCache, DateTime.Now.AddHours(24));

The code has added the List<string> test data into the memory cache with the name “IncidentMetaData”.  From a CRM perspective this is a similar to items in the Attributes collection on an entity.

You can get things out from the cache by doing this

cache.Get(keyText)

The keyText is the name of the cacheObject you want to retrieve, if you wanted to get the example above you would type in “IncidentMetadata”.

There is other functionality which can monitor changes for cached files.

There is other functionality to remove items from the cache.

Cache.Remove(key);

Code to get started

I have only briefly only mentioned the functionality, I would advise you to read this blog and download the sample code and you can use the static class.  This first blog I found the most useful

Object Caching – .NET 4

These other blogs will give you some more reading and examples

System.Runtime.Caching Namespace

How to: Cache Objects Simply using System.Runtime.Caching.MemoryCach

Working with System.Runtime.Caching.MemoryCache

When to use the in memory cache with CRM

Most of the time I don’t think you would need to use this code in CRM plugins because it’s easy to store and retrieve data from CRM using LINQ or QueryExpression queries.

The in memory cache is suitable for queries which return the same data most of the time.

This could be things like

  • Language settings
  • Config file settings
  • Metadata

In the example I am using I am retrieving all the metadata for the incident entity and this data would be retrieve many times in short period of time whilst 100 of records would be imported.  In this scenario it made sense to  cache the data.

Consider this would create a cache on each CRM server because the cache works on server basis.

plugins triggered by importing

if you have plugins which are triggered when importing lots of records, you may benefit from caching some queries.

I thought I would blog about this because it seemed like an interesting tool developers might want to use.  Be aware there could be limitations and gotcha’s I haven’t worked out yet

I certainly wouldn’t overuse this because most queries retrieving records in CRM run quickly, with the majority of plugins performance is not an issue