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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

To Get The Contact Information By Just Passing Email Address

(0) ShareShare
ReportReport
Posted on by 958

I am using web api to get the data of my organisation.

I want to get the contact information by passing just email address of that contact.

How could i do the same using C#?

I am just want check that the contact record is present or not in my organization. If yes, then get all the fields.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Try below code:

    ==============

    public Entity SearchContactByEmail(IOrganizationService service, string emailAddress)

           {

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

                                           <entity name = 'contact' >

                                               <all-attributes/>

                                               <filter type='and'>

                                                   <condition attribute='emailaddress1' operator='eq' value='{0}' />

                                               </filter>

                                           </entity>

                                       </fetch>";

               fetchXml = string.Format(fetchXml, emailAddress);

               var results = service.RetrieveMultiple(new FetchExpression(fetchXml));

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

               {

                   return results[0];

               }

               else

               {

                   return null;

               }

           }

    ==================

    You can call this as below-

    =============

    var ContactRecord = SearchContactByEmail(service, "ravi@email.com");

                   if (ContactRecord != null)

                   {

                       Console.WriteLine("Contact Found: " + ContactRecord["fullname"]);

                   }

                   else

                   {

                       Console.WriteLine("Contact Not Found!");

                   }

    =============

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Shakti,

    You can follow Ravi's answer which is easiest , In addition if you want to go with web API please have  a look below reference -

    code.msdn.microsoft.com/CRM-Web-API-Basic-27fcbf2e

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Shakti,

    How did you get on with this? If you have got your answer, please close the thread by mark the suggestion as answer if it helped you

  • Shakti Singh Rajput Profile Picture
    958 on at

    I got an error in below code.

    Can you please let me know what is the error?

    public Entity SearchContactByEmail(IOrganizationService service, "admin@contoso.com")

    {

    string fetchXml = @"<fetch mapping='logical' output-format='xml-platform' version='1.0' distinct='false'>"
    + "<entity name='contact'>"
    + " <attribute name='fullname' />"
    + " <attribute name='telephone1' />"
    + " <attribute name='contactid' />"
    + " <attribute name='emailaddress1' />"
    + " <attribute name='createdby' />"
    + " <order descending='false' attribute='fullname' />"
    + " <filter type='and'>"
    + " <condition attribute='emailaddress1' operator='eq' value='admin@contoso.com'/>"
    + " </filter>"
    + " </entity>"
    + "</fetch>";

    fetchXml = string.Format(fetchXml, emailAddress);

    var results = service.RetrieveMultiple(new FetchExpression(fetchXml));

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

    {
    Console.WriteLine("Contact Found: " + ContactRecord["fullname"]);


    return results[0];

    }

    else

    {

    return null;

    }

    }

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Try to replace below line for dynamics value-

    + " <condition attribute='emailaddress1' operator='eq' value='{0}'/>"

    OR

    + " <condition attribute='emailaddress1' operator='eq' value='"+emailAddress+"'/>"

    If you put hard code value then you don't have to write below line-

    fetchXml = string.Format(fetchXml, emailAddress);

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    So there is two way - 

    Passing value Inline in the FetchXML -

         public Entity SearchContactByEmail(IOrganizationService service, string emailAddress)
            {
                string fetchXml = @"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>" +
                "<entity name='contact' >" +
                "<attribute name='fullname' />" +
                "<attribute name='telephone1' />" +
                "<attribute name='contactid' />" +
                "<attribute name='emailaddress1' />" +
                "<attribute name='createdby' />" +
                "<order attribute='fullname' descending='false' />" +
                "<filter type='and'>" +
                "<condition attribute='emailaddress1' operator='eq' value='"+ emailAddress + "'/>" +
                "</filter>" +
                "</entity>" +
                "</fetch>"; 
    EntityCollection results = null; results = service.RetrieveMultiple(new FetchExpression(fetchXml)); if (results != null && results.Entities.Count > 0) { Console.WriteLine("Contact Found: " + results.Entities[0].Attributes["fullname"].ToString()); } else { return null; } }


    Pass value using Format -

       public Entity SearchContactByEmail(IOrganizationService service, string emailAddress)
            {
                string fetchXml = @"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>" +
                "<entity name='contact' >" +
                "<attribute name='fullname' />" +
                "<attribute name='telephone1' />" +
                "<attribute name='contactid' />" +
                "<attribute name='emailaddress1' />" +
                "<attribute name='createdby' />" +
                "<order attribute='fullname' descending='false' />" +
                "<filter type='and'>" +
                "<condition attribute='emailaddress1' operator='eq' value='{0}'/>" +
                "</filter>" +
                "</entity>" +
                "</fetch>";
    
                fetchXml = string.Format(fetchXml, emailAddress);
                EntityCollection results = null;
                results = service.RetrieveMultiple(new FetchExpression(fetchXml));
                if (results != null && results.Entities.Count > 0)
                {
                    Console.WriteLine("Contact Found: " + results.Entities[0].Attributes["fullname"].ToString());
                }
                else
                {
                    return null;
                }
    
            }


  • Shakti Singh Rajput Profile Picture
    958 on at

    Thanks for help Ravi And Goutam,

    Is there any way to create/update/delete multiple account records using same?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    I am assuming by "Same" you mean C#, if yes then "Yes, there are ways to create/update/delete multiple accounts".

    You first need to use the code to retrieve the records you want to update/delete. Once , use a foreach loop for each record and update/delete accordingly. Below sample retrieves all contacts and then updates emailaddress to "test@test.com" for all retrieved contacts

    ===========

    using (service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null))

               {

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

                                          <entity name = 'contact' >

                                              <all-attributes/>

                                          </entity>

                                      </fetch>";

                   var allContact = service.RetrieveMultiple(new FetchExpression(fetchXml));

                   foreach (var contactRecord in allContact.Entities)

                   {                  

                       Entity contactUpd = new Entity(contactRecord.LogicalName, contactRecord.Id);

                       contactUpd["emailaddress1"] = "test@test.com";

                       service.Update(contactUpd);

                   }

               }

    ===============

    Hope this helps.

    Also, it is advisable/recommended to open a new thread/post for your new questions. This will help the suggestion on the thread only related to the main question which will help others looking for the same issue.

  • Shakti Singh Rajput Profile Picture
    958 on at

    Thanks Ravi,

    What about create multiple record by passing attribute names?

  • Shakti Singh Rajput Profile Picture
    958 on at

    And, also will keep in mind as u suggest for threads.

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

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
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans