Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > How to Add/Edit Widgets like using the Page Tab but with the API? View modes: 
User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 7/22/2012 2:37:45 PM
   
How to Add/Edit Widgets like using the Page Tab but with the API?
I am importing content from another CMS and I would like to be able to use widgets for some of their content elements.

I can't find an example anywhere that tells me how to Add new widget instances or edit existing widget instances in code using the API.

I did find an example of how to add and edit widgets as though they were added using the Design tab, but that is not what I need.

I have been able to do this without using the API by querying the database and building the xml myself and saving it, but this just seems like a dirty way to do it.

I would also like to know how to make it so a page will re-inherit the widgets from it's template that were added in Design Tab. Here is the Scenario:

1. I created a Page Template and added widgets in the Design Tab on a parent page.

2. I added a child page and set it to inherit the page template.

3. I added a widget to the child page using the Page Tab. (I believe that when I edit the child page's widget zone's widget collection, it copies the widgets from it's parent and adds the widget that I just added with the dialog, essentially breaking the link between the widgets added in the Design tab of the page template.)

4. I added another widget to the Parent page using the Design Tab.

5. I check the child page and the new widget does not appear. (I believe this is because of the same reason I described in #3)

6. I delete the widget that I had added to the child page in the Page Tab to see if it would show the new widget from the Parent page. It didn't. (This still makes sense because I am still editing the child page's specific collection of widgets)

7. I delete all of the widgets on the child page to see if the widgets from the template would appear. They don't. (This makes sense because someone may want to have no widgets at all in a zone where it would otherwise inherit widgets).

8. I look everywhere for a way to make it inherit the widgets again using the CMS interface, I find nothing.

9. I look in the database to compare a document that is inheriting widgets and the one child page that has no widgets displayed. I notice that the child page's DocumentWepParts XML still has a reference to the WebPartZone in it, but with no child elements in it.

10. I start looking for a way to remove that instance using the API. I can't find one.

There needs to be a button or something in the CMS interface that allows a user to tell the page to inherit the widgets from the template again.

Until then, (I suspect it won't be a feature added until at least version 8) I need to know how I can do this by creating a custom tool that I can put in a page template or something for the user to click.

User avatar
Kentico Support
Kentico Support
kentico_janh - 7/23/2012 4:34:45 AM
   
RE:How to Add/Edit Widgets like using the Page Tab but with the API?
Hello,

I hope I've got it right and here is an explanation:

1) All you change or set under the Design tab is page template related -> this means that the changes are applied on each document which uses that template.

2) All you change or set under the Page tab is document related -> these changes are applied only for that particular document

So if you apply any change on your parent document Design tab, then it is not applied on the child document Page tab (not even on the parent document Page tab), but there is a button (on a Page tab) which you are probably looking for called "Reset to default" which takes definitions from the page template (in your case from the parent document) and apply them to a current document.

Best regards,
Jan Hermann

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 7/23/2012 9:13:49 AM
   
RE:How to Add/Edit Widgets like using the Page Tab but with the API?
Thanks Jan,

I do understand why it does what it does. It makes perfect sense to me. I just didn't know about the "Reset to Default" button. I still can't find that button. Could you describe where it is or show me a screenshot?

It doesn't sound like that button does exactly what I want (I'll test it when I find it). If it removes all of the Page's widgets from all of the web part zones and inherits them from the template, that isn't exactly what I want. I need more control than that. I want to either be able to choose which widget zones to clear out and inherit or to make it so that Reset to Default button only resets widget zones where I have removed all of the Page's widgets.

I have made a web part with a button in it that will remove the references of empty widget zones for the current document when I click it, which then causes the templates widgets to appear. I can turn this into an AJAX call to a web service that will refresh the page on callback so I can inject a button somewhere in the interface without changing any of the CMS's code.

The problem is, I am using regular ADO.Net to query the database, modify the XML for the DocumentWebParts column and then ADO.Net again to update the database, which does not use the Document Workflow and does not create a new version of the document so that it can be undone if necessary.

This is why I need to know how to use the API to modify the widgets on a document. So, if you know the answer, could you please answer the first part of my earlier question?


Thank you!

User avatar
Kentico Support
Kentico Support
kentico_janh - 7/26/2012 1:11:25 AM
   
RE:How to Add/Edit Widgets like using the Page Tab but with the API?
Hello,

That reset button are not there by default, you need to add the Widget action web part on that document to display mentioned button. This button resets all widget on that page, but it shouldn't be that difficult to customize it only for a specific widget zone. The code of this web part is located in the \CMSWebParts\Widgets\WidgetActions.ascx.cs file and here you can find also some code examples. If you need more API than is contained in that file, please let me know and I will discuse it with our developer.

Best regards,
Jan Hermann