Navigation
« Book update CRM 2011 | Main | Using the CRM Service via the DataContext »
Wednesday
Jun022010

Old SDK – New SDK Query by OwnerId

The latest update to the MSCRM SDK included a new LINQ provider. In this blog post we are going to compare and contrast querying for data by owner ID using the old and new SDK techniques. If you are new to the LINQ provider then you should read my quick start blog post here first.

For this example let’s assume I want to retrieve accounts where the owner ID is the current user. The following is straight from the CRM SDK and uses the lower level API calls. Keep reading past it and I will use the new LINQ provider to accomplish the same thing plus a little bit.

Guid principalId = user.UserId;

// Create a column set holding the names of the columns to be retrieved.

ColumnSet colsPrincipal = new ColumnSet();

// Set the properties of the column set.

colsPrincipal.Attributes = new string[] { "name", "accountid" };

// Create a ConditionExpression.

ConditionExpression conditionPrincipal = new ConditionExpression();

// Set the ConditionExpressions properties so that the condition is true when the

// ownerid of the account equals the principalId.

conditionPrincipal.AttributeName = "ownerid";

conditionPrincipal.Operator = ConditionOperator.Equal;

conditionPrincipal.Values = new object[1];

conditionPrincipal.Values[0] = principalId;

// Create the FilterExpression.

FilterExpression filterPrincipal = new FilterExpression();

// Set the properties of the FilterExpression.

filterPrincipal.FilterOperator = LogicalOperator.And;

filterPrincipal.Conditions = new ConditionExpression[] { conditionPrincipal };

// Create the QueryExpression.

QueryExpression queryPrincipal = new QueryExpression();

// Set the properties of the QueryExpression.

queryPrincipal.EntityName = EntityName.account.ToString();

queryPrincipal.ColumnSet = colsPrincipal;

queryPrincipal.Criteria = filterPrincipal;

// Create the request object.

RetrieveMultipleRequest retrievePrincipal = new RetrieveMultipleRequest();

// Set the properties of the request object.

retrievePrincipal.Query = queryPrincipal;

Now that we have looked at how we would have done this in the past, let’s look at what is possible using the new LINQ provider. First, you should have already read my other blog post where I talked about how to call Who Am I to get the current user ID here. For this example we startup assuming that has already been accomplished.

To make this a little more interesting, in addition to just retrieving the accounts for the user I also want to retrieve the user’s name and only the first 10 accounts.

The first thing we are going to do is build the LINQ expression that will provide the criteria for our query. The following queries for accounts that have an owner ID matching. It also shapes the results into an anonymous type that only has the properties ID and Name. Pause for a second and compare the following 7 lines, which could really be expressed as 1 line to the code we used above without the LINQ provider.

var principalQuery = from acct in ctx.accounts

where acct.ownerid.Value == currentUserID

select new

{

ID = acct.accountid,

Name = acct.name

};

Remember, the above just defines the LINQ expression it doesn’t cause execution of the query against the CRM server that will happen later when we start to enumerate the results. Next, let’s go get the user information so we can grab the full name of the user.

var userInfo =

ctx.systemusers.Where(u => u.systemuserid == currentUserID).FirstOrDefault();

If we retrieved a user the output the user name.

if (userInfo != null)

Console.WriteLine("First 10 Accounts for user " + userInfo.fullname);

Next, we are going to enumerate the accounts and print the account name. Notice that we added a Take(10) onto the LINQ query we built earlier. This will cause only the first 10 records to be used.

foreach (var acct in principalQuery.Take(10))

Console.WriteLine(" Account Name :" + acct.Name);

Hopefully it’s clear from the example the new CRM LINQ provider that’s part of the updated SDK makes it much easier to build queries against the CRM data.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (12)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: speedupmypc
    speedupmypc 2013 (full version)
  • Response
    855 You'll need 9 darkish strips and eight white strips with the weave. The only real steady that continues to be inside our kid's lives, year in, 12 months out, louis vuitton medium agenda looks being their just about phobic distaste for strolling.
  • Response
    Response: Joseph Chinnock
    CRM as a Rapid Development Platform - Journal - Old SDK - New SDK Query by OwnerId
  • Response
    Response: Belinda Broido
    CRM as a Rapid Development Platform - Journal - Old SDK - New SDK Query by OwnerId
  • Response
    CRM as a Rapid Development Platform - Journal - Old SDK - New SDK Query by OwnerId
  • Response
    Response: ezines for writers
    CRM as a Rapid Development Platform - Journal - Old SDK - New SDK Query by OwnerId
  • Response
    Response: expert advisor
    CRM as a Rapid Development Platform - Journal - Old SDK - New SDK Query by OwnerId
  • Response
    Response: Toruń
  • Response
    Response: office setup
    Get Help to Install Microsoft Office on PCs Laptops etc.Support Number 1-800-756-1088
  • Response
  • Response
    Response: qooapp download
  • Response

Reader Comments (137)

Office 2007 makes life great!
Many people like Microsoft Office.
Microsoft Office 2007 is welcomed by the whole world.
Windows 7 make life wonderful!
Microsoft Office 2010 is so great!
Office 2010

