Skip to main content

Soap Entity Repository Class MS CRM 2016 Part 1

 Hi guys hope you all are good.
 Today I am writing a new article for the CRM developer and this will help the you alot.
 I have created Entity repository class which perform different opertion in crm. I will tell you how to call these method in my next article.

 public class SoapEntityRepository : Repository
    {
        private IOrganizationService _service;
        private OrganizationServiceProxy proxyService;

  // get CRM Service Object
        public IOrganizationService GetCRMService(string UserName,string Password, string Domain)
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password,Domain);
            Uri organizationUri = new Uri(this.CRMServie);

            proxyService = new OrganizationServiceProxy(organizationUri, null, credentials, null);
            _service = (IOrganizationService)proxyService;
            return _service;
        }
  // get Entity Collection through C#
        public EntityCollection GetEntityCollection(QueryExpression query)
        {
            try
            {
                proxyService.EnableProxyTypes();
                EntityCollection mzk_EntityCollection = _service.RetrieveMultiple(query);
                return mzk_EntityCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
              
  // Create Entity  through C#
        public Guid CreateEntity(Entity _entity)
        {
            try
            {
                proxyService.EnableProxyTypes();
                return _service.Create(_entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  // get Entity through C#
        public Entity GetEntity(string entityName, Guid Id, ColumnSet column)
        {
            try
            {
                proxyService.EnableProxyTypes();
                return _service.Retrieve(entityName, Id, column);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  // Update Entity through C#
        public void UpdateEntity(Entity entity)
        {
            try
            {
                _service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  // delete Entity through C#
        public void DeleteEntity(string entityName, Guid Id)
        {
            try
            {
                _service.Delete(entityName, Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  //get Optionset Items through C#
        public OptionMetadata[] GetOptionSetItems(string optionSetName)
        {
            try
            {
                OptionMetadata[] optionList = null;
                RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest
                {
                    Name = optionSetName
                };

                RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)_service.Execute(retrieveOptionSetRequest);
                OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
                // Get the current options list for the retrieved attribute.
                optionList = retrievedOptionSetMetadata.Options.ToArray();               

                return optionList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  //get Optionset Items of specific Entity through C#
        public OptionMetadata[] GetOptionSetItems(string entityName,string optionSetAttributeName)
        {
            try
            {
                OptionMetadata[] optionList = null;
                RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest
                {
                    EntityLogicalName = entityName,
                    LogicalName = optionSetAttributeName,
                    RetrieveAsIfPublished = true
                };
                RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)_service.Execute(retrieveAttributeRequest);
                if (retrieveAttributeResponse.AttributeMetadata.AttributeType == AttributeTypeCode.Picklist)
                {
                    PicklistAttributeMetadata retrievedPicklistAttributeMetadata = (PicklistAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;
                    optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
                }
                return optionList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
  }

Comments