Dynamics 365





Friday, November 20, 2015

delete crm record using c#

 Delete CRM record using c#.


 protected void HCIF_Click(object sender, EventArgs e)
        {

            string connectionInfo = "http://crmXXXXXXX/XXXXXXXXXs/XRMServices/2011/Organization.svc";
            string userName = "crmadmin";
            string passWord = "XXXXXX";
            string domain = "APPS";

            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = new System.Net.NetworkCredential(userName, passWord, domain);

            //System.Net.CredentialCache.DefaultNetworkCredentials;
            Uri homeRealUri = null;
            Uri organizationUri = new Uri(connectionInfo);
            //This ServiceUri will be taken from web.config later.
            //ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(CustomXertificateValidation);
            using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealUri, credentials, null))
            {
                try
                {
                    string HCIF = TextBox1.Text.ToString();
                    //_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                    //_serviceProxy.EnableProxyTypes();

                    IOrganizationService _service = (IOrganizationService)_serviceProxy;
                    _serviceProxy.Timeout = new TimeSpan(24, 60, 18);

                    OrganizationRequest req = new OrganizationRequest();

                 
                    //StringWriter stringWriterRes = new StringWriter();
                    //XmlSerializer serializerRes = new XmlSerializer(typeof(Entity));
                    //serializerRes.Serialize(stringWriterRes, _service);

                    string fetchXml =
              @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' >
                        <entity name='new_cif'>
                           <attribute name='new_cifno' alias='HCIF' />
                            <filter type='and'>
                            <condition attribute='new_cifno' operator='eq' value='{0}' />
                            </filter>
                        </entity>
                      </fetch>";

                
//  fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
//                                <entity name='new_cif'>
//                                <attribute name='new_cifno' />                            
//                                <filter type='and'>
//                                    <condition attribute='new_cifno' operator='eq' value='{0}' />
//                                </filter>
//                                </entity>
//                            </fetch>";
             

                string formatXml = string.Format(fetchXml, HCIF);
               FetchExpression fetchexp = new FetchExpression(formatXml);
                // Fetch the results.
               var EntHCIF = _serviceProxy.RetrieveMultiple(fetchexp);


               if (EntHCIF.Entities.Count > 0)
               {
                   bool isDel = false;

                   foreach (var ent in EntHCIF.Entities)
                   {
                       _serviceProxy.Delete(ent.LogicalName, ent.Id);
                       isDel = true;
                   }
                   if (isDel)
                       Label1.Text = "Deleted HCIF Successfully.. with HCIF:" + HCIF + "...nunmber of HCIFs found.." + EntHCIF.Entities.Count;
               }
               else
                   Label1.Text = "HCIF NOT FOUND..";

            

                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    Label1.Text = err;
                }
            }
        }​