Adding web part to template dynamically.

Targutai Yesugei asked on June 30, 2017 11:20

Is there any possibility to add web parts on template dynamically? I need to create template and fill it at the same time. I found PageTemplateWebParts column in CMS_PageTemplate table, but i'm not sure that it's a good idea - working with database ignoring Kentico API.

P.S. I found similar questions, but they were refer to older versions of Kentico.

Thanks in advance!

Correct Answer

Trevor Fayas answered on June 30, 2017 16:15

I would definitely not just modify in the database if it can be helped!

Here's some code, i don't have time to test it, but check it out and see if this gets you where you need!

PageTemplateInfo theTemplate = PageTemplateInfoProvider.GetTemplates().WhereEquals("TemplateName", "MyTemplate").FirstObject;
    foreach(var templateZone in theTemplate.WebPartZones)
    {
        switch(templateZone.ZoneID) {
            case "MyZone1":
                WebPartInstance newWebPart = new WebPartInstance();
                newWebPart.IsWidget = false;
                newWebPart.ControlID = "WebPartCodeName";
                // Set properties
                newWebPart.Properties.Add("Property1", "value1");
                templateZone.AddWebPart(newWebPart);
                break;  
        }


    }
    // Check to see if the additions worked, if so just call update
    string WebPartXML = theTemplate.WebParts;
    theTemplate.Update();

    // However, if the WebPartXMl isn't updated at this point, may need to manually generate the new XML and set it
    List<XmlElement> newWebPartZoneXML = theTemplate.WebPartZones.Select(x => x.GetXmlNode()).ToList();
    // BUILD proper XML structure here using hte list of XmlElements
    XmlDocument newWebPartXmlDoc = new XmlDocument();
    // ...
    // Save the webpart XML
    theTemplate.WebParts = newWebPartXmlDoc.OuterXml;
    theTemplate.Update();
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 30, 2017 17:33

Also check out the API Examples docs. They will get you some of the namespaces and classes you can use although no specific example on how to do exactly what you're looking for. Trevor's answer will help a lot with that.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on June 30, 2017 17:48

If you can explain better what you are trying to achieve - you will get the right answer. You can dynamically hide/show web parts using macro lanugage. This is a better and simpler way to manage content of a page template. I've never had a situation when I need dynamically add web part -hide/show will be enough to achieve this.

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on July 3, 2017 06:21

Hello, sorry for late answer.

Trevor, thank you, i'll try it today.

Peter, I want to migrate site to Kentico and now it's uses some kind of page builder where you can build you page from different htmls. I did a little research and found that static html web part is good enough to achieve that in Kentico, so i want to create api which will create web parts with certain html content and place it onto template to create ready-for-publish page.

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on July 4, 2017 09:47 (last edited on July 4, 2017 10:04)

Trevor, I tried your code and it almost worked!

One last question - i can't find proper value of WebPartType property to initialize WebPartInstance object. I checked api examples given by Brenden, but didn't found it. Also, i checked CMS_WebPart table and found WebPartType field but it have integer type. I tried using "Standard" but Kentico shows me "Error loading the webPart of type 'Standard'

I need to use Static HTML web part, if this info needed.

Found out, that this WebPartType field should contain codename of webpart.

P.S. thank all you!

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 5, 2017 15:29

Apologies, i thought the ControlID was the Webpart COdeName.

In which case, can you add the below and try again?

newWebPart.WebPartType = "statichtml";

That should do the trick, the Integer in the CMS_WebPart is defining what 'genre' of webpart it is (there are standard, layout, and other types of web parts), but the WebPartInstance.WebPartType is a reference to the webpart's code name, so slightly confusing that they are using the same name but they are 2 separate items.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.