public ActionResult AddProductToCart(Guid productid, int quantity)
        {
            var          visitorID = HttpContext.Profile.UserName;
            ShoppingCart cart;
            var          portal             = PortalCrmConfigurationManager.CreatePortalContext();
            var          context            = portal.ServiceContext;
            var          product            = context.CreateQuery("product").FirstOrDefault(p => p.GetAttributeValue <Guid>("productid") == productid);
            var          productDataAdapter = new ProductDataAdapter(product, new Adxstudio.Xrm.Products.PortalContextDataAdapterDependencies(portal, null, Request.RequestContext));

            // Check for an existing cart

            var shoppingCartDataAdapter = new ShoppingCartDataAdapter(new PortalConfigurationDataAdapterDependencies(null, Request.RequestContext), visitorID);

            var existingShoppingCart = shoppingCartDataAdapter.SelectCart();

            // Create cart if it does not already exist.

            if (existingShoppingCart == null || existingShoppingCart.Entity == null)
            {
                cart = shoppingCartDataAdapter.CreateCart() as ShoppingCart;
            }
            else
            {
                cart = existingShoppingCart as ShoppingCart;
            }

            // Add product to cart and redirect to shopping cart page

            if (cart != null)
            {
                cart.AddProductToCart(productid, productDataAdapter.Select().PricingInfo.PriceListName, quantity);
            }

            return(RedirectToShoppingCart());
        }
        public ActionResult CreateReview(string productid, string title, string content, double rating,
                                         int maximumRatingValue, string reviewerName, string reviewerLocation, string reviewerEmail, bool recommend)
        {
            Guid productID;

            if (string.IsNullOrEmpty(productid) || !Guid.TryParse(productid, out productID))
            {
                throw new ArgumentException("Please provide a valid product ID.");
            }
            var portal             = PortalCrmConfigurationManager.CreatePortalContext();
            var context            = new OrganizationServiceContext(new OrganizationService("Xrm"));
            var product            = GetActiveProduct(productID, context);
            var productDataAdapter = new ProductDataAdapter(product, new Adxstudio.Xrm.Products.PortalContextDataAdapterDependencies(portal, null, Request.RequestContext));

            TryCreateReview(productDataAdapter, title, content, rating, maximumRatingValue, reviewerName, reviewerLocation, reviewerEmail, recommend);
            return(PartialView("CreateReview", productDataAdapter.Select()));
        }
        private ActionResult GetProductView(Entity product)
        {
            var context = PortalCrmConfigurationManager.CreatePortalContext();

            var productDataAdapter = new ProductDataAdapter(product.ToEntityReference(), new Adxstudio.Xrm.Products.PortalContextDataAdapterDependencies(context, null, Request.RequestContext));

            var imageGalleryNodes = new List <IProductImageGalleryNode>(productDataAdapter.SelectImageGalleryNodes());
            var iproduct          = productDataAdapter.Select();

            var productViewModel = new ProductViewModel
            {
                Product           = iproduct,
                ImageGalleryNodes = new ProductImageGalleryViewModel {
                    ImageGalleryNodes = imageGalleryNodes, Product = iproduct
                },
                UserHasReviewed = HttpContext.Request.IsAuthenticated ? productDataAdapter.HasReview(context.User.ToEntityReference()) : productDataAdapter.HasReview(HttpContext.Request.AnonymousID)
            };

            return(View("Product", productViewModel));
        }