API Questions on Kentico API.
Version 5.x > API > Updating EditableWebParts content via API View modes: 
User avatar
Member
Member
markcoatsworth - 7/15/2011 10:27:06 AM
   
Updating EditableWebParts content via API
Hi there,

I have a website with a lot of standard pages. My standard page template has two webparts: a title box called 'PageTitle' and a content html area called 'PageContent'.

I'm trying to run a script over all the pages in my site which will update the content in the PageContent area. The script I used looks something like this:


ThisNode = tree.SelectSingleNode(Convert.ToInt32(nodeData["NodeID"]), "en-CA");
string PageContent = ThisNode.DocumentContent["PageContent"];
// ... manipulate the PageContent string here ...
ThisNode.DocumentContent.EditableWebParts["PageContent"] = PageContent;
ThisNode.Update();


The script executes properly, but when I looked at the page again later, it still had original content.

So I tried running my script again. This time, the script fails completely because ThisNode.DocumentContent["PageContent"]; does not contain any data. But when I look at the page on the front end of the site, it still looks fine.

I decided to look at the actual [CMS_Document].[DocumentContent] column in the database. It turns out that my script actually saved a new webpart value into the content node. Here's what it looks like now:

<content>
<webpart id="PageContent">...</webpart>
<webpart id="pagetitle;42f75fe3-cfe0-4438-9c78-e64b92c71544">...</webpart>
<webpart id="pagecontent;f3c4ef64-3c5b-4672-8fe3-0c99a6ef236d">...</webpart>
</content>

Can you let me know how to modify my script so that it overwrites the original pagecontent webpart content, instead of creating a new one?

Thanks!

User avatar
Kentico Developer
Kentico Developer
kentico_ondrejv - 7/28/2011 8:11:36 AM
   
RE:Updating EditableWebParts content via API
Hello,

We were solving this issue over an e-mail conversation and I'd like to share some hints with other users:

There was problem with workflow which was being used. In this case, it was necessary to use DocumentHelper class to obtain the node object via the GetDocument method:

node = DocumentHelper.GetDocument(node, tree);

and UpdateDocument method to save it properly:

DocumentHelper.UpdateDocument(node, tree);

More info can be found here: http://devnet.kentico.com/docs/devguide/a_complete_example.htm

Best regards
Ondrej Vasil