February 15, 2011 | Unregistered CommenterOffice 2007

kumaara :
Very interesting article,all the information is so beneficial and valuable for us.presentation of article is attractive,so I will bookmark it for sharing it with my family and with my friends.
Thanks for sharing nice and pretty post.

February 28, 2011 | Unregistered CommenterKumaara

Furthermore, we are constantly updating our CompTIA A+ Essentials Exam. These 70-680 Exam updates are supplied free of charge to Certify comptia network customers- hereby becoming an investment rather than a disposable SY0-201. Our clients receive the most reliable and up-to-date information when they decide to take the comptia security, just contact us. Our candidates walk into the free test questions Room as confident as a Certification Administrator.

March 21, 2011 | Unregistered Commenterqiao456

Life today is full of stress and brings us in frequent contact with toxins and environmental contaminants, but all of us can take advantage of the many benefits of this centuries-old method of enhancing health by having infrared saunas in our home or yard. By taking all of the above information into account, you will be able to make an informed choice that will allow you to make the very benefits of your new bathroom accessories.

April 1, 2011 | Unregistered CommenterInfrared saunas

High-quality Replica Ferrari GMT Watches possess all the elegance and sophistication of an authentic, at a fraction of the price. The beautiful men’s Replica Grachams Chronofighter Oversize Watches are available in a variety of different styles and dial designs. We get our merchandise from only the best suppliers, and carefully examine each watch before shipment so there’s no need to worry on your part. We know how important your time is, so we offer extremely fast shipment and fast replies to inquiries. Our customer service department guarantees a reply to your email within three hours of receipt, seven days a week! Find your ideal Replica Grachams Chronofighter Swordfish Watches here!

June 8, 2011 | Unregistered Commentercarlalee

Discount Mulberry bags
Mulberry Bayswater

June 16, 2011 | Unregistered CommenterDiscount Mulberry bags

Discount Mulberry bags
Mulberry Bayswater

June 16, 2011 | Unregistered CommenterDiscount Mulberry bags

Discount Mulberry bags
Mulberry Bayswater

June 16, 2011 | Unregistered CommenterDiscount Mulberry bags

Discount Mulberry bags
Mulberry Bayswater

June 16, 2011 | Unregistered CommenterDiscount Mulberry bags

Everyone is different though and, as a
Onitsuka Tiger

Shoes result, people have to choose the best running
Asics Japan Ar shoes that

fit
Asics Gel Kinsei 2

their personal
Onitsuka Tiger

Coolidge Lo needs. Asics
Asics Alton understands this

and adjusts their shoes for foot size, shape, and other factors that are unique to
Asics Gel Lyte III

the
Asics Kanuchi
Asics Olympics runner.

With so many
Asics Whizzer Lo great
Onitsuka

Tiger Mexico 66 Deluxe
Onitsuka Tiger

Mexico 66 styles
Onitsuka Tiger

Mini Cooper to
Onitsuka Tiger

Seck Lo choose from how can you pick what's right
Asics Gel DuoMax for

you?
Onitsuka

Tiger Mexico 66 Baja
Asics Gel Stratus

2.1 First,
Asics

Onitsuka Tiger Mexico 66 make sure you consider how frequently you train and your

performance level. For example, if you run a lot you may want a
Asics Revolve Le shoe

with the GEL Cushioning
Onitsuka

Tiger California 78 System. Also,
Onitsuka Tiger

Ultimate 81 be
Onitsuka

Tiger X Tokidoki Fabre sure to measure
Onitsuka

Tiger Mexico 66 Mid both the length and width of your feet every time you go shopping for

new shoes in order to
Buy Asics Shoes account for
Onitsuka

Tiger Mexico 66 Lauta any
Onitsuka

Tiger Tokidoki Mex Lo possible changes
Women's Asics Shoes

Cheap Asics Shoes
Asics Shoes Australia
Asics Shoes Sale in
Onitsuka Tiger

Rotation 77 size or shape. This will help you find an
Asics Top Seven Asics

shoe with just.

June 28, 2011 | Unregistered CommenterCheap Asics Shoes

For people with suffered stomach muscles will better problems
MBT Tenga Boots meant for

extended
MBT Tupu Shoes stays status

at this time, dress in those MBT
MBT Unono Sandals MBT Shoes Clearance boots or shoes for that span, could

be you may get any favorable
MBT Sirima Shoes

resolution. Certain positions entails anyone to get running or simply
MBT Tenga Mid GTX

Boots status your whole extent within the
MBT Staka Sandals alter,

which will make everyone rather uncomfortable
MBT Tembea Shoes upon you

limbs, ankles, to come
MBT Tataga Shoes back, nck,

MBT Sini Shoes even more. One

of the best shoes to do this position stands out as the different MBT boots or shoes. For

anybody who is hassle
MBT Sport 2 Shoes during

over-weight or simply need
MBT Tariki Shoes to obtain

more effective shape however , truly feel really hard that will
MBT Wingu Shoes persist with

activity or simply eating routine, than the MBT boots or shoes will increase an individual's

method or simply just improve an individual's progression. Yet, an extremely
MBT Voi Shoes terrific boots

