public void Createkeyword()
{
client = CoreServiceClientFactory.GetClient();
//string publicationuri = System.Configuration.ConfigurationSettings.AppSettings["PubID"].ToString();
string CategoryId = "tcm:13-36761-512";
CategoryData category = (CategoryData)client.Read(CategoryId, new ReadOptions());
SchemaData meta = (SchemaData)client.Read("tcm:12-145242-8", new ReadOptions());
string[] tcmList = System.IO.File.ReadAllLines(Environment.CurrentDirectory + "\\" + "familykeywordTitle.txt");
if (category != null)
{
int i = 0;
foreach (var title in tcmList)
{
KeywordData keyword = (KeywordData)client.GetDefaultData(ItemType.Keyword, category.Id, new ReadOptions()) as KeywordData;
// Tridion.KeywordData keyword = (Tridion.KeywordData)client.GetDefaultData(Tridion.ItemType.Keyword, category.Id);
keyword.Id = "tcm:0-0-0";
keyword.Title = title;
keyword.MetadataSchema = meta;
//keyword.Title = "Sea-web the ultimate marine online database";
//keyword.Key = null;
keyword = (KeywordData)client.Create(keyword, new ReadOptions()) as KeywordData;
Console.Write(i);
// client.Create(keyword, null);
i++;
}
}
1 Answer
If you look at the Core Service API documentation, the MetadataSchema property should be of type LinkToSchemaData
.
public LinkToSchemaData MetadataSchema { get; set; }
The way you'd set this in your code is like this:
keyword.MetadataSchema = new LinkToSchemaData() { IdRef = meta.Id.ToString() };
Also, you'd want to set the metadata field values as well. For this, you'd have to build the contents as a string and set that to the Metadata
property of the keyword.
string metadataContent = "<Metadata xmlns=\"" + meta.NamespaceUri + "\">" +
"<field1>value</field1>" +
"<field2>value</field2>" +
"</Metadata>";
keyword.Metadata = metadataContent;
The Core Service API documentation is available here: https://docs.sdl.com/LiveContent/content/en-US/SDL%20Web-v5/GUID-2301148A-E3EA-43EF-A2FC-7D3C9BAEE8FF.
You can also use the Fields class that Nuno built (https://github.com/NunoLinhares/TridionWebData/blob/master/ImportContentFromRss/ImportContentFromRss/Content/Fields.cs) which simplifies updating content and metadata field values without you having to do XML manipulation.
To see how the Fields class is used to update content or metadata, samples are here: https://github.com/TridionPractice/tridion-practice/wiki/ChangeContentOrMetadata.