public async Task <bool> removeUserFavourite(Products product) { SoapEntityRepository entityRepository = SoapEntityRepository.GetService(); xrm.mzk_userfavourite userFavouriteEntity = new xrm.mzk_userfavourite(); try { QueryExpression query = new QueryExpression(mzk_userfavourite.EntityLogicalName); query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true); if (!string.IsNullOrEmpty(product.FavouriteId)) { query.Criteria.AddCondition("mzk_userfavouriteid", ConditionOperator.Equal, product.FavouriteId); } EntityCollection entitycollection = entityRepository.GetEntityCollection(query); if (entitycollection.Entities.Count < 1) { return(false); } if (!string.IsNullOrEmpty(product.FavouriteId) && (!product.FavouriteId.Equals("null"))) { entityRepository.DeleteEntity(xrm.mzk_userfavourite.EntityLogicalName, new Guid(product.FavouriteId)); } return(true); } catch (Exception ex) { throw ex; } }
public async Task <string> AddUserFavourite(Products product) { SoapEntityRepository entityRepository = SoapEntityRepository.GetService(); xrm.mzk_userfavourite userFavouriteEntity = new xrm.mzk_userfavourite(); QueryExpression query = new QueryExpression(mzk_userfavourite.EntityLogicalName); query.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(true); if (!string.IsNullOrEmpty(product.UserId)) { query.Criteria.AddCondition("mzk_userid", ConditionOperator.Equal, product.UserId); query.Criteria.AddCondition("mzk_productid", ConditionOperator.Equal, product.ProductId); } EntityCollection entitycollection = entityRepository.GetEntityCollection(query); if (entitycollection.Entities.Count > 1) { throw new ValidationException("Favourite Already exist"); //FavouriteId = entitycollection.Entities[0].Attributes["mzk_userfavouriteid"].ToString(); // return FavouriteId.ToString(); } if (string.IsNullOrEmpty(FavouriteId)) { if (!string.IsNullOrEmpty(product.ProductId)) { userFavouriteEntity.mzk_ProductId = new Microsoft.Xrm.Sdk.EntityReference("product", new Guid(product.ProductId)); } if (!string.IsNullOrEmpty(product.UserId)) { userFavouriteEntity.mzk_UserId = new Microsoft.Xrm.Sdk.EntityReference("systemuser", new Guid(product.UserId)); } if (!string.IsNullOrEmpty(product.Type)) { userFavouriteEntity.mzk_ProductType = new Microsoft.Xrm.Sdk.OptionSetValue(Convert.ToInt32(product.Type)); } FavouriteId = Convert.ToString(entityRepository.CreateEntity(userFavouriteEntity)); } return(FavouriteId.ToString()); }