Updating a property using the Content Service api in Umbraco 6.x
Asked Answered
C

1

6

I've created a custom user control for the back-end of my Umbraco site that allows administrators to quickly update certain fields on nodes without having to navigate through the content tree.

So far my code is working as expected: I can update simple true/false properties without a problem. However now I'm trying to update a property that's of a custom data type and I'm running into difficulties.

The data type itself is just a simple drop down that lists a series of availability statuses ie. Available, Unavailable, Sold and Reserved. The datatype is storing the text values.

Here's the code I have that allows me to update my true/false properties:

public void ChangeInteractiveStatus(string nodeId, bool chkValue)
{
    var cs = ApplicationContext.Current.Services.ContentService;
    var apartment = cs.GetById(Convert.ToInt32(nodeId));
    apartment.SetValue("displayOnInteractive", chkValue);
    cs.SaveAndPublish(apartment);
}

This works absolutely fine as the data type of this property is a regular true/false data type.

Here's the code I'm using to change the value of my custom dropdownlist data type:

public void ChangeAvailabilityStatus(string nodeId, string status)
{
    var cs = ApplicationContext.Current.Services.ContentService;
    var apartment = cs.GetById(Convert.ToInt32(nodeId));            
    apartment.SetValue("status", status);
    cs.SaveAndPublish(apartment);
}

As you can see there's very little difference and yet this code isn't working.

In order to check what was happening when I was updating the properties with the above code, I checked the umbraco.config file only to find that the property in question was displaying as follows:

<status><![CDATA[]]></status>

However when I change the value in the content tree (without using my admin control) the value gets saved properly as:

<status><![CDATA[Sold]]></status>

So for whatever reason, when I try to update the value it's being rejected and I can't work out why.

FYI I tried entering the value as:

"<![CDATA[" + status + "]]>"

Yet that made no difference.

Does anyone know how I can fix this? How can I get the property to update correctly?

Thanks

Cavender answered 19/9, 2013 at 16:21 Comment(0)
C
3

Okay I've figured out what the problem was. It seems the values were being stored as name-value pairs, so the actual value getting stored in the database was an integer. Once I updated the code to insert the integer id it all worked as expected! Hooray.

Cavender answered 20/9, 2013 at 9:59 Comment(1)
Thanks, I was having a similar problem with a Contour workflow that created a node with dropdown values. I was trying to save the text and not the id. Your pointer help me fix the problem.Mince

© 2022 - 2024 — McMap. All rights reserved.