Share
Go down
avatar
Admin
Admin
Tổng số bài gửi : 389
Points : 704
Reputation : 0
Join date : 11/10/2018
https://thanthan.forumvi.com

Delete record by Console Empty Delete record by Console

on Mon Oct 29, 2018 9:50 am
Code:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using Microsoft.Xrm.Tooling.Connector;
using System.Configuration;
using System.ServiceModel;

namespace Console_Delete_Record
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                CrmServiceClient _client = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
                CrmServiceClient conn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);
                IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                deleteRecord(service, getIdRecord(service), /*entityName*/);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                string message = ex.Message;
                throw;
            }
        }

        private static void deleteRecord(IOrganizationService service, EntityCollection list, string nameEntity)
        {
            if (list.Entities.Count > 0)
            {
                int i = 1;
                foreach (var item in list.Entities)
                {
                    Guid Id = item.Id;
                    service.Delete(nameEntity, Id);
                    Console.WriteLine(i);
                    i++;
                }
            }
        }

        public static EntityCollection getIdRecord(IOrganizationService service)
        {
            var fetchXml = $@"
                            ...";

            return service.RetrieveMultiple(new FetchExpression(fetchXml));
        }
    }
}
Về Đầu Trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết