Beispiel #1
0
        private void SetAmp()
        {
            DeviceRecords devices = Sitecore.Context.Item?.Database.Resources.Devices;

            if (devices != null)
            {
                DeviceItem defaultDevice = devices?.GetAll()?.Where(d => d.Name.ToLower() == "amp").FirstOrDefault();
                if (defaultDevice != null)
                {
                    Sitecore.Context.Device = defaultDevice;
                }
            }
        }
Beispiel #2
0
 // check to see if item is a page item (has layout)
 public static bool IsPageItem(Item item)
 {
     if (item != null)
     {
         DeviceRecords devices       = item.Database.Resources.Devices;
         DeviceItem    defaultDevice = devices.GetAll().Where(d => d.Name.ToLower() == "default").First();
         LayoutItem    layout        = item.Visualization.GetLayout(defaultDevice);
         if (layout == null)
         {
             return(false);
         }                                     // item does not have page layout
         else
         {
             return(true);
         }                     // item has page layout
     }
     return(false);
 }
        private string GetVisitedItemsRenderings()
        {
            var           visitedItems        = _databaseReader.GetMostVisitedItems(database: database);
            List <string> renderingStringList = new List <string>();

            foreach (KeyValuePair <Guid, int> visitedItem in visitedItems)
            {
                Item item = database.GetItem(ID.Parse(visitedItem.Key));
                if (item != null)
                {
                    DeviceRecords        devices       = database.Resources.Devices;
                    DeviceItem           defaultDevice = devices.GetAll().First(d => d.Name.ToLower() == "default");
                    RenderingReference[] renderings    = item.Visualization.GetRenderings(defaultDevice, true);
                    foreach (var rendering in renderings)
                    {
                        string renderingId = rendering.RenderingID.ToString();
                        renderingStringList.Add(renderingId);
                    }
                }
            }
            return(String.Join("|", renderingStringList.Distinct()));
        }
Beispiel #4
0
        protected void ProcessLayoutDefinition(List <RecommendedRenderingModel> result, Item item)
        {
            using (new SecurityStateSwitcher(SecurityState.Disabled))
            {
#if (SC72 || SC75)
                Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__Renderings"];
#else
                Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__Final Renderings"];
#endif

                if (layoutField == null)
                {
                    return;
                }

                // Get all devices
                DeviceRecords devices = item.Database.Resources.Devices;
                foreach (var deviceRecord in devices.GetAll())
                {
                    // Get the rendering references for the default device
                    Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(deviceRecord);
                    if (renderings == null)
                    {
                        continue;
                    }

                    foreach (var renderingReference in renderings)
                    {
                        result.Add(new RecommendedRenderingModel
                        {
                            Placeholder = renderingReference.Placeholder,
                            RenderingID = renderingReference.RenderingID.ToShortID().ToString()
                        });
                    }
                }
            }
        }
Beispiel #5
0
        public static void AddRenderings(Item parentItem)
        {
            if (parentItem == null)
            {
                return;
            }

            if (parentItem.Children.Count == 0)
            {
                return;
            }
            else
            {
                localDatasource = parentItem.Children.FirstOrDefault();
            }

            if (localDatasource.Children.Count == 0)
            {
                return;
            }

            ResetRenderings(parentItem);

            // Get the default device
            DeviceRecords devices       = parentItem.Database.Resources.Devices;
            DeviceItem    defaultDevice = devices.GetAll().Where(d => d.Name.ToLower() == "default").First();

            var renderingsList = parentItem.Visualization.GetRenderings(defaultDevice, true);

            if (renderingsList == null)
            {
                return;
            }

            LayoutField      layoutField      = new LayoutField(parentItem.Fields[FieldIDs.FinalLayoutField]);
            LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
            DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(defaultDevice.ID.ToString());

            foreach (Item child in localDatasource.Children)
            {
                if (child.TemplateID.ToString() == NewsChild.Text)
                {
                    Guid   newGuid           = Guid.NewGuid();
                    string renderingUniqueID = newGuid.ToString().TrimStart('{').TrimEnd('}').ToLower();

                    if (layoutDefinition.Devices.Count > 0)
                    {
                        if (!RenderingAvailable(layoutDefinition, Renderings.BodyContent_Rendering))
                        {
                            RenderingDefinition textSubLayout = new RenderingDefinition();

                            textSubLayout.ItemID      = Renderings.BodyContent_Rendering;
                            textSubLayout.Placeholder = "page-layout";
                            textSubLayout.UniqueId    = renderingUniqueID;
                            deviceDefinition.AddRendering(textSubLayout);
                        }
                        else
                        {
                            var rendering = deviceDefinition.GetRendering(Renderings.BodyContent_Rendering);
                            renderingUniqueID = rendering.UniqueId;
                            renderingUniqueID = renderingUniqueID.TrimStart('{').TrimEnd('}').ToLower();
                        }
                    }

                    RenderingDefinition newRenderingDefinition = new RenderingDefinition();
                    newRenderingDefinition.ItemID = Renderings.Text;
                    string placeholderValue = !string.IsNullOrEmpty(renderingUniqueID) ? "/page-layout/body-content_" + renderingUniqueID : "/page-layout/body-content";
                    newRenderingDefinition.Placeholder = placeholderValue;
                    newRenderingDefinition.Datasource  = child.ID.ToString();

                    deviceDefinition.AddRendering(newRenderingDefinition);
                }
                else if (child.TemplateID.ToString() == NewsChild.Quote)
                {
                    Guid   newGuid           = Guid.NewGuid();
                    string renderingUniqueID = newGuid.ToString().TrimStart('{').TrimEnd('}').ToLower();

                    if (layoutDefinition.Devices.Count > 0)
                    {
                        if (!RenderingAvailable(layoutDefinition, Renderings.BodyContent_Rendering))
                        {
                            RenderingDefinition quoteSubLayout = new RenderingDefinition();
                            quoteSubLayout.ItemID      = Renderings.BodyContent_Rendering;
                            quoteSubLayout.Placeholder = "page-layout";
                            quoteSubLayout.UniqueId    = renderingUniqueID;

                            deviceDefinition.AddRendering(quoteSubLayout);
                        }
                        else
                        {
                            var rendering = deviceDefinition.GetRendering(Renderings.BodyContent_Rendering);
                            renderingUniqueID = rendering.UniqueId;
                            renderingUniqueID = renderingUniqueID.TrimStart('{').TrimEnd('}').ToLower();
                        }
                    }

                    RenderingDefinition newRenderingDefinition = new RenderingDefinition();
                    newRenderingDefinition.ItemID = Renderings.Quote;

                    string placeholderValue = !string.IsNullOrEmpty(renderingUniqueID) ? "/page-layout/body-content_" + renderingUniqueID : "/page-layout/body-content";

                    newRenderingDefinition.Placeholder = placeholderValue;
                    newRenderingDefinition.Datasource  = child.ID.ToString();

                    deviceDefinition.AddRendering(newRenderingDefinition);
                }
            }

            using (new SecurityDisabler())
            {
                parentItem.Editing.BeginEdit();
                if (parentItem.Editing.IsEditing)
                {
                    parentItem["__Renderings"] = layoutDefinition.ToXml();
                }
                parentItem.Editing.EndEdit();
            }
        }