I was faced with the task of creating a custom Field in SharePoint 2007 (WSS 3.0) which presents (in new and edit mode) a TreeView control with some hierarchical data. The user should click and select
one node in the TreeView before saving the item.
Now, my challenge turned out to be
what to save in the item. I had never created a custom Field before and still, as far as I can see, it's possible to only save one value for the field.
I needed to save both the ValuePath and the textual representation of the path to the selected Node.
The solution was to save both those values in the same string, separated by, by my choice, a '#' character. Now, in order to present the textual representation of this string in the Item's List's View, I had to implement a small JavaScript on the client to split the value and return the correct part of the string. The JavaScript looks like this:
function SplitString(theString) {
var theStrings = theString.split("#");
var theStringsLength = theStrings.length;
if (theStringsLength > 1) {
document.write("<div style='DISPLAY: block'>");
document.write("<div style='WHITE-SPACE: nowrap'>" + theString[0]+ "</div>");
document.write("</div>");
}
}
Now, in Edit mode, it was straight forward to make sure to select the correct Node. I just did a split on the value again, and fetched to other value of the split (which in my case is the ValuePath of the Node) and did a myTreeView.FindNode(mySplitValue).Selected = true;.
This isn't very elegant and it hurts implementing this, but it works! I wish though, that there's a better way of doing this. I actually expected to have a 'View presentation' of the saved string (where I could format the string the way I wanted) and a 'Edit presentation' of the saved string. However, I couldn't find any. So, if anyone knows that it actually is possible and how, I would appreciate the solution :-)!
Have a nice day!