web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to fetch intercept table data and display using c#

(0) ShareShare
ReportReport
Posted on by

Hi Guys,

How to get the records for each active contact which has a field called job title. And display that using C#

I tried this:

RetrieveMultipleRequest RM = new RetrieveMultipleRequest()
{
Query = new FetchExpression(linkFetch.ToString())
};
EntityCollection entityResults = ((RetrieveMultipleResponse)_service.Execute(RM)).EntityCollection;
foreach (var entity in entityResults.Entities)
{
String output = String.Empty;
if (entity.Attributes.Contains("job title"))
MessageBox.Show("" + entity.Attributes["job title"]);
}

I wanted to display job title for each active contact .. please help me 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi Suchisuchi,

    You can retrieve this information by using queries. I have attached articles that teach you how to work with a range of different queries.

  • Suggested answer
    Inogic Profile Picture
    715 on at

    Please try below code:

    1. Using simple retrieve

      /// <summary>

            /// retrieve data for field "XYZ" for record

            /// "new_xyz" is the Logical name of the XYZ field on the contact form

            /// </summary>

            public void retrieveContactDetailsforSingleRecord()

            {

                try

                {

                    Entity entContact = _service.Retrieve("contact", new Guid("recordid"), new ColumnSet("new_xyz"));

                    if (entContact.Attributes.Contains("new_xyz"))

                    {

                        string xyzValue = entContact["new_xyz"].ToString();

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception(ex.Message);

                }

            }

     

    2. Using Retrieve multiple

     

            /// <summary>

            /// Retrieve Contact Details for Mutliple Records using Fetch XML

            /// </summary>

            public void retrieveContactDetailsUsingFetchXML()

            {

                try

                {

                    string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                      <entity name='contact'>

                        <attribute name='new_xyz' />

                        <order attribute='new_xyz' descending='false' />

                        <filter type='and'>

                          <condition attribute='new_xyz' operator='not-null' />

                        </filter>

                      </entity>

                    </fetch>";

                    EntityCollection entContactColl = _service.RetrieveMultiple(new FetchExpression(fetchXml));

     

                    if (entContactColl != null && entContactColl.Entities.Count > 0)

                    {

                        foreach (Entity entContact in entContactColl.Entities)

                        {

                            if (entContact.Attributes.Contains("new_xyz"))

                            {

                                string xyzValue = entContact["new_xyz"].ToString();

                            }

                        }

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception(ex.Message);

                }

            }

     

    3. Using Linq: 

     

          /// <summary>

            /// Retrieve Contact Details Using Linq

            /// </summary>

            public void retrieveContactDetailsUsingLinq()

            {

                try

                {

                    var contactDetails = from contactEntity in _context.CreateQuery("contact")

                                         select new

                                         {

                                             xyzValue = contactEntity.Attributes.Contains("new_xyz") ? (string)contactEntity["new_xyz"] : string.Empty,

                                         };

     

                    foreach (var recordsContact in contactDetails)

                    {

                        if (recordsContact.xyzValue != null)

                        {

                            string XYZ = recordsContact.xyzValue;

                        }

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception(ex.Message);

                }

            }

     

    4. Using Query Expression

          

      /// <summary>

            /// Retrieve Contact Details Using QueryExp

            /// </summary>

            public void retrieveContactDetailsUsingQueryExp()

            {

                try

                {

                    QueryExpression getAllRecords = new QueryExpression();

                    getAllRecords.EntityName = "contact";

                    getAllRecords.ColumnSet = new ColumnSet("new_xyz");

                    ConditionExpression con = new ConditionExpression("new_xyz", ConditionOperator.ContainValues);

     

                    EntityCollection entContactColl = _service.RetrieveMultiple(getAllRecords);

     

                    if (entContactColl != null && entContactColl.Entities.Count > 0)

                    {

                        foreach (Entity entContact in entContactColl.Entities)

                        {

                            if (entContact.Attributes.Contains("new_xyz"))

                            {

                                string xyzValue = entContact["new_xyz"].ToString();

                            }

                        }

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception(ex.Message);

                }

            }

     

    Hope this helps.

    Thanks!

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    Just to add to everyone elses answers, just be careful that you use the correct attribute names.

    Your job title attribute has a space between job and title in your code.

    It should be "jobtitle" and not "job title"

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hello Inogic, I want to use the fetchXml method here, in order to use a messagebox.show to display Lstring xyzValue = entContact["new_xyz"].ToString();]

    Can you please help me how to do that... kindly.

  • Verified answer
    Inogic Profile Picture
    715 on at

    Please find the solution below to display the value in Message box:

                        foreach (Entity entContact in entContactColl.Entities)

                        {

                            if (entContact.Attributes.Contains("new_xyz"))

                            {

                                string xyzValue = entContact["new_xyz"].ToString();

                                MessageBox.Show("Value: " + xyzValue);

                            }

                        }

    If this answered your question, please make sure to Mark this as an Answer.

    Thanks!

  • Community Member Profile Picture
    on at

    string getcontactrecords = string.Format(@"<fetch version='1.0' output-format='xml-platform'
    mapping='logical' distinct='false'>
    <entity
    name='XYZ'>
    <attribute name='contactid' />
    <filter type='and'>
    <condition attribute='contactid' operator='eq'
    value='{0}' />
    </filter>
    <link-entity name='aac_abc'
    from='aac_XYZ' to='aac_abc' type='outer' alias='ao'>
    <attribute name='aac_name' />
    </link-entity>
    </entity>
    </fetch>", contactid.ToString());

    EntityCollection records = _service.RetrieveMultiple(new FetchExpression(getcontactrecords));
    if (records != null && records.Entities.Count > 0)
    {
    foreach(Entity entcontact in records.Entities)
    {
    if(entcontact.Attributes.Contains("contactid"))
    {
    string contactid = entcontact["contactid"].ToString();

    }
    }
    }

    But it is throwing NullReference exception, Can you please help...

    This is what i want it should get into each contact and find the field as shown in the picture and display the roles attached to field if at all the field is not blank, as am new to crm, i am unable to make anyone understand so i took a paint file to explain my need. Till now my code can fetch all the guid of contact entity but after that how to fetch the details of that fields in the picture, is my doubt, shall be helpful if you help.6724.Untitled.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Shidin Haridas Profile Picture

Shidin Haridas 2

#1
SA-08121319-0 Profile Picture

SA-08121319-0 2

#1
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans