1

We are migrating Data from existing sharepoint to Sitecore 9.0.2 where we are using SXA 1.7.1 we got all the data like text & Images which need to be migrated but now we need to do below steps at rune time

1) Create Partial designs at run time

2) Create Page Designs at Run time

3) Assign Page to Page design

Can some body help me with relevant Sitecore API's to do these activities at run time.

0

2 Answers 2

1

SPE is a really good option. It really just wraps the Sitecore API and groups some things to make the calls easier.

Partial Designs

But if you want to just use the native API, then it should be pretty simple too. Partial Designs are just Sitecore Items with presentation applied to them. They need to be based of the right template, but it should be as simple as creating the item and setting the __Renderings or __Final renderings fields appropriately.

One thing to remember is that your Partial Design template is unique to your Tenant, so the template would be located somewhere like: /sitecore/templates/Project/My Tenant/Partial Design.

public Item CreatePartialDesign(string partialDesignName)
{
    // Get the parent item
    var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
    var parentItem = masterDb.Items["/sitecore/content/My Tenant/My Site/"];

    //Now we need to get the template from which the item is created
    TemplateItem partialDesignTemplate = masterDb.GetTemplate("Project/My Tenant/Partial Design");

    //Now we can add the new item as a child to the parent
    return parentItem.Add(partialDesignName, partialDesignTemplate);
}

Now you can set the presentation. It can get a bit complicated, but basically you need to set get the LayoutDefinition, find the default device and add a layout and 1 or more renderings. Here is some sample code that might get you started:

var partialDesignItem = CreatePartialDesign("New Partial Design");
string renderingXml = sampleItem["__Renderings"];
LayoutDefinition layoutDefinition = new LayoutDefinition();
layoutDefinition.LoadXml(renderingXml);

string defaultDeviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}";
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId);
string sampleLayoutId = "{layout-item-guid}";
deviceDefinition.Layout = sampleLayoutId;

// Add any renderings you want to a bit like this. You will need the rendering Id's
string sampleRenderingId = "{rendering-item-guid}";
RenderingDefinition renderingDefinition = new RenderingDefinition();
renderingDefinition.ItemID = sampleRenderingId;
renderingDefinition.Placeholder = "content";
deviceDefinition.AddRendering(renderingDefinition);

// Write the presentation back to the renderings field
string outputXml = layoutDefinition.ToXml();
sampleItem.Editing.BeginEdit();
sampleItem["__Renderings"] = outputXml;
sampleItem.Editing.EndEdit();

Page Designs

Page designs are also just items, so create the item and then you can set the value in the field Partial Designs. It is a multilist field, so just use the MultilistField object in the Sitecore API to set the values. Or just set the raw value as pipe separated guids.

Assigning a page design to a page

Again, this is just a field on the page item. The field just stores the ID of the page design you want to assign.

-1

Use Sitecore PowerShell Extensions. You can automate basically everything with it.

Take a look here: /sitecore/system/Modules/PowerShell/Script Library/SXA/SXA - Scaffolding. You will find there SXA scaffolding scripts which you can use as a tutorial. Whole automation, upgrade scripts and context menu scripts in SXA are written in PowerShell.

3
  • Thanks for update just wanted to know is this not possible using Native Sitecore API's . I am actually pretty new to Powershell Commented Oct 22, 2018 at 15:49
  • SPE is mostly a wrapper for the Native API's so it is possible to do it that way.
    – Richard Seal
    Commented Oct 23, 2018 at 12:48
  • Not really an answer
    – Mark Cassidy
    Commented Oct 23, 2018 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.