2
votes

Following on from my earlier question about creating Address Books (many thanks Peter!), I have a small throw-away console application doing just that and working great - but in addition I'm trying to update the metadata of a Keyword with the Item Id of the created Address Book.

Slightly shortened snippet ...

StaticAddressBook ab = new StaticAddressBook();
ab.Title = title;
ab.Key = key;
ab.Save();

// id is a correct Keyword TCM ID
Keyword k = tdse.GetObject(id, EnumOpenMode.OpenModeEdit);
if (k != null)
{
    k.MetadataFields["addressbookid"].value[0] = ab.Id.ItemId;
    k.Save(true);
}

I keep getting the following error on Save():

XML validation error. Reason: The element 'Metadata' in namespace 'uuid:2065d525-a365-4b45-b68e-bf45f0fba188' has invalid child element 'addressbookid' in namespace 'uuid:2065d525-a365-4b45-b68e-bf45f0fba188'. List of possible elements expected: 'contact_us_email' in namespace 'uuid:2065d525-a365-4b45-b68e-bf45f0fba188'

But I know the Keyword has the correct Metadata assigned, (thats why I don't bother checking!). Shortened Tridion XML from a current keyword in question:

<tcm:Keyword>
   <tcm:Data>
      <tcm:MetadataSchemaxlink:type="simple"xlink:title="IP.Location.Metadata" xlink:href="tcm:49-2142-8" />
      <tcm:Metadata>
         <Metadata xmlns="uuid:2065d525-a365-4b45-b68e-bf45f0fba188">
            <email>...</email>
            <addressbookid>3</addressbookid>
            <contact_us_email>...</contact_us_email>
            <request_a_sample_email>...</request_a_sample_email>
            <webinar_feedback_email>....</webinar_feedback_email>
         </Metadata>
      </tcm:Metadata>
      <tcm:IsRoot>true</tcm:IsRoot>
   </tcm:Data>
</tcm:Keyword>

Have I missed something can Keyword metadata not be updated in this way?

I guess I could look at the Core Service to update Keywords, but it seemed to to make sense to do everything within this application.

UPDATE

Order was key here, strangely! The following code works:

ItemFields fields = k.MetadataFields;

System.Diagnostics.Debug.WriteLine(fields.Count);

string email = fields[1].value[1];
string contact = fields[3].value[1];
string request = fields[4].value[1];
string webinar = fields[5].value[1];

fields[1].value[1] = email;
fields[2].value[1] = ab.Id.ItemId;
fields[3].value[1] = contact;
fields[4].value[1] = request;
fields[5].value[1] = webinar;

k.Save(true);

Got caught out by the non-0-based index when getting/setting values and had to reassign existing fields back, in order.

Cheers

1
Almost replied with "Use ItemFields, it will handle that for you"... then noticed you're using TDSE... Try opening/saving & closing the keyword, then try again? - Nuno Linhares

1 Answers

2
votes

It seems that the order of the fields has changed in the Schema since that Component was created. At least the Schema expects contact_us_email in the position where you current have addressbookid.

There may be other changes, so I'd verify the order of fields in the Schema and make sure the Component(s) match, before you run your tool.