Sunday, June 28, 2015

Ektron accessing smart form content from code and displaying in a widget

Ektron smart forms are a great way to provide a content structure which is easy for content authors to use. When it comes to displaying the content on your Ektron web site, XSLT is one option, however accessing the content programmatically (via the Ektron API) gives the developer more control over how the data is transformed and displayed.

Create a C# class of the smart form

The first requirement is to create a C# class of the smart form being accessed. This is done by downloading the XSD file (an XSD file is the definition of XML data - in this case the Ektron smart form which is stored in XML) of the smart form - found on the data definition section of the smart form configuration.

Windows has a built in tool (called XSD.exe) which can automatically generate a class based on the XSD. Open up the command line and focus it on the following directory;
c:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\x64
Once in there, run the following command:
xsd.exe c:\temp\MySmartform.xsd /c /language:CS /outputdir:"c:\temp" 
In this case it is creating a C# class of the MySmartform.xsd (found in c:\temp) and outputting in the same location. The class is automatically named root, we can give this a better name (such as news) and include a definition so that it will map correctly from the Ektron service.
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", ElementName = "root", IsNullable = false)]
public partial class News {

public partial class root {
Now place this class in the App_Code folder in your Ektron web site, this will allow it to be used in code

Accessing Ektron smart from data via the API

In this case, we have an folder in the Ektron folder which contains items of this smartform type. We simply set the API criteria to that folder ID and the content (smartforms) inside will be returned.
Ektron.Cms.Framework.Core.Content.Content content = new Ektron.Cms.Framework.Core.Content.Content();
var criteria = new Criteria<ContentProperty>();
criteria.AddFilter(ContentProperty.FolderId, CriteriaFilterOperator.EqualTo, 75);
var resultList = content.GetList(criteria);
var publications = new List<Publication>();

foreach (var result in resultList)
    var pub = (Publication)EkXml.Deserialize(typeof(Publication), result.Html);
Publication in this case is the name of the smartform class (created earlier). This code will then give a list of these publications which can then be transformed and displayed as required.

1 comment:

  1. I am currently using XSLT which allows me to style all the content from the smartform as I please. How will it work if I serialize the data as shown above?