Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a component by providing the XML source file as input

Tags:

c#

.net

xml

tridion

I want to create a component by giving XML source input directly using core service 2011, in SDL Tridion 2011.

I want to write code to create a component by uploading source XML. Using the core service 2011.

Say name of the xml file is helloworld.xml and location is D:\abcd\cdef\all\helloworld.xml.

I have written the code like this, but its not working.

XmlDocument contentxml = new XmlDocument();
contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
Response.Write("<BR>" + contentxml.ToString());
component.Content = contentxml.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

The Response.write is displaying nothing. Correct me if I missed any thing. It's not creating any component and error is coming.

When i tried this:

XmlDocument contentxml = new XmlDocument();
try
{
    contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
}
catch (XmlException exp)
{
    Console.WriteLine(exp.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
contentxml.WriteTo(xw);
Response.Write("<BR>" + sw.ToString());

component.Content = sw.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

This time it's showing unable to find UUId: some thing like that.

My helloworld.xml looks like this.

<Content xmlns="uuid:1111eb85-0X11-11f9-1e2X-1X82X78fX920">
    <first>Hello World.This is Fisrt field</first>
    <second>Hello World.This is second field</second>
</Content>

It would be great if some one share some sample code to do it.

like image 562
Patan Avatar asked Dec 01 '22 23:12

Patan


2 Answers

Loading any XML from a a file and trying to create a component won't work unless the XML uses the format the CMS is expecting.

The XML structure of a component in SDL Tridion has some fixed parts (Example Nodes Content, Metadata) plus some flexible parts (The way you define the fields). First you need to have the XML with the same structure that the CMS is expecting. Typically the nodes that should be in your xml are the CONTENT and METADATA, load those in an XML Document and use the Core Service API to create a component using the content contained in those nodes. The best way to know how which is the structure of a component based on an schema is to create a sample component using the Tridion UI and see how the XML is constructed. After that you need to create your XML Sources using that structure. I posted recently an example of how to create a component using the Core Service, please have a look at that.

Faulted State error while creating component with Core Service

Following this code, you can access the nodes Content and Metadata

componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri); componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri);

And replace those with your content

like image 138
Miguel Avatar answered Dec 18 '22 11:12

Miguel


The general outline:

  1. Load the XML from the file into an XDocument / XmlDocument.
  2. Create a new Component by calling GetDefaultData on the client.
  3. Set the Content property of the Component to the XML.
  4. Save the Component by calling Save on the client.

If you haven't already, please have a look at the Core Service API documentation available on SDL Tridion World.

If you have trouble implementing this, please post the code that you have and what you have tried in order to make it work.

like image 31
Peter Kjaer Avatar answered Dec 18 '22 11:12

Peter Kjaer