Updating Components Using the Core Service in SDL Tridion 2011

In my previous post I covered how to create components in Tridion using the WCF Core Service and XML De-serializing.

Using that same code base, you can also update a component by using the Serialize method on the Object. This below method does the trick:

[code lang="c-sharp"]
public void UpdateComponent(CoreService2010Client client, string componentUri)
{
try
{
ComponentData component;

// check out the component
component = client.TryCheckOut(componentUri, null) as ComponentData;

try
{
//deserialize the xml content into a ProductPrice object
ProductPrice pricing = ProductPrice.Deserialize(component.Content);

//update the required fields
pricing.SetupFee = 100;

//serialize the object back to xml and set this as the component content
component.Content = pricing.Serialize();

//save the component
client.Save(component, null);
}
catch (Exception ex)
{
Log.Error("UpdateComponent: Unable to save component", ex);
}

//check the component back in
client.CheckIn(componentUri, null);
}
catch (Exception ex)
{
Log.Error("UpdateComponent: Could not update the component", ex);
}
}
[/code]

Any questions?

If you need more information or have any questions just get in touch and we'd be happy to answer them for you.