title: Sitecore programmatically add layout. Value cannot not be null. Parameter name Path
body: <p>In my previous question I figured out how to add a layout programmatically in Sitecore, this works fine when the layouts and sublayouts are the same as the Standard Values. But when this is different I get a Value cannot not be null. Parameter name Path error. </p>
  
  <p>I am adding a layout programatically because the requirements are to add a mobile layout when a checkbox is checked. And remove to layout when the checkbox is not checked.</p>
  
  <p>I've searched on the internet and added the <a href="http://sdn.sitecore.net/Products/Sitecore%20V5/Sitecore%20CMS%206/ReleaseNotes/KnownIssues%20Recommended/Incorrect%20Renderings%20field%20value%20may%20cause%20errors.aspx" rel="nofollow">fix</a> from Sitecore for the __Renderings field, but <a href="http://sdn.sitecore.net/Products/Sitecore%20V5/Sitecore%20CMS%206/ReleaseNotes/KnownIssues%20Recommended/Incorrect%20Renderings%20field%20value%20may%20cause%20errors.aspx" rel="nofollow">this</a> doesn't solve the problem.</p>
  
  <p>The code to add a Layout + sublayout:</p>
  
  <pre><code>        protected void AddMobileLayout(Item item)
      {
          using (new SecurityDisabler())
          {
              LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);
              DeviceDefinition mobileDevice = layoutDefinition.GetDevice(Resources.mobileDeviceID);
              TemplateItem itemTemplate = item.Template;
  
              if (itemTemplate != null)
              {
                  if (itemTemplate.StandardValues != null)
                  {
                      Item standardValues = itemTemplate.StandardValues;
  
                      foreach (DeviceItem deviceItem in Sitecore.Configuration.Factory.GetDatabase("master").Resources.Devices.GetAll())
                      {
                          if (deviceItem.ID.ToString() == Resources.mobileDeviceID)
                          {
                              mobileDevice.Layout = standardValues.Visualization.GetLayout(deviceItem).ID.ToString();
                              RenderingReference[] sublayouts = standardValues.Visualization.GetRenderings(deviceItem, true);
  
                              foreach (RenderingReference sublayout in sublayouts)
                              {
                                  RenderingDefinition rendering = new RenderingDefinition();
                                  rendering.Placeholder = sublayout.Placeholder;
                                  rendering.ItemID = sublayout.RenderingItem.ID.ToString();
  
                                  mobileDevice.AddRendering(rendering);
                              }
                          }
                      }
                  }
              }
              item.Editing.BeginEdit();
              try
              {
                  item.Fields[Sitecore.FieldIDs.LayoutField].Value = layoutDefinition.ToXml();
                  item.Editing.EndEdit(false);
              }
              catch (System.Exception ex)
              {
                  Sitecore.Diagnostics.Log.Error("Could not update item " + item.Paths.FullPath + ": " + ex.Message, this);
                  item.Editing.CancelEdit();
              }
          }
      }
  </code></pre>
  
  <p>The code to remove the layout + sublayouts:</p>
  
  <pre><code>        protected void RemoveMobileLayout(Item item)
      {
          using (new SecurityDisabler())
          {
              LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);
              DeviceDefinition mobileDevice = layoutDefinition.GetDevice(Resources.mobileDeviceID);
  
              if (mobileDevice.Layout != null) mobileDevice.Layout = null;
              if (mobileDevice.Renderings != null) mobileDevice.Renderings = new System.Collections.ArrayList();
  
              item.Editing.BeginEdit();
              try
              {
                  item.Fields[Sitecore.FieldIDs.LayoutField].Value = layoutDefinition.ToXml();
                  item.Editing.EndEdit(false);
              }
              catch (System.Exception ex)
              {
                  Sitecore.Diagnostics.Log.Error("Could not update item " + item.Paths.FullPath + ": " + ex.Message, this);
                  item.Editing.CancelEdit();
              }
          }
      }
  </code></pre>
  
  <p>Does anyone have any suggestions/answer(s)? I am using sitecore 6.4.1</p>
  
  <p>Thanks in advance</p>