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

  • Home
  • SEARCH
  • 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 6792629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:59:39+00:00 2026-05-26T17:59:39+00:00

I am facing the following problem: I want to add dynamically a mobile site

  • 0

I am facing the following problem: I want to add dynamically a mobile site for specific templates, I specified the mobile layout in the standard values of the specific item. This all works fine but when I changed a field of the Item the layout and renderings of the default site is gone! Does anyone has a solution/suggestion for this problem?

I am working with Sitecore 6.4.

Thanx in advance!

The code that I am currently using (this is to add hardcoded a layout to an item, the next step is (when I fixed this problem) to get the layout from the standard_values item)

    public class CheckMobileLayout
    {
    public void Process([NotNull] SaveArgs args)
    {
        try
        {
            foreach (Sitecore.Pipelines.Save.SaveArgs.SaveItem saveItem in args.Items)
            {
                Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
                if(orgItem.Name != "Content Editor")
                {
                    TemplateItem testTemplate = orgItem.Template;

                    foreach (Field orgField in orgItem.Fields)
                    {
                        if (orgField != null)
                        {
                            if (orgField.GetTemplateField().Type == "Mobile Checkbox")
                            {
                                foreach (Sitecore.Pipelines.Save.SaveArgs.SaveField saveField in saveItem.Fields)
                                {
                                    if (saveField.ID == orgField.ID)
                                    {
                                        if (saveField.Value != orgField.Value)
                                        {
                                            if (saveField.Value == "1") AddMobileLayout(orgItem);
                                            else RemoveMobileLayout(orgItem);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (NullReferenceException)
        {
        }
    }

    private void RemoveMobileLayout(Item orgItem)
    {
        using (new SecurityDisabler())
        {
            Database masterDatabase = Database.GetDatabase("master");
            orgItem = masterDatabase.GetItem(orgItem.Paths.Path);
            string renderingXml = orgItem[Strings.Renderings];

            LayoutDefinition layoutDefinition = new LayoutDefinition();
            layoutDefinition.LoadXml(renderingXml);

            string mobileDeviceId = Strings.mobileDeviceID;
            DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);

            deviceDefinition.Layout = String.Empty;

            string outputXml = layoutDefinition.ToXml();
            Log.Info(outputXml, this);
            orgItem.Editing.BeginEdit();
            orgItem[Strings.Renderings] = outputXml;
            orgItem.Editing.EndEdit();
        }
    }

    private void AddMobileLayout(Item orgItem)
    {
        using (new SecurityDisabler())
        {                
            Database masterDatabase = Database.GetDatabase("master");
            Item testItem = masterDatabase.GetItem(orgItem.Paths.Path);
            string renderingXml = testItem[Strings.Renderings];


            LayoutDefinition layoutDefinition = new LayoutDefinition();
            layoutDefinition.LoadXml(renderingXml);

            string mobileDeviceId = Strings.mobileDeviceID;
            DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);

            deviceDefinition.Layout = Strings.mobileLayoutID;

            string outputXml = layoutDefinition.ToXml();

            testItem.Editing.BeginEdit();
            testItem[Strings.Renderings] = layoutDefinition.ToXml();
            testItem.Editing.EndEdit();
        }
    }
}
  • 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-26T17:59:40+00:00Added an answer on May 26, 2026 at 5:59 pm

    I’ve fixed this with the following to methods:

            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 = null;
    
                item.Editing.BeginEdit();
                item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
                item.Editing.EndEdit();
            }
        }
    
        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) mobileDevice.AddRendering(new RenderingDefinition() { ItemID = sublayout.RenderingItem.ID.ToString(), Placeholder = sublayout.RenderingItem.Placeholder });
                            }
                        }
                    }
                }
                item.Editing.BeginEdit();
                item[Sitecore.FieldIDs.LayoutField] = layoutDefinition.ToXml();
                item.Editing.EndEdit();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing the following problem: I have a HTML document where I want
I'm learning Haskell now and I'm facing the following problem: I want to rewrite
I am facing the following problem. I have an image Gallery and I want
I'm facing the following problem: in the controller I select the data I need
I am learning facelets and Seam and I'm facing the following problem: I have
I am implementing a small database(university Project) and i am facing the following problem.
I'm currently facing the following issue: My app dynamically creates images (320 x 480
I am facing the following problem. Let's say I have a file, which contains
I'm making the design of a OO framework and I'm facing the following problem.
I am currently facing a serious problem. I use the standard django admin interface

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.