Update other node property values in a custom datatype
Hi everyone,
Before my first post, will just say I'm just starting out with Umbraco and loving it!Thanks for a great product and great documentation/support.
I have a particular document type which some of its properties' data already exist in another system. So the idea is when the CMS user creates the page, they may choose to import some content from the other system by entering an ID, clicking a button, then the CMS will let them choose which data to copy in, before updating the properties' values in the CMS.
So far I have created a new datatype which stores the identifier entered (which is just a string) and renders using a user control implementing IUsercontrolDataEditor. I added a button to the user control and made the API call to the other system to load in the existing data and let the user choose which values to import. Now comes my problem - I have to somehow copy the values into the CMS.
My ideal solution is to find the various editor fields in the cms (all are textstrings) and update the value in them, so that the new values can be checked in their actual fields before the user clicks Save. Is this possible (to update values of OTHER properties from within one property's editor) and if so, how? If not, I was going to find the current node's ID so I can load it up and update the values programatically, but I can't find out how to get the current node ID either! Perhaps i'm going about this completely wrong and should be extending the main CMS editor 'page' instead of a custom datatype.
I think your approach should work fine. you've implemented the right interface, and the framework will take care of calling the Save() method on your custom datatype. On Save(), make sure to format your data in the form which umbraco can accept to save the data to the database (int, varchar, ...).
If you need to check or change other properties' data, you could do this by subscribing to the Save event on your document (Find more info on event handling in the wiki section) and if required, change the data of the other properties. Properties are exposed throuhg their alias, so
document.getProperty("alias).Value
will get you the value of a specific property on the current edited document.
If using the event approach, you'll have total control over what document is being edited (comes as an argument on the event)
Thanks for your reply. Using the save event would work but it would mean I have to serialize the list of "data to import" and then deserialize it in the event handler, and the user would not have a chance to check the data before it gets saved... I was hoping I could just copy the values into the right text boxes while the user is editing, so they can check the values before Saving. Perhaps it can be done using JavaScript to get a reference to the other inputs (it seems their ID are kind of fixed using the property alias....?)
Yes, I was thinking the same as you... using JavaScript (or specifically jQuery) to populate the other fields on the client-side.
I've looked through a few of my recent Umbraco installs and the HTML INPUT Id's for each property are "ctl00_body_", then the property alias. So it should be easy to populate those textfields via your custom data-type.
The only gotcha would be if the control ID prefix changes ... not sure what the circumstances are for that to happen ... but I wouldn't rule it out. So then you might have to try to guess it - using the ClientId of your custom data-type (stripping out the alias suffix, if that makes sense?)
Good luck with this, let me know how it goes ... it could be very promising for future custom data-types.
Cheers, Lee.
PS. It's good to have another local (Bristol-based) developer on the forums!
Thanks, this is what I ended up doing and it works a treat. Unfortunately the ClientID of my usercontrol doesn't match that of the builtin textstring fields - it has another level of nesting, so I have hardcoded it for now. Hopefully the IDs could only change on upgrading the CMS in which case we would notice it and update the code.
Update other node property values in a custom datatype
Hi everyone,
Before my first post, will just say I'm just starting out with Umbraco and loving it!Thanks for a great product and great documentation/support.
I have a particular document type which some of its properties' data already exist in another system. So the idea is when the CMS user creates the page, they may choose to import some content from the other system by entering an ID, clicking a button, then the CMS will let them choose which data to copy in, before updating the properties' values in the CMS.
So far I have created a new datatype which stores the identifier entered (which is just a string) and renders using a user control implementing IUsercontrolDataEditor. I added a button to the user control and made the API call to the other system to load in the existing data and let the user choose which values to import. Now comes my problem - I have to somehow copy the values into the CMS.
My ideal solution is to find the various editor fields in the cms (all are textstrings) and update the value in them, so that the new values can be checked in their actual fields before the user clicks Save. Is this possible (to update values of OTHER properties from within one property's editor) and if so, how? If not, I was going to find the current node's ID so I can load it up and update the values programatically, but I can't find out how to get the current node ID either! Perhaps i'm going about this completely wrong and should be extending the main CMS editor 'page' instead of a custom datatype.
Thanks for your help,
Andy
Hi Andrew,
I think your approach should work fine. you've implemented the right interface, and the framework will take care of calling the Save() method on your custom datatype. On Save(), make sure to format your data in the form which umbraco can accept to save the data to the database (int, varchar, ...).
If you need to check or change other properties' data, you could do this by subscribing to the Save event on your document (Find more info on event handling in the wiki section) and if required, change the data of the other properties. Properties are exposed throuhg their alias, so
will get you the value of a specific property on the current edited document.
If using the event approach, you'll have total control over what document is being edited (comes as an argument on the event)
Hope this helps.
Regards,
/Dirk
Hi Dirk,
Thanks for your reply. Using the save event would work but it would mean I have to serialize the list of "data to import" and then deserialize it in the event handler, and the user would not have a chance to check the data before it gets saved... I was hoping I could just copy the values into the right text boxes while the user is editing, so they can check the values before Saving. Perhaps it can be done using JavaScript to get a reference to the other inputs (it seems their ID are kind of fixed using the property alias....?)
Thanks
Hi Andrew,
Yes, I was thinking the same as you... using JavaScript (or specifically jQuery) to populate the other fields on the client-side.
I've looked through a few of my recent Umbraco installs and the HTML INPUT Id's for each property are "ctl00_body_", then the property alias. So it should be easy to populate those textfields via your custom data-type.
The only gotcha would be if the control ID prefix changes ... not sure what the circumstances are for that to happen ... but I wouldn't rule it out. So then you might have to try to guess it - using the ClientId of your custom data-type (stripping out the alias suffix, if that makes sense?)
Good luck with this, let me know how it goes ... it could be very promising for future custom data-types.
Cheers, Lee.
PS. It's good to have another local (Bristol-based) developer on the forums!
Hi Lee,
Thanks, this is what I ended up doing and it works a treat. Unfortunately the ClientID of my usercontrol doesn't match that of the builtin textstring fields - it has another level of nesting, so I have hardcoded it for now. Hopefully the IDs could only change on upgrading the CMS in which case we would notice it and update the code.
Andy
is working on a reply...