2

I am using below code to create a new component from a existing component. However I am getting metadata exception

ReadOptions _ReadOptions = new ReadOptions() { LoadFlags = LoadFlags.Expanded };
ComponentData OriginalComponentData =
    (ComponentData)_CoreServiceClient.Read(ComponentID, _ReadOptions);

ComponentData NewComponent = new ComponentData();
NewComponent.Title = "new-" + OriginalComponentData.Title;           
NewComponent.Content = OriginalComponentData.Content;
NewComponent.Metadata = OriginalComponentData.Metadata;

... few more lines of code to set location info and call to create method.

I am getting below error:

XML validation error. Reason: The element 'Metadata' in namespace 
'http://www.mycompany.com/tridion/schema/feedback' has invalid child element   
'embedded_workflow' in namespace 
'http://www.mycompany.com/tridion/schema/feedback'. 
List of possible elements expected: 'embedded_global' in namespace 
'http://www.mycompany.com/tridion/schema/feedback'.

I am unable to find why I am getting this error. When I check the value of OriginalComponentData.Metadata it's having below XML value

<Metadata xmlns="http://www.mycompany.com/tridion/schema/feedback">
      <embedded_workflow>
        <workflow_release>Non-Release</workflow_release>
        <effective_date>2013-01-29T15:35:53</effective_date>
      </embedded_workflow>
</Metadata>

So why it's invalid for new component. I am creating the component at the same location of its original component.

2
  • Just to double check, the schema is the same, right? Is the original component valid? Can you open it, edit and save, edit metadata in particular Commented Jul 24, 2015 at 11:02
  • When I check original component meta data schema in CME manually it's really 'embedded_global' not 'embedded_workflow'. But I unable to debug why core service retruning metadata schema as 'embedded_workflow' Commented Jul 24, 2015 at 11:08

2 Answers 2

2

Have you changed the original component's schema's fieldname at some point after it was created?

Components are not updated field name changes, mandatory data etc. when you change their schema. The CME does this for you when you open the component (you should see an information message stating this) but you must also then save it again to save the changes.

You can fix the original component as Peter suggests by opening it in the CME and saving it.

If you're using SDL Tridion 2013 SP1, you might also be able to leverage the CoreService2013 endpoint's synchronisation methods to try synchronise your new component before creating it. Something like...

NewComponent = (ComponentData) (_CoreServiceClient.SynchronizeWithSchema(
    NewComponent,
    new SynchronizeOptions
        {
            SynchronizeFlags = SynchronizeFlags.All
        }
    ).SynchronizedItem);

Also, if you want to create a copy of a component you could just use

RepositoryLocalObjectData _CoreServiceClient.Copy(
    string id,
    string destinationId,
    bool makeUnique,
    ReadOptions readBackOptions)
5
  • just to confirm NewComponent is an argument of SynchronizeWithSchema method ? I mean by then it's not been created. Shouldn't it be original component ? and then reading the original component data again via core service. Commented Jul 24, 2015 at 11:38
  • If I recall correctly you can pass either the original or the new component to the Core Service for synchronisation. The new component does not have to have been created already in order to synchronise it - the data currently held by the ComponentData object should be sufficient. No data is changed in the Content Manager Database when you synchronise - It's up to you to choose what you do with the synchronised component. Commented Jul 24, 2015 at 11:44
  • thanks David for help. It seems like yes the schema got updated. However looking at Sync method I am not getting created component data in return from core service. SynchronizationWithSchema returns synchronizationresult type rather than ComponentData and casting also gives compile time error. Commented Jul 24, 2015 at 12:35
  • Please re-read my example code above. SynchronisationResult has a property, SynchronizedItem, which is the synchronised component Commented Jul 24, 2015 at 12:36
  • Apology. I missed after .dot part. It worked perfectly. Commented Jul 24, 2015 at 12:47
4

Looks like the Metadata of "OriginalComponentData" is invalid. It's likely that someone changed the Schema after it was created. The Components do not automatically get updated when a Schema is changed (hence the giant warning about editing existing Schemas :)).

So it is possible for a Component to return invalid content or metadata, which would mean that you have to fix it before it will save.

To fix the error, you can try opening and saving the original Component in the UI -- which may fix it to match the current state of the Schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.