6

I have a requirement to create Components based on a Schema via Core Service. The content will be coming from an Excel spreadsheet. I was able to handle most of the field types, except one with KeywordFieldDefinitionData. To add to the complexity it's a multi valued field.

The content I get from Excel for that field is a list Ex. "IN", "JP", "US". How can I add these Keywords to the multi valued field? My sample data:

comp.Metadata

<Metadata xmlns="uuid:5f855153-bb88-4796-826b-efef6985bfad">
  <AssetType>Multimedia Document</AssetType> 
  <ContentType>Case Study</ContentType> 
  <ContentType>Research Report</ContentType> 
  <Audience>Customers</Audience> 
  <Audience>Partners</Audience> 
  <Industry>Financial Services</Industry> 
  <Industry>Media & Entertainment</Industry> 
  <Industry>Online Gaming</Industry> 
  <Country>IN</Country> 
  <Country>JP</Country> 
  <Country>KR</Country> 
  <Country>US</Country> 
  <ProductFamily>Cloud Networking</ProductFamily> 
  <Product>Kona Web Application Firewall</Product> 
  <ActualCreationDate>2014-04-14T18:28:37</ActualCreationDate> 
  <ModifiedDate>2017-04-14T18:31:36</ModifiedDate> 
  <DisplayStatus>Active</DisplayStatus> 
</Metadata>

Code:

//Metadata Fields
var metadataFields = Fields.ForMetadataOf(schemaFields, component);
metadataFields["AssetType"].Value = (firstSheet.Cells[rowIdx, 10].Value).ToString();

//what to do here
metadataFields["ContentType"].Value = (firstSheet.Cells[rowIdx, 11].Value).ToString();
metadataFields["ContentType"].Value = (firstSheet.Cells[rowIdx, 12].Value).ToString();
metadataFields["Audience"].Value = (firstSheet.Cells[rowIdx, 13].Value).ToString();
metadataFields["Industry"].Value = (firstSheet.Cells[rowIdx, 14].Value).ToString();
metadataFields["Country"].Value = (firstSheet.Cells[rowIdx, 15].Value).ToString();
metadataFields["ProductFamily"].Value = (firstSheet.Cells[rowIdx, 16].Value).ToString();
metadataFields["Product"].Value = (firstSheet.Cells[rowIdx, 17].Value).ToString();
metadataFields["ActualCreationDate"].Value = (firstSheet.Cells[rowIdx, 18].Value).ToString();
metadataFields["ModifiedDate"].Value = (firstSheet.Cells[rowIdx, 19].Value).ToString();

if (((firstSheet.Cells[rowIdx, 20].Value).ToString()) != null)
    metadataFields["CampaignID"].Value = (firstSheet.Cells[rowIdx, 20].Value).ToString();

metadataFields["DisplayStatus"].Value = (firstSheet.Cells[rowIdx, 21].Value).ToString();
component.Metadata = metadataFields.ToString();
7

Creating a Component via the Core Service can be tricky thing. I see that you use Fields class for creating Components. For your case here, Keyword is a dependency that you must have already in system if you want to use it in your Component.

That being said, you must alter your code to firstly check if a Keyword with that title is present in Tridion. If it is present, read it via Core Service and use it, if not, you will need to create it. Regarding the issue that it's multivalue, you should be able to use Values property instead of Value.

Also, instead of using Fields class, I would suggest working with XML directly. Try creating your model that reflects Dchema and then do the object serialization to XML (for content and metadata).

| improve this answer | |
  • Thank you Marko. Yes the keywords are already created, so i dont have to go through the trouble of checking for their existance.. will try to use values. I did start with XML by creating a model that reflects my schema, but got struck on how to handle the metadata.. and again even here, same issue with multi-select fields – Harish A May 23 '17 at 13:20
3

For starters it is important to understand how a Keyword is used in a Component. If you look at the Component XML, you will see that Keywords are xlinks:

<Content xmlns="uuid:48e5a5ad-8bb0-4216-88ee-4448aa11c1c4">
  <ExampleField xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:1-2-1024" xlink:title="Keyword 1">Keyword 1</ExampleField>
  <ExampleField xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:1-3-1024" xlink:title="Keyword 2">Keyword 2</ExampleField>
</Content>

So if you only have the Keyword title in your Excel sheet, you will indeed have to do some additional work like Marko has mentioned in his answer (in case you have to create the Keywords, than you will for sure need to do extra work indeed).

As for dealing with multivalue, there have been a few questions with answers about that here already, for example:

You could also use the XML to not have to deal with the separate fields and update the content of your Component in one go (again for this you will also find a few examples online if you search).

| improve this answer | |
  • Thank you Bart. Yes i do understand how keywords are shown as xlinks. will check these links.. About XML, yes it does make sense, but in my case sometimes in the spreadsheet, that values may be null, or wrong values (keywords). such cases, i just want to skip that and continue with others. – Harish A May 23 '17 at 15:25
  • As a matter of fact, Keyword fields can indeed be requested as XLink elements (using LoadFlags.KeywordXlinks), but are not expected to be XLinks when saving; Keyword fields should be represented as XML elements with the Keyword title as text content. – Rick Pannekoek May 24 '17 at 19:08
  • 1
    For clarity: the example XML shown is the Question is perfectly valid if Country is a Keyword field with Keyword titles IN, JP, KR and US. – Rick Pannekoek May 24 '17 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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