Pogo69's Blog

March 22, 2012

CRM 2011 LINQ Provider – FormattedValues Collection

Filed under: C#, CRM — pogo69 [Pat Janes] @ 09:06

I just re-discovered an idiosyncrasy of the CRM 2011 LINQ Provider that I thought I should share in here, as it may help others who try and fail to retrieve formatted values for attributes such as OptionSets.

The FormattedValues collection will contain entries:

  • Only for those attributes that require it (e.g. text attributes format as the text they represent – no formatting required)
  • Only for those attributes for which the values have been requested in your query

So, if you write a query such as the following:

var contacts =
	(
		from c in xrm.ContactSet
		join a in xrm.AccountSet on c.ParentCustomerId.Id equals a.Id
		where a.Name == "Acme Pty Ltd"
		select new
		{
			Name = c.FullName,
			DOB = c.BirthDate,
			Gender = (c.FormattedValues.Contains("gendercode") ? c.FormattedValues["gendercode"] : "Ambiguous")
		}
	);

the Gender of each Contact will ALWAYS be “Ambiguous”, as we have not queried for the GenderCode attribute.

Alter the query to the following:

var contacts =
	(
		from c in xrm.ContactSet
		join a in xrm.AccountSet on c.ParentCustomerId.Id equals a.Id
		where a.Name == "Acme Pty Ltd"
		select new
		{
			Name = c.FullName,
			DOB = c.BirthDate,
			GenderCode = c.GenderCode,
			Gender = (c.FormattedValues.Contains("gendercode") ? c.FormattedValues["gendercode"] : "Ambiguous")
		}
	);

only those Contacts whose Gender truly is ambiguous, will return “Ambiguous”.

Thanks to ms_crm for the inspiration!

Advertisements

9 Comments »

  1. Very helpful! I was having this problem and new there had to be some trick to it. Thanks for saving me!

    Comment by Kenny — March 28, 2012 @ 02:38

  2. Same here. Thanks for the help. Many dead end attempts until finding this little nugget.

    Comment by Griff — October 23, 2012 @ 14:46

  3. Ok, here’s an interesting twist. I have a new entity that uses FormattedValues. All works fine without any of the above trickery…. Except, when I add a “new_” attribute to the select. Any other attribute works fine, but when I add a “new_”attribute the FormmatedValueCollection is 0 instead of 7.

    So for instance;
    select new
    {
    Name = c.FullName,
    DOB = c.BirthDate,
    Gender = c.FormattedValues[“gendercode”];
    }

    works, but .
    So for instance;
    select new
    {
    Name = c.FullName,
    DOB = c.BirthDate,
    SSN = c.new_SSN,
    Gender = c.FormattedValues[“gendercode”];
    }

    Fails with an error on FormattedValues that says “The given key was not present in the dictionary”

    Any thoughts?

    Comment by Louis — October 25, 2012 @ 04:38

    • guess it must be for any custom fields where the above workaround is needed!

      Comment by bruce — November 13, 2012 @ 14:44

    • Hi Louis,

      I am perplexed only by the fact that your former query worked – it should not. If you do not include the optionset attribute in your select statement, the corresponding entry in the FormattedValues collection should not exist. Try:

      select new
      {
      Name = c.FullName,
      DOB = c.BirthDate,
      SSN = c.new_SSN,
      GenderCode = c.GenderCode,
      Gender = c.FormattedValues[“gendercode”];
      }

      Comment by pogo69 — November 13, 2012 @ 15:03

  4. Awesome,thanks…!I was about to explode !:) …actually reminded me of something similar i saw a few months ago,with plugins and context tracking etc but thats for another day!

    Comment by bruce — November 13, 2012 @ 14:45

  5. Thanks very much, basically when picklist value is null and we don’t assign any value to it, throughs error message KeyNotFoundException 4 “The given key was not present in the dictionary”.

    Comment by eBusiness Guru (@inf4web) — May 2, 2013 @ 19:46

    • Correct. Whenever accessing collections such as these, it is good practise to code defensively and check for the existence of a value before attempting to access it. Something like:

      string formattedValue = (entity.Contains("new_optionsetattribute") ? entity.FormattedValues["new_optionsetattribute"] : "N/A");

      Comment by pogo69 — May 6, 2013 @ 09:14

      • Here is the strange bit, I am checking if the contain returns true or not before trying to access formatted value. Contain returns, true but formattedValue is empty.

        I am using this in a plugin for update with pre image entity and current entity. pre image shows the label but the current label is empty.

        Comment by kmria (@kmria) — May 7, 2016 @ 01:27


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: