Tuesday, June 7, 2011

Executing FetchXML in CRM 2011

Hi friends, in crm 2011 service don't have fetch method to execute fetch XML query. we can execute fetch xml query using RetrieveMultiple method of service. Here i'm going to retrieve system user's team details using fetchxml.

string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                                    <entity name='team'>
                                    <attribute name='name' />
                                    <attribute name='businessunitid' />
                                    <attribute name='teamid' />
                                    <order attribute='name' descending='false' />
                                    <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>
                                        <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='user'>
                                        <filter type='and'>
                                            <condition attribute='systemuserid' operator='eq' value='{0}' />
                                        </filter>
                                        </link-entity>
                                    </link-entity>
                                    </entity>
                                </fetch>";

                 //  pass user guid as parameter
                string formatXml = string.Format(fetchXml, userid.ToString());

                // Executing fetchxml using  RetrieveMultiple method
                EntityCollection entities = service.RetrieveMultiple(new FetchExpression(formatXml));

                foreach (Entity e in entities.Entities)
                {
                   Guid Id = new Guid(e.Attributes["teamid"].ToString());
                   string TeamName = e.Attributes["name"].ToString();
                }

3 comments:

  1. Hi Prasad,
    Thanks for valuable information .

    I am not getting result of 'display-fetch-in-iframe' in MS CRM 2011.Howevere its working with CRM 4.0.

    Its showing error on 'fetchSubmittedRecords.RegisterOnTab(2);
    Can you please help me out.

    Regrads,
    Yusuf

    ReplyDelete
  2. Hi Yusuf,

    I just added my working copy of it on my blog, you can find it here,
    http://guruprasadcrm.blogspot.com/2011/12/display-fetchxml-in-iframe-in-crm-2011.html

    ReplyDelete
  3. Hi Prasad,

    i have used this fetch xml code in SSRS report. It is very helful .. thanks

    Regards
    Bhikshalu

    ReplyDelete