Showing posts with label flex. Show all posts
Showing posts with label flex. Show all posts

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);
}

Tuesday, May 29, 2012

Consuming SDL Tridion Core Services from Adobe Flex - Part 1

With the SDL Tridion 2011 arrival several integration opportunities came into the picture of content consumption and integration. In this post I will cover the basics about how to consume content from SDL Tridion by using non COM, .Net or java related technologies like adobe flex.

The techniques covered in this post can be used for a wide range of technologies which can support standard SOAP 1.1 web services.

Establishing connectivity between Core Services and Flex. 

In order to establish connectivity in adobe flex we will use the mx.rpc.soap.WebService class. Not like .Net or Java, flex does not provide an abstraction layer like a stub which hides the low level details, in this case it is necessary to have a good understanding of the Core Services WSDL.

Not like WCF where we have the concepts of endpoint, binding, addresses, methods and contracts, in adobe flex we have to use standard WSDL concepts like ports, operations, messages and schemas.

The following WSDL fragment will show the list of Core Services ports that can be used in flex.

<wsdl:service name="CoreService2011">
  <wsdl:port name="basicHttp" binding="i0:basicHttp">
    <soap:address location="http://localhost/webservices/CoreService2011.svc/basicHttp" />
  </wsdl:port>
  <wsdl:port name="streamDownload_basicHttp" binding="i0:streamDownload_basicHttp">
    <soap:address location="http://localhost/webservices/CoreService2011.svc/streamDownload_basicHttp" />
  </wsdl:port>
  <wsdl:port name="streamUpload_basicHttp" binding="i0:streamUpload_basicHttp">
    <soap:address location="http://localhost/webservices/CoreService2011.svc/streamUpload_basicHttp" />
  </wsdl:port>
</wsdl:service>

streamDownload_basicHttp and streamUpload_basicHttp are not 100% soap 1.1 compatible since they use MTOM/XOP optimizations as message format and Flex does not support MTOM/XOP yet. I found a workaround for it by adding a new binding/endpoint combination that uses base64Array(Text) as message format which is the format used for interoperability.

For this post I have developed 3 actionscript classes than can be used to connect by using different kind of ports. CoreServicesClientBase is the base class used by BasicHttpClient and StreamDownloadClient

CoreServicesClientBase.cs

package com.tridion.cs {
    import mx.rpc.events.*;
    import mx.rpc.soap.*;
     
    public class CoreServicesClientBase {
        public const WSDL_URL:String = "http://localhost/webservices/CoreService2011.svc?singleWsdl";
        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";
       
        protected var port:String;
        protected var channel:WebService;
       
        public function CoreServicesClientBase(loadListener:Function) {
            channel = new WebService();
            channel.useProxy = false;
            channel.port = port;
            channel.addEventListener(LoadEvent.LOAD, loadListener);
            channel.loadWSDL(WSDL_URL);
        }
    }
}

BasicHttpClient.cs

package com.tridion.cs {
    import mx.rpc.events.*;
    import mx.rpc.soap.*;
    import mx.rpc.wsdl.*;
    import mx.rpc.xml.*;
    import mx.utils.ObjectProxy;
   
    public class BasicHttpClient extends CoreServicesClientBase {
        public function BasicHttpClient(loadListener:Function) {
            port = "basicHttp";
            super(loadListener);
        }
    }
}


StreamDownloadClient.cs

package com.tridion.cs {
    import mx.rpc.events.*;
    import mx.rpc.soap.*;
    import mx.rpc.wsdl.*;
    import mx.rpc.xml.*;
    import mx.utils.ObjectProxy;
   
    public class StreamDownloadClient extends CoreServicesClientBase {
        public function StreamDownloadClient(loadListener:Function) {
            port = "streamDownload_base64_basicHttp";
            super(loadListener);
        }
    }
}


*Note: As mentioned above, I added a new binding/endpoint "streamDownload_base64_bassicHttp" to the Core Services configuration file in order to disable MTOM/XOP but keeping it in the out of the box functionality.


The last step in this post will be the actual code to connect to the Core Services as shown in the code fragment below.

protected var channel:BasicHttpClient;
protected var downloadChannel:StreamDownloadClient;

protected function Init():void {
    InitBasicHttpChannel();
    InitDownloadHttpChannel();
}

protected function InitBasicHttpChannel():void {
    var OnChannelLoaded:Function = function(event:LoadEvent):void {
        event.currentTarget.removeEventListener(LoadEvent.LOAD, OnChannelLoaded);
       
        trace("Connection is established using BasicHttp");
    };
    channel = new BasicHttpClient(OnChannelLoaded);   
}

protected function InitDownloadHttpChannel():void {
    var OnChannelLoaded:Function = function(event:LoadEvent):void {
        event.currentTarget.removeEventListener(ResultEvent.RESULT, OnChannelLoaded);
       
        trace("Connection is established using StreamDownload_BasicHttp");
    };
    downloadChannel = new StreamDownloadClient(OnChannelLoaded);
}