Access in code to details of all web parts and widgets used on specific document

Tom Troughton asked on October 15, 2015 14:29

Hi,

I have a particular requirement in code where, given a specific TreeNode, I'd like to find first all the zones on the template being used then, for each zone, get the details of all web parts and widgets used in those zones.

In my case I do not need to worry about template inheritance. None of my pages implement template inheritance.

I found this post on the old Kentico forums which suggested I might be able to do use PageInfoProvider to get a PageInfo object for the relevant TreeNode then use its PageTemplateInfo property to gain access to what I need.

However, I don't see a PageTemplateInfo property of CMS.DocumentEngine.PageInfo. There is DesignPageTemplateInfo and UsedPageTemplateInfo. I thought maybe UsedPageTemplateInfo would be the one, and it does indeed include the correct zones in its WebPartZones collection. But I don't see the web parts (actually, widgets) I'm expecting in the zones' WebParts collections.

Does anyone know how I might access the details I need? Thanks.

P.S. My template uses the ASPX+portal model.

P.P.S. I guess what I'm asking is, how can I use API to gain access to the content of the DocumentWebParts column from dbo.CMS_Document, preferably as a structured object?


Update 1

I've realised I can get access to the information I need by calling .GetProperty("DocumentWebParts") on TreeNode, but is it possible to get this information as a structured object?

Recent Answers


Brenden Kehren answered on October 15, 2015 15:03

You can use PageTemplateInfo and PageTemplateInfoProvider to get the page template info you need. The webparts are stored under the .Webparts property of the PageTemplateInfo object. You can get your page template ID from the treeNode.DocumentPageTemplateID property of your tree node.

0 votesVote for this answer Mark as a Correct answer

Tom Troughton answered on October 16, 2015 10:14

Hi Brenden. Thanks for the reply. However, correct me if I'm wrong but this would return details of the template, out of the scope of the current document. Crucially I also need to know details of widgets added to widget zones on the document, is this possible?

As I mentioned in my update, the DocumentWebParts property of dbo.CMS_Document holds all the information I need, but ideally I wouldn't parse this XML myself (presumably there is already something in the API which does this). Any idea?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 19, 2015 15:19

Sorry, I didn't see the update before I published my answer. Unfortunately, I've not had a need to this kind of work. But looking through the API Documentation should get you going. Also knowing the structure of how the API works should help. Also know, that only widget info is stored with the document info. Webparts and their settings are stored with the page template so you cannot use the CMS_Document table exclusively. Reason being is widgets have content that is specific to a page. Webparts are not specific to pages, they are specific to a template.

Hypothetically, you should be able to do this to get the webpart info.

  • Parse the XML to get the controlid attribute of the <webpart> element.
  • For each <webpart> take the controlid and use the WebPartInfoProvider.GetWebPartInfo(webpartName) to get the webpart properties. Unfortunately, this won't give you the content *. For each of the widgets you should be able to perform a similar action.

As I stated, I've not had a need to do this before but knowing the API, this should get you closer to what you're looking for.

0 votesVote for this answer Mark as a Correct answer

Timothy Fenton answered on October 19, 2015 16:47 (last edited on October 19, 2015 18:15)

Hello Nat

Brenden is correct that you will need more than just the CMS_Document table for this. So something like this should work:

PageTemplateInfo somepagetemplate = PageTemplateInfoProvider.GetPageTemplateInfo(SomeDocument.GetUsedPageTemplateId()); //where "SomeDocument" is a PageInfo object.

string webparts = somepagetemplate.webparts;

webparts is a string of the webparts on the document

you also have access to somepagetemplate.webpartzones which is a collection of WebPartZoneInstance's that you can shuffle through

** after reviewing the previous replies again i see what the issue is that you are having, brenden's solution may be your best bet here -- we do not have anything built in to parse the XML into an object directly, you would simply get the controlid of the webpart and then get the webpartinfo object by that id using the provider.

Hope this helps

0 votesVote for this answer Mark as a Correct answer

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