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.