<commit_before>﻿using System;
using System.Data;
using System.Management.Automation;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Layouts;

namespace Cognifide.PowerShell.PowerShellIntegrations.Commandlets.Presentation
{
    [Cmdlet(VerbsCommon.Get, "Layout")]
    [OutputType(new[] {typeof (Item)})]
    public class GetLayoutCommand : BaseLanguageAgnosticItemCommand
    {
        [Parameter]
        public DeviceItem Device { get; set; }

        protected override void ProcessItem(Item item)
        {
            LayoutField layoutField = item.Fields[FieldIDs.LayoutField];
            if (layoutField == null || string.IsNullOrEmpty(layoutField.Value))
            {
                return;
            }

            LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);

            if (layout.Devices == null)
            {
                return;
            }

            foreach (DeviceDefinition device in layout.Devices)
            {
                if (Device == null || string.Equals(device.ID, Device.ID.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    Item layoutItem = item.Database.GetItem(device.Layout);
                    WriteItem(layoutItem);
                    if (Device != null)
                    {
                        return;
                    }
                }
            }
            if (Device != null)
            {
                WriteError(
                    new ErrorRecord(
                        new ObjectNotFoundException(
                            string.Format("Item \"{0}\" has no layout defined for device \"{1}\"", item.Name,
                                Device.Name)), "sitecore_layout_for_device_not_found", ErrorCategory.ObjectNotFound,
                        Device));
            }
        }<commit_msg>Issue #233 This will now work: Get-Item master:\content\Home | Get-Layout | Format-Table Name, ID , Device, DeviceID<commit_after>﻿using System;
using System.Data;
using System.Management.Automation;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Layouts;

namespace Cognifide.PowerShell.PowerShellIntegrations.Commandlets.Presentation
{
    [Cmdlet(VerbsCommon.Get, "Layout")]
    [OutputType(new[] {typeof (Item)})]
    public class GetLayoutCommand : BaseLanguageAgnosticItemCommand
    {
        [Parameter]
        public DeviceItem Device { get; set; }

        protected override void ProcessItem(Item item)
        {
            LayoutField layoutField = item.Fields[FieldIDs.LayoutField];
            if (layoutField == null || string.IsNullOrEmpty(layoutField.Value))
            {
                return;
            }

            LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);

            if (layout.Devices == null)
            {
                return;
            }

            foreach (DeviceDefinition device in layout.Devices)
            {
                if (Device == null || string.Equals(device.ID, Device.ID.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    Item layoutItem = item.Database.GetItem(device.Layout);
                    PSObject psobj = ItemShellExtensions.GetPsObject(SessionState, layoutItem);
                    psobj.Properties.Add(new PSNoteProperty("DeviceID", device.ID));
                    DeviceItem deviceItem = Device ?? (DeviceItem)item.Database.GetItem(device.ID);
                    psobj.Properties.Add(new PSNoteProperty("Device", deviceItem.Name));
                    WriteObject(psobj);
                    if (Device != null)
                    {
                        return;
                    }
                }
            }
            if (Device != null)
            {
                WriteError(
                    new ErrorRecord(
                        new ObjectNotFoundException(
                            string.Format("Item \"{0}\" has no layout defined for device \"{1}\"", item.Name,
                                Device.Name)), "sitecore_layout_for_device_not_found", ErrorCategory.ObjectNotFound,
                        Device));
            }
        }
{'fraction_non_alphanumeric': 0.09197559831065227, 'fraction_numerical': 0.001642421398404505, 'mean_word_length': 1.6593886462882097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6889551', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1009, 'n_words': 215}