Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

To Get The Contact Information By Just Passing Email Address

Posted on by 932

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

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: To Get The Contact Information By Just Passing Email Address

    Hi Shakti,

    Can you share more details on what exactly you mean by "create multiple record by passing attribute names"? Are you tryng to create multiple recors with same values or multiple records with same field but with different values. Either way the code would be similar to which I have shared above. Have you tried anything yet at your end?

  • RE: To Get The Contact Information By Just Passing Email Address

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

  • RE: To Get The Contact Information By Just Passing Email Address

    Thanks Ravi,

    What about create multiple record by passing attribute names?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: To Get The Contact Information By Just Passing Email Address

    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.

  • RE: To Get The Contact Information By Just Passing Email Address

    Thanks for help Ravi And Goutam,

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

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: To Get The Contact Information By Just Passing Email Address

    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;
                }
    
            }


  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: To Get The Contact Information By Just Passing Email Address

    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);

  • RE: To Get The Contact Information By Just Passing Email Address

    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;

    }

    }

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: To Get The Contact Information By Just Passing Email Address

    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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: To Get The Contact Information By Just Passing Email Address

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans