Database database = Sitecore.Configuration.Factory.GetDatabase("master");
Item item = //Sitecore item to which wfm form will be assigned
Item wfmItem = //wfm form item which to be assign
Item formLayout = database.GetItem("{6D3B4E7D-FEF8-4110-804A-B56605688830}"); // Rendering used Form/Mvc Form , here i am using Form
//Renderings/Modules/Web Forms for Marketers/Form
string renderingXml = item["__Renderings"];
LayoutDefinition layoutDefinition = LayoutDefinition.Parse(renderingXml);
string defaultDeviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}";
// Layout/Devices/Default item id
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(defaultDeviceId);
string sampleRenderingId = formLayout.ID.ToString();
RenderingDefinition renderingDefinition = new RenderingDefinition();
renderingDefinition.ItemID = sampleRenderingId;
renderingDefinition.Datasource = wfmItem.ID.ToString();
renderingDefinition.Placeholder = "main"; // ContentPlaceHolderName;
deviceDefinition.AddRendering(renderingDefinition);
string outputXml = layoutDefinition.ToXml();
item.Editing.BeginEdit();
item["__Renderings"] = outputXml;
item.Editing.EndEdit();
// The publishOptions determine the source and target database,
// the publish mode and language, and the publish date
PublishOptions publishOptions =
new PublishOptions(item.Database,
Database.GetDatabase("web"),
PublishMode.SingleItem,
item.Language,
System.DateTime.Now); // Create a publisher with the publishoptions
Publisher publisher = new Publisher(publishOptions);
// Choose where to publish from
publisher.Options.RootItem = item;
// Publish children as well?
publisher.Options.Deep = true;
// Do the publish!
publisher.Publish();
Comments
Post a Comment