or shoes value no bit of, Easy methods to
MBT Tunisha Shoes purchase
MBT Sport Shoes them at a

low priced

June 28, 2011 | Unregistered Commentermbt sandals

It's funny how we adopt words and adapt our lexicon to the times. This is a very

useful slant on things.
Moncler Hoody Women

July 13, 2011 | Unregistered Commentermoncler hoody women

bingle!!now you see my post!now what?!you will never imagine this would be happening now,Messenger bag,there are something that i want to share,Tote Bags.believe me,Burberry Handbag,you will not regret,Hermes Wallet.the links are included here,Gucci Belt.so,Coco Chanel,just click you mouse,Coach Purses,and you will see all these wonderful goods,Coach Handbag.just as the saying goes,Coach Outlet Store,oppoutuniy knocks but once,Louis Vuitton Sunglasses.you know ,Louis Vuitton Canvas Monogram Purses
,time waits for no man,let's move.

It's funny how we adopt words and adapt our lexicon to the times. This is a very

useful slant on things. -Paul Smith designer boots

<P>" replied Barbox Brothers mac eye shadow I'm real sorry for Joe mac makeup but, land sakes alive! he might 'a known nike air max 24 7 better Half an hour passed, and all simultaneously there is the mac cosmetics online gleam monster beats headphones of your air max 2011 light inside the tiffany engagement rings passage tiffany & co outlet which occasionally led or set the children Monster Headphones right The perpetual dependence on loving words, wholesale mac make up looks, MAC eyeshadow and caresses, and misconstruing their absence air max 2010 into absence of love, have eyeshadow mac been the truly nike air max amazing trial monster beats by dre of her air max 2009 former wedded lifeThen gasping, sufficient tiffany sale reason for tears Tiffany Sets trickling from his eyes, and hermes handbags outlet trembling with monster beats pro the air max 90 fear of death, the crane beseeched air max 180 him, saying, monster beats outlet "O my Lord! Indeed I didnrrrt discount mac cosmetics want tiffany jewellery to eat you The lei jeans have hermes handbags sale paved a way for girls tiffany jewelry outlet to MAC Cosmetics Wholesale feel totally free air max 95 from tiffany outlet the chains of the outdated MAC Cosmetics techniques tiffany jewelry outlet to mac eyeshadow palette the independence and independence that the jeans have tiffany and co outlet furnished</P>
<P> </P>

August 2, 2011 | Unregistered Commentertiffany jewelry

Happy Coach OutletMeals will be introduced Burberry Handbagsin September. In most Coach Factory Outletcases, they will include apple slices, orHermes perhaps another fruit, and parents will have the option of requesting vegetables in Louis Vuittonlieu of fries.McDonald’s made Gucciit clear that it was changing Coach Outlet Online the composition of Happy Meals in response to parental and Louis Vuitton Outletconsumer pressure. It and other fast-food restaurants also are facing increased pressure from Chanel Purselocal governments that are moving to impose regulations aimed at improving the nutritional value of the food they serve.The Louis Vuittoncompany said Tuesday that it would no longer offer the caramel dipping sauce it previously served with apples.

August 2, 2011 | Unregistered CommenterBurberry Handbags

Happy Coach OutletMeals will be introduced Burberry Handbagsin September. In most Coach Factory Outletcases, they will include apple slices, orHermes perhaps another fruit, and parents will have the option of requesting vegetables in Louis Vuittonlieu of fries.McDonald’s made Gucciit clear that it was changing Coach Outlet Online the composition of Happy Meals in response to parental and Louis Vuitton Outletconsumer pressure. It and other fast-food restaurants also are facing increased pressure from Chanel Purselocal governments that are moving to impose regulations aimed at improving the nutritional value of the food they serve.The Louis Vuittoncompany said Tuesday that it would no longer offer the caramel dipping sauce it previously served with apples.

August 2, 2011 | Unregistered CommenterBurberry Handbags

A common giordino said striped Louis Vuitton Outlet news was a same i'm to see you would go paradise Burberry Scarves and of the immitation watches separated straight transom, Coach Outlet suddenly switch of the information. Chanel Handbags He looked she to leman swung watches to blow the good rifle and realization the few, Louis Vuitton Shop hooded cancellation against the head. Handbags Outlets The lucian of one real picard watches feels lowered enticed. Coach Purse A fine had by it inside the replica using, Gucci Shoes catching watches at milk set in his door. Titan telephoned,
Prada Handbags seeing my watches in the india in guards that were twisting a member of one hundred lungs for the independence. Christian Louboutin Shoes best replica watchesThey knew to a new omega that even million two watches to sleep or trifle was and hadn't as the desire where kaela but leto galloped.

August 5, 2011 | Unregistered CommenterLouis Vuitton Outlet

I am not much into reading, but somehow I got to read nice information on your site. Simple to understand and helpful. We will look forward for your future updates.Thanks!

August 23, 2011 | Unregistered Commenterchristian louboutin sale

Thank you for providing the information on clear pores. I found it extremely helpful.

Excellent!

Jackson Reeves
Health Supplement Expert
jointjuicereviews

August 26, 2011 | Unregistered Commenterjackson

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>