Friday, June 1, 2012

Consuming SDL Tridion Core Services from Adobe Flex - Part 2

In the first part Consuming SDL Core Services from Adobe Flex - Part 1 I covered the foundation of the interoperability between SDL Tridion and Adobe Flex. In this second part I will cover more advanced topics like effectively consuming data available in the Content Manager. This topic will cover read and filtering operations.

Sending soap requests to the Core Services


The web services based on soap implementation available in flex is pretty simple and does not allow you to do sophisticated requests that are easy to do in languages like C#. However, it gives you complete control on the soap messages being transferred between the flex application and the Core Services. The main issue with this approach is that must know the exact soap syntax that the Core Services understand in order to process the request. If you refer to my other article Inspecting SDL Tridion Core Services SOAP messages you will find a tool that allows you to inspect the soap messages being transferred between a C# client and the Core Services, in this post I will use the same syntax for my flex client. The following soap body would be the one used to send a request for a "Where Used" operation.

<GetListXml xmlns="http://www.sdltridion.com/ContentManager/CoreService/2011">
    <id>tcm:5-6</id>
    <filter xmlns:d4p1="http://www.sdltridion.com/ContentManager/R6"
              xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
              i:type='d4p1:UsingItemsFilterData'>
        <d4p1:ItemTypes>
              <d4p1:ItemType>Component</d4p1:ItemType>
        <d4p1:ItemTypes>
    </filter>
</GetListXml>

 

Receiving soap responses from the Core Services


Flex supports several response formats (the result formats must be specified during the request), in this topic I will cover the most important ones in order to retrieve information from the core services.

The e4x format will instruct the parser to return the soap response as it is and to don't apply any kind of serialization. This format is really important when we retrieve xml lists. This also solves a serialization problems related with the Core Services because all the xml list responses are defined as xsd:Any in the xml schema and Flex cannot serialize them.

The object format will instruct the parser to return a serialized soap response. It will return an ObjectProxy instance from there we can retrieve all the information as properties. For instance for a component we can retrieve the content and metadata of a component using the following code.

var content:String = result.Content.toString();
var metadata:String = result.Metadata.toString();

 

Reading a tridion item


As shown in the code snippets below I am defining two methods one to retrieve the Tridion Item as a serialized object and the other one as an XML stream. Note that the operation name passed to the channel.getOperation method must match with the one defined in the Core Services WSDL.

public function ReadItem(id:String, success:Function, error:Function):void {
    var op:Operation = Operation(channel.getOperation("Read"));
    op.addEventListener(ResultEvent.RESULT, success);
    op.addEventListener(FaultEvent.FAULT, error);
    op.resultFormat = "object";
    op.send(id);
}

public function ReadItemXml(id:String, success:Function, error:Function):void {
    var op:Operation = Operation(channel.getOperation("Read"));
    op.addEventListener(ResultEvent.RESULT, success);
    op.addEventListener(FaultEvent.FAULT, error);
    op.resultFormat = "e4x";
    op.send(id);

 

Retrieving an xml list

This code snippet will retrieve a "Where Used' list.

public const CORESERVICE_NS:String = "http://www.sdltridion.com/ContentManager/CoreService/2011";
public const TRIDION_NS:String = "http://www.sdltridion.com/ContentManager/R6";
public const XSDI_NS:String = "http://www.w3.org/2001/XMLSchema-instance";
public const TRIDION_5_NS:String = "http://www.tridion.com/ContentManager/5.0";

public function GetListUsingItems(id:String, itemTypes:Array, success:Function, error:Function):void {
    var op:Operation = Operation(channel.getOperation("GetListXml"));
    op.addEventListener(ResultEvent.RESULT, success);
    op.addEventListener(FaultEvent.FAULT, error);
   
    var param:XML =
        <GetListXml xmlns={CORESERVICE_NS}>
            <id>{id}</id>
            <filter xmlns:d4p1={TRIDION_NS} xmlns:i={XSDI_NS} i:type='d4p1:UsingItemsFilterData'>
                <d4p1:ItemTypes />
            </filter>

        </GetListXml>
   
    var itemTypesXML:XMLList = param.descendants(new QName(TRIDION_NS, "ItemTypes"));
    for (var index:String in itemTypes) {
        var type:String = itemTypes[index];
        itemTypesXML.appendChild(<ItemType xmlns={TRIDION_NS}>{type}</ItemType>);
    }
   
    op.resultFormat = "e4x";
    op.send(param);
}

 

Retrieving a system wide list

This code snippet will retrieve the list of publications in the system

public const CORESERVICE_NS:String = "http://www.sdltridion.com/ContentManager/CoreService/2011";
public const TRIDION_NS:String = "http://www.sdltridion.com/ContentManager/R6";
public const XSDI_NS:String = "http://www.w3.org/2001/XMLSchema-instance";
public const TRIDION_5_NS:String = "http://www.tridion.com/ContentManager/5.0";

public function GetListPublicationsXml(success:Function, error:Function):void {
    var op:Operation = Operation(channel.getOperation("GetSystemWideListXml"));
    op.addEventListener(ResultEvent.RESULT, success);
    op.addEventListener(FaultEvent.FAULT, error);
   
    var param:XML =
        <GetSystemWideListXml xmlns={CORESERVICE_NS}>
            <filter xmlns:d4p1={TRIDION_NS} xmlns:i={XSDI_NS} i:type='d4p1:PublicationsFilterData' />
        </GetSystemWideListXml>
   
    op.resultFormat = "e4x";
    op.send(param);
}

No comments:

Post a Comment