Ejemplo n.º 1
0
		public static void SetupSitecoreItems()
		{
			using(new Sitecore.SecurityModel.SecurityDisabler())
			{
				// Get databases
				Database database = Sitecore.Configuration.Factory.GetDatabase("master");
				if (database == null)
					throw new Exception("Failed to find master database");

				Item itemHome = database.GetItem(Constants.Paths.Home);
				if (itemHome == null)
					throw new Exception("Failed to find the home node");

				// Grab templates
				TemplateItem simpleTemplate = database.Templates[Constants.Paths.DocTemplate];
				if (simpleTemplate == null)
					throw new Exception("Failed to find document template");

				TemplateItem folderTemplate = database.Templates[Constants.Paths.FolderTemplate];
				if (folderTemplate == null)
					throw new Exception("Failed to find folder template");

				// Grab presentation
				LayoutItem documentLayout = database.Resources.Layouts[Constants.Paths.Layout];
				if (documentLayout == null)
					throw new Exception("Failed to find the document layout");

				DeviceItem defaultDevice = database.Resources.Devices["Default"];
				if(defaultDevice == null)
					throw new Exception("Failed to find the default device");

				RenderingItem itemRendering = database.Resources.Renderings[Constants.Paths.Rendering];
				if(itemRendering == null)
					throw new Exception("Failed to find the item rendering");

				// Get user items
				if(!Sitecore.Security.Accounts.User.Exists("sitecore\\a"))
					Sitecore.Security.Accounts.User.Create("sitecore\\a", "a");

				if (!Sitecore.Security.Accounts.User.Exists("sitecore\\b"))
					Sitecore.Security.Accounts.User.Create("sitecore\\b", "b");

				itemHome.Delete();

				Item content = database.GetItem(Constants.Paths.Content);

				itemHome = content.Add(Constants.Names.Home, simpleTemplate);

				itemHome.Editing.BeginEdit();
				itemHome["title"] = "Sitecore";
				itemHome["text"] = "Welcome to sitecore";
				itemHome["__icon"] = "Network/16x16/home.png";
				itemHome["__Security"] = @"ar|sitecore\Author|pe|+item:write|+item:create|pd|+item:write|+item:create|";
				itemHome.Editing.EndEdit();

				// Create items
				Item itemA = itemHome.Add("a", simpleTemplate);

				itemA.Editing.BeginEdit();
				itemA["title"] = "Title A";
				itemA["text"] = "This is the <b>text</b> of node a";
				itemA.Editing.EndEdit();

				Item itemB = itemHome.Add("B", simpleTemplate);

				itemB.Editing.BeginEdit();
				itemB["title"] = "title b";
				itemB["text"] = "Some text for item b";

				// Set layout for default device
				Sitecore.Data.Fields.LayoutField layout = itemB.Fields[Sitecore.FieldIDs.LayoutField];
				Sitecore.Layouts.LayoutDefinition ld = Sitecore.Layouts.LayoutDefinition.Parse(layout.Data.OuterXml);
				Sitecore.Layouts.DeviceDefinition dd = ld.GetDevice(defaultDevice.ID.ToString());
				dd.Layout = documentLayout.ID.ToString();
				Sitecore.Layouts.RenderingDefinition rd = new Sitecore.Layouts.RenderingDefinition();
				rd.ItemID = itemRendering.ID.ToString();
				dd.AddRendering(rd);
				itemB[Sitecore.FieldIDs.LayoutField] = ld.ToXml();

				itemB.Editing.EndEdit();

				Item itemF = folderTemplate.CreateItemFrom("f", itemHome);

				Item itemAA = itemF.Add("AA", simpleTemplate);
				itemAA.Editing.BeginEdit();
				itemAA["title"] = "title aA";
				itemAA.Editing.EndEdit();

				Item itemBB = itemF.Add("bB", simpleTemplate);
				itemBB.Editing.BeginEdit();
				itemBB["title"] = "title Bb";
				itemBB["text"] = "<h1>This is node bB</h1>";
				itemBB.Editing.EndEdit();
			}
		}
        public override void Apply(T ruleContext)
        {
            SC.Diagnostics.Assert.ArgumentNotNull(ruleContext, "ruleContext");
            SC.Diagnostics.Assert.ArgumentNotNull(
                ruleContext.Item,
                "ruleContext.Item");
            SC.Data.Fields.LayoutField layoutDetails =
                ruleContext.Item.Fields[SC.FieldIDs.LayoutField];
            SC.Diagnostics.Assert.IsNotNull(layoutDetails, "layoutDetails");

            if (String.IsNullOrEmpty(layoutDetails.Value) ||
                layoutDetails.InnerField.ContainsStandardValue)
            {
                return;
            }

            SC.Layouts.LayoutDefinition layout =
                SC.Layouts.LayoutDefinition.Parse(layoutDetails.Value);
            bool modified = false;

            for (int i = 0; i < layout.Devices.Count; i++)
            {
                SC.Layouts.DeviceDefinition device =
                    layout.Devices[i] as SC.Layouts.DeviceDefinition;
                SC.Diagnostics.Assert.IsNotNull(device, "device");

                for (int j = 0; j < device.Renderings.Count; j++)
                {
                    SC.Layouts.RenderingDefinition rendering =
                        device.Renderings[j] as SC.Layouts.RenderingDefinition;
                    SC.Diagnostics.Assert.IsNotNull(rendering, "rendering");

                    if (String.IsNullOrEmpty(rendering.Datasource) ||
                        !rendering.Datasource.StartsWith("/"))
                    {
                        continue;
                    }

                    SC.Data.Items.Item dataSource =
                        ruleContext.Item.Database.GetItem(rendering.Datasource);

                    if (dataSource == null)
                    {
                        string msg = this
                                     + " : unable to retrieve data source item "
                                     + rendering.Datasource
                                     + " in "
                                     + ruleContext.Item.Paths.FullPath;
                        SC.Diagnostics.Log.Warn(msg, this);
                        continue;
                    }

                    rendering.Datasource = dataSource.ID.ToString();
                    modified             = true;
                }
            }

            if (modified)
            {
                using (new SC.Data.Items.EditContext(ruleContext.Item, false, false))
                {
                    layoutDetails.Value = layout.ToXml();
                }
            }
        }