public ActionResult AddDeal()
        {
            // deals root id '/sitecore/content/home/deals'
            string dealParentID = "{8069B844-1D05-400A-A40A-26412345DDF3}";

            // get deals count
            int dealCount = GetDealCount();

            #region Get parent item
            // Get Parent Item
            var options = new GetItemByIdOptions(new Guid(dealParentID))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var parentItem = _sitecoreService.GetItem <BaseDeal>(options);
            #endregion

            #region  Create a new deal
            var baseDeal = new BaseDeal()
            {
                Name          = $"Newdeal-{dealCount + 1}",
                StartPrice    = dealCount + 1000,
                PriceIncludes = "Price includes everything",
                HeaderImage   = new Glass.Mapper.Sc.Fields.Image()
                {
                    MediaId = new Guid("{CD431039-D155-42A3-89A3-B97621775075}")
                }
            };

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                var newdeal = _masterSitecoreService.CreateItem <BaseDeal>(parentItem, baseDeal);

                // publish newly created item.
                PublishItem(newdeal.Id);
            }

            #endregion

            return(Redirect("/deals"));
        }
        public ActionResult DemoCarousel()
        {
            // example for lazy loading & SitecoreService:
            Database         masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
            ISitecoreService service  = new SitecoreService(masterDb);

            Carousel target  = service.GetItem <Carousel>("/sitecore/content/Home/New MVC Page/Assets/DemoCarousel", x => x.LazyDisabled());
            var      options = new GetItemByIdOptions(System.Guid.NewGuid())
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target2 = service.GetItem <Carousel>(options);
            // example end



            var dataSource = _mvcContext.GetDataSourceItem <Carousel>();

            var viewModel = new CarouselViewModel
            {
                Title = dataSource.Title
            };

            for (var i = 0; i < dataSource.Items.Count(); i++)
            {
                var item = dataSource.Items.ElementAt(i);
                viewModel.Items.Add(new CarouselItemViewModel
                {
                    Index       = i,
                    ImageUrl    = item.Image?.Src,
                    ImageAlt    = item.Image?.Alt,
                    ShowCaption = !string.IsNullOrEmpty(item.Caption),
                    Caption     = item.Caption,
                    Class       = i == 0 ? "active" : string.Empty
                });
            }

            return(View(viewModel));
        }
 protected AbstractGetItemByIdBuilder(GetItemByIdOptions options)
     : base(options)
 {
     _options = options;
 }
Beispiel #4
0
 public Sitecore.Data.Items.Item GetItemById(GetItemByIdOptions options)
 {
     return(DB?.GetItem(ID.Parse(options.Id)));
 }
        public ActionResult Deals()
        {
            // product path
            string dealPath = "/sitecore/content/home/deals/london";
            // product Id
            string dealID = "{0CD5C7B6-D994-49BF-BB62-AC303D99A5D3}";
            // product name
            string dealName = "sydney";
            // query by product name
            string dealQuery = $"/sitecore/content/home/deals//*[contains(@@name,'{dealName.ToLower()}')]";
            // query type 1 to get multiple products
            string dealsPathType1 = "/sitecore/content/home/deals/*";
            // query type 2 to get multiple products
            string dealsPathType2 = "/sitecore/content/home/deals//*";

            BaseDeal target = _sitecoreService.GetItem <BaseDeal>(dealPath, x => x.LazyDisabled());

            #region Single Item

            #region  Get Item by Id
            //Example 1
            var options = new GetItemByIdOptions(new Guid(dealID))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target2 = _sitecoreService.GetItem <BaseDeal>(options);
            #endregion

            #region  Get Item by Path
            // Example 2
            var options1 = new GetItemByPathOptions(dealPath)
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target3 = _sitecoreService.GetItem <BaseDeal>(options1);
            #endregion

            #region  Get Item by Query
            // Example 3
            var options4 = new GetItemByQueryOptions(new Query(dealQuery))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target4 = _sitecoreService.GetItem <BaseDeal>(options4);
            #endregion

            #endregion

            #region Multiple Items

            #region  Get Item by Query
            //Example 1
            var options10 = new GetItemsByQueryOptions(new Query(dealsPathType1))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target10 = _sitecoreService.GetItems <BaseDeal>(options10);

            //Example 2
            var options11 = new GetItemsByQueryOptions(new Query(dealsPathType2))
            {
                InferType = true,
                Lazy      = Glass.Mapper.LazyLoading.Disabled
            };
            var target11 = _sitecoreService.GetItems <BaseDeal>(options11);

            #endregion

            #endregion

            return(View("~/Areas/basic/Views/Home/Deals.cshtml", target11));
        }