Setting Content in Editable Webparts via the API

Nick Shepherd asked on February 10, 2015 19:30

This below is on an 8.x site.

So I'm trying to set some content on an editable web part on a page, the snippet of code I'm using is the following:

PageInfo nodePageInfo = PageInfoProvider.GetPageInfo(pageNode.DocumentGUID);
nodePageInfo.EditableWebParts["keyforeditablewebpart"] = ValidationHelper.GetString(content, "");

pageNode.SetValue("DocumentContent", nodePageInfo.DocumentContent);

What I'm finding is the above code only works if the page has been saved previously with content in the editableweb part. If it hasn't then there are no keys in the newPageInfo.EditableWebParts.Keys collection.

Am I going about this the wrong way? Based on the realization that these pages don't have any keys in their editablewebparts collection unless they've been physically saved using the Pages app, I'm starting to think there may be a better way of handling this via the API but have been unable to find anything on it.


Recent Answers

Trevor Fayas answered on February 12, 2015 15:12

What are you exactly going for, are you trying to simply get the content of another webpart (keyforeditablewebpart)?

I have a snippet of code that can get the rendered value of another webpart on the page, would that help?

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on February 12, 2015 15:51

This is how i found a webpart on the page (by WebpartID).

List<Control> allControls = new List<Control>();
GetControlList<Control>(Page.Controls, allControls);
Control specifiedWebpart = null;
foreach (var childControl in allControls )
     if (childControl.ID == WebpartID) specifiedWebpart = childControl;
if (specifiedWebpart == null) throw new Exception("Webpart ID Not found");

// Convert to CMSAbstractWebPart and call the ReloadData to ensure it's ready to be rendered
theControl = (CMSAbstractWebPart)specifiedWebpart;

The Function "GetControlList" is below:

/// <summary>
/// Helper function, gets all controls on page.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="controlCollection"></param>
/// <param name="resultCollection"></param>
private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection) where T : Control
    foreach (Control control in controlCollection)
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner

        if (control.HasControls())
            GetControlList(control.Controls, resultCollection);

From this you should be able to use "theControl" to set the value to your content. If you need to get the content from the web part. I'm not 100% positive if this is the 'quickest' way as it does scan all the controls, but it was the only way i could be sure to find it.

0 votesVote for this answer Mark as a Correct answer

Nick Shepherd answered on February 13, 2015 21:48

I'm doing a sort of migration of data from Documents on an older version of Kentico to version 8.1. On the older version of Kentico the content is stored as a Column on the Document Type and in the newer version of Kentico the content is just being thrown in an editable webpart on the Page itself.

The issue I ran into was that I found that if the document had content in let's say an editable webpart with the ID of content the actual key for that editable webpart in that Dictionary was something like content-<id> so I solved that by looking at all the keys and doing a partial string match to find the most appropriate match. However, the behavior is different if you try and access an Editable webpart on a newly created Page that hasn't had content saved in it. You can't find it in the EditableWebParts.Keys dictionary, since it's empty (if none of the editable webparts on the page have any content assocaited with them).

What I found though, was if this is the state of the Page and you just do the following it works:

pageInfo.EditableWebParts["content"] = content;

it let's you assign the content without issue. I'm not sure if this is a bug or done on purpose and there is a better way to handle this (another series of libraries I should be using to access these EditableWebparts).

Thanks for the response, I'll take a look at your code and see if it makes more sense to take your approach moving forward.

0 votesVote for this answer Mark as a Correct answer

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