Wednesday 31 January 2018

Update Contact Entity From Website to CRM using IOraganization Service in Dynamic CRM.


Update Contact Entity From Website to  CRM using IOraganization Service in Dynamic CRM.

Description:

In this example we explain that how to update the contact entity in Dynamic CRM using plugin or custom Workflow. Or how to update the Contact fields based on the capture contact from website to CRM.

Here we demonstrate that how to create or update contact or capture contact from website to Dynamic CRM using Web Service or IOrganization Service in Asp.Net.if the entered email id or FirstName and LastName already Exists in Dynamic CRM then we will update the Contact otherwise insert the new Contact in Dynamic CRM.
Code:

   protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
         
            //Authenticate using credentials of the logged in user;      

            ClientCredentials Credentials = new ClientCredentials();
           

             Uri OrganizationUri = new Uri("http://<SERVERURL>/<ORGNAME>/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);
            IOrganizationService service = (IOrganizationService)serviceProxy;
            QueryExpression ContactlList = new QueryExpression("contact");
            ContactlList.Criteria.AddCondition(

              "emailaddress1",
               ConditionOperator.Equal,
               txtEmailAddress.Text
           );
             ContactlList.Criteria.AddCondition(

              "fullname",
               ConditionOperator.Equal,
               txtFirstName.Text+" "+txtLastName.Text
           );
           
            var contactDetail = service.RetrieveMultiple(ContactlList);
            if (contactDetail.Entities.Count > 0)
            {
                List<Entity> Collection = contactDetail.Entities.ToList();
                for (int i = 0; i < contactDetail.Entities.Count; i++)
                {
                    Entity entity = Collection[i];
                                        if (entity.LogicalName == "contact")
                    {
                        entity["new_subscibed"] = chksubscribe.Checked;
                        service.Update(entity);

                    }
                }
            }
            Else
{
//Insert contact code here           
               
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error Occured while Creating Contact.')", true);
        }


    }



0 comments:

Post a Comment