Yes you could use the ContentService to find the page, read the likeCount property, increment by 1 and then Save it back into Umbraco.
However I wouldn't do that, Every save, will create a new version of your content item in the Umbraco database, editors won't be able to roll back to earlier versions of the content,without losing likes! - but if lots of people like things, your db will slow down with tons of un-necessary content versions.
If your site uses Members, then consider using Relations to create a relation between a Member and a Page object, you can count the number of Relations created, and make sure people don't like pages twice!
If you don't have membership then yes, having the like button call an APIController that updates a custom sql table is the way to go.
Great!, it used to be an exercise on the old Umbraco Level 2 training course I used to teach, in order to highlight pitfalls of incrementing content items via code... so your post 'took me back' a bit!
Update node property from surface controller?
Good morning,
I've been asked to add a simple like button to pages on our intranet.
I was thinking of adding a "likeCount" property to the document type, then calling a surface controller to update the counter.
Is it possible for a surface controller to update a property on a node?
If not I'll create a SQL table to hold the like counts.
Thanks, Mike
Hi Mike
Yes you could use the ContentService to find the page, read the likeCount property, increment by 1 and then Save it back into Umbraco.
However I wouldn't do that, Every save, will create a new version of your content item in the Umbraco database, editors won't be able to roll back to earlier versions of the content,without losing likes! - but if lots of people like things, your db will slow down with tons of un-necessary content versions.
If your site uses Members, then consider using Relations to create a relation between a Member and a Page object, you can count the number of Relations created, and make sure people don't like pages twice!
If you don't have membership then yes, having the like button call an APIController that updates a custom sql table is the way to go.
regards
marc
Hi Marc,
Thanks for the info. I was wondering if updating the node property would require a save, causing issues.
We don't have members, yet, so it's just a simple counter for now.
I have got it working with a separate sql table.
Cheers, Mike
Hi Mike
Great!, it used to be an exercise on the old Umbraco Level 2 training course I used to teach, in order to highlight pitfalls of incrementing content items via code... so your post 'took me back' a bit!
Glad you got something working.
regards
Marc
is working on a reply...