Hi Joe,
It's generally not wrong to extend functionality of a form control so that it stores an extra information in separate field, so it's ok.
An altenative way would be to use
Custom TreeNode handlers (
http://devnet.kentico.com/docs/devguide/treenode_handler.htm).
Anyway, in your case, the solution and explanation is quite straightforward:
1) It's better to perform any field setting in Form Control's
Value GET method, because that is called when the form is being saved. Set method is for filling the control when the form is loaded for existing document.
2) Your code doesn't work when publishing the document, because under workflow, you cannot use this approach and SelectSingleNode - see details in
API reference on Workflows and versioning / Working with the versioned documents
3) It's not necessary to select the node twice, since you have access to the document that's being edited.
The best example of code that you'll need in your custom form control can be found in
~\CMSModules\Content\FormControls\Tags\TagSelector.ascx.cs
in GroupId property GET method:
public int GroupId
{
get
there are two branches for state when new document is being created or when an existing one is being edited, to simplify, the most important code lines (accessing the node's NodeAliasPath property) are:
for insterting new doc:
((TreeNode)this.Form.ParentObject).NodeAliasPathfor editing existing doc:
((TreeNode)this.Form.EditedObject).NodeAliasPathYou can copy and modify whole branch starting with
if ((mGroupId == 0) && (this.Form != null))
{
... where you only need to check the second condition (that Form is not null).
So, I believe it should be enough to use this approach instead of your code, set the node's property and let the form engine save the document with the modified custom field value automatically. You won't need to worry about workflow or call Update method... it should just simply work.
Hope this helps.
Regards,
Zdenek