Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6893811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:42:19+00:00 2026-05-27T06:42:19+00:00

In my previous question I figured out how to add a layout programmatically in

  • 0

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.

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.

I’ve searched on the internet and added the fix from Sitecore for the __Renderings field, but this doesn’t solve the problem.

The code to add a Layout + sublayout:

        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();
            }
        }
    }

The code to remove the layout + sublayouts:

        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();
            }
        }
    }

Does anyone have any suggestions/answer(s)? I am using sitecore 6.4.1

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:42:19+00:00Added an answer on May 27, 2026 at 6:42 am

    A colleague (who is a Sitecore certified developer) has send a ticket to Sitecore… They send the following answer:

    Try to use the following code:

    Sitecore.Layouts.LayoutDefinition.Parse(LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField]))
    

    instead of:

    Sitecore.Layouts.LayoutDefinition.Parse(item[Sitecore.FieldIDs.LayoutField]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a previous question I figured out how to fix the anti-aliasing caused by
In a previous question I figured out how to start a password-authenticated ssh sessions
This question is based off of the same app/source from my previous question which
I've almost got this figured out by using previous questions, but I'm running into
This question is directly related to my previous question ASP.NET AJAX Is it possible
this question is an extension to a previous question i asked (and was answered).
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.