3

Recently I'm trying to use the Core Service in SDL Web 8 to create Components from XML files.

I found that the information on the SDL Documentation Center is not enough for me, and information from browser is a little messy.

Could anyone share some links from basic concepts to deep implementation?
Any share would be appreciated.
Thanks. :)

4 Answers 4

4

If you search this site for "core service create component", you get more than 100 resuls and some of those are pretty much to what you are looking for. I'm seeing a complete code example of constructing a Component from XML in Trying to create a Component with the Core Service: Data at the root level is invalid, which translates into something like:

using (var client = new CoreServiceClient(endPoint))
{
    var componentData = (ComponentData)client.GetDefaultData(ItemType.Component, folderUri, new ReadOptions()); 

    componentData.Title = "Your Title";
    componentData.Schema = new LinkToSchemaData { IdRef = schemaUri }
    componentData.Content = serializableXmlDocument;

    client.Create(componentData, new ReadOptions());
}

If you look through the documentation you also see a few examples regarding creating items, and specifically dealing with Components and fields.

All in all, I think all the information you are looking for is right there for you. If you encounter a specific issue with stringing all this example code together, that is something you could create a new question on.

1

You may also want to look at the Core Service Recipies section of the Tridion Cookbook: https://github.com/TridionPractice/tridion-practice/wiki/CookbookDocumentation#core-service-recipies

Although it doesn't have a specific example for creating a Component, it does introduce some Core Service concepts nicely, using examples such as Creating a new Publication and Changing the content and metadata of existing Components.

1

To create/migrate Tridion item using XmL is good option but its tricky when you have component link and media links etc.

  • Define your Schema with simple fields to test.
  • Title
  • Description

Create a class in C# like following.

public class Book
    {

        [XmlElement(ElementName = "Title")]
        public string Title { get; set; }
        [XmlElement(ElementName = "Description")]
        public string Synopsis{ get; set; }
   }

Fill this component with your required data and serialize it to disk as XML file. Similarly you can Create XML for metadata of you component.

Importing XML (Code similar to @bart's)

 var componentData = (ComponentData)tridionClient.CoreServiceClient.GetDefaultData(ItemType.Component, folderId, readOptions);
        componentData.Title = componentDetails.Title;
        var schemaId = tridionClient.CoreServiceClient.Read(componentDetails.SchemaWebDavUrl, readOptions).Id;
        componentData.Schema.IdRef = schemaId;

        var schemaFields = tridionClient.CoreServiceClient.ReadSchemaFields(schemaId, true, readOptions);

        componentData.Content = UpdateNamespace(componentDetails.ComponentData, schemaFields.NamespaceUri).OuterXml; ;
        // this is custom method which add required namespace in the XML. assuming you are generating XML from another source and this namespace is missing

        if (componentDetails.MetaData != null)
        {
            componentData.Metadata = UpdateNamespace(componentDetails.MetaData, schemaFields.NamespaceUri).OuterXml);
        }
        tridionClient.CoreServiceClient.Create(componentData, readOptions);
  • for any component link. you have to first create linked component and cache its tcm-id and put that tcm-in the parent component. this is very interesting part.

  • Don't forget to put check of component/folder already exists

  • Category keywords are just set of repeating nodes.
1

I wrote a program some time ago that imports RSS feed articles into components, you can see the code here.

Most of the CoreService logic is in ContentItem.cs and in the main Program.cs. It may be a bit more complex than what you need to get started , but I always see this as a good example of mapping content to a proper class structure, and then using class inheritance/OO logic to deal with the CRUD operations via CoreService.

Your Answer

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

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