Attention

This document was written for an unsupported version of Plone, Plone 2.1.x, and was last updated 1606 days ago.

For more information, see the version support policy.

To learn how to upgrade to the current version of Plone, read the upgrade manual.

Creating New Entries with CTR

by Paul Everitt last modified Dec 30, 2008 03:05 PM
Loading a new XML file should create the correct content type. The Content Type Registry helps us.

As shown, editing existing entries was straightforward.  Creating new content in Plone based on external XML files is more problematic.  Namely, what content type should we use for the new resource?

As this isn't a one-size-fits-all situation, Marshall approaches this with configurability in mind.  For mortals like me, choice means confusion.  So this first example shows the simplest possible way to make it work, albeit in a clumsy-to-use fashion.

For this example, we will set a policy that any XML file ending in .atxmlpage will be used to create a Page resource.  The id of that resource will come from the rest of the filename.

The CMF's Content Type Registry is responsible for policies related to file extensions.  This tool can be reached via the ZMI in the portal root under the name "content_type_registry".

  1. Click on content_type_registry tool in ZMI.
  2. Scroll to the bottom.
  3. Add a predicate with a name such as atxmlpage, using Extension as the predicate type in the drop down, and click Add.
    Add CTR Predicate
  4. After saving, change the settings. Set the extensions value to atxmlpage and the content type in the drop-down to Page, then click Change.
    CTR Predicate Editing

The content type registry is now setup.  On your local disk, create a file somewhere named mynewxmlpage.atxmlpage and give it contents as shown below:

<?xml version="1.0" ?>
<metadata xmlns="http://plone.org/ns/archetypes/"
xmlns:cmf="http://cmf.zope.org/namespaces/default/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xmp="adobe:ns:meta">
<cmf:type>
Document
</cmf:type>
<dc:title>
My first page from XML
</dc:title>
<dc:description>
Congratulations! You have successfully installed Plone.
</dc:description>
<field id="text">
<![CDATA[
<h2>Initial Content</h2>
<p>This content came from an XML file on disk.</p>
]]>
</field>
</metadata>

Save this file, then return to cadaver.  In the top folder of your Plone site, use cadaver to add the file:

put mynewxmlpage.atxmlpage

cadaver uploads the file to Plone.  The CTR sees the extension and knows to create a Page (Document) using the ATXML marshaller, which reads the XML file for all the initial settings.

In Plone you can now go to the URL http://localhost:8080/atxml/mynewxmlpage.atxmlpage and see your new page.

Sure, there are a bunch of caveats to note:

  1. Be very careful to ensure you don't have a field with id="id" in your upload, nor a uid entry.
  2. Would be nice if the .atxmlpage extension disappeared.
  3. If you want to see all the settings, such as dc:subject and workflow state, that you can serialize to XML, go change some things on an existing Page and open it via cadaver.  There's lots there!  Sidnei's XML format was meant to capture lots of semantics.
  4. In a potential future addition to this how-to, I'll cover how to use cmf:type in the XML to create different types without the use of crazy file extensions.



Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.