Creating the Pipe

To create the pipe, perform the following procedure:

  1. Create a new class and name it XmlDocument. You use the XmlDocument class to store an xml document:
    public class XmlDocument
    {
        public XmlDocument()
        {
            this.Id = Guid.Empty;
            this.Title = String.Empty;
            this.Content = String.Empty;
        }
     
        public XmlDocument(Guid id, string title, string content)
            : this()
        {
            this.Id = id;
            this.Title = title;
            this.Content = content;
        }
     
        public Guid Id
        {
            get;
            set;
        }
     
        public string Title
        {
            get;
            set;
        }
     
        public string Content
        {
            get;
            set;
        }
    }
  2. Create a new class and name it XmlInboundPipe.
  3. Add the following namespaces:
    using System.IO;
    using System.Security.Cryptography;
    using System.Xml.Linq;
    using PublishingModuleExtensions.Web.UI;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Localization;
    using Telerik.Sitefinity.Publishing;
    using Telerik.Sitefinity.Publishing.Configuration;
    using Telerik.Sitefinity.Publishing.Model;
    using Telerik.Sitefinity.Publishing.Pipes;
    using Telerik.Sitefinity.Publishing.Translators;
  4. Change the class definition to:
    [PipeDesigner(typeof(XmlPipeImportDesignerView), null)]
    public class XmlInboundPipe : IPipe, IPushPipe, IPullPipe, IInboundPipe
    {
    }
  5. Define the pipe properties by pasting the following code:
    public const string PipeName = "XmlInboundPipe";
     
    private string document = @"
    <documents>
    <document>
    <id>2998152B-83C8-4627-B19D-0CB203DBFD06</id>
    <title>test title</title>
    <content>
    this is my document body
    </content>
    </document>
    <document>
    <id>167B5AE9-61BC-4F70-AB6B-AD9563114AAB</id>
    <title>another test title</title>
    <content>
    this is another document
    </content></document></documents>";
     
    private IPublishingPointBusinessObject publishingPoint;
    private string publishingProviderName;
    private IDefinitionField[] definition = null;
  6. To deserialize the xml document into XmlDocument objects, add the following method:
    protected virtual IList<XmlDocument> DeserializeXml(string value)
    {
        var result = new List<XmlDocument>();
        using (TextReader tr = new StringReader(value))
        {
            var doc = XDocument.Load(tr);
            var xRoot = doc.Root;
            var documents = xRoot.Elements("document");
            foreach (var item in documents)
            {
                var id = new Guid(item.Element("id").Value);
                var title = item.Element("title").Value;
                var content = item.Element("content").Value;
     
                result.Add(new XmlDocument(id, title, content));
            }
        }
        return result;
    }
  7. To convert the XmlDocument object into WrapperObject, add the following method:
    public virtual WrapperObject ConvertToWraperObject(XmlDocument item)
    {
        WrapperObject obj = new WrapperObject(null);
        obj.MappingSettings = this.PipeSettings.Mappings;
        obj.Language = this.PipeSettings.LanguageIds.FirstOrDefault();
     
        obj.AddProperty(PublishingConstants.FieldTitle, item.Title);
        obj.AddProperty(PublishingConstants.FieldContent, item.Content);
        obj.AddProperty(PublishingConstants.FieldItemHash, GenerateItemHash(item));
        obj.AddProperty( PublishingConstants.FieldIdentifier, item.Id );
     
        return obj;
    }
  8. To load the wrapper objects when pushing them to the publishing point, add the following method:
    protected virtual IList<WrapperObject> LoadWrapperObjectItems()
    {
        var wrapperObjects = new List<WrapperObject>();
     
        IEnumerable<XmlDocument> result = this.DeserializeXml(this.document);
        foreach (var item in result)
        {
            var wrapperObject = this.ConvertToWraperObject(item);
            wrapperObjects.Add(wrapperObject);
        }
     
        return wrapperObjects;
    }
  9. To generate default mappings and definitions for the custom pipe, add the following method:
    internal protected static List<Mapping> GetDefaultMapping()
    {
        var mappingsList = new List<Mapping>();
     
        mappingsList.Add(PublishingSystemFactory.CreateMapping(PublishingConstants.FieldTitle, ConcatenationTranslator.TranslatorName, true, PublishingConstants.FieldTitle));
        mappingsList.Add(PublishingSystemFactory.CreateMapping(PublishingConstants.FieldContent, ConcatenationTranslator.TranslatorName, true, PublishingConstants.FieldDescription));
        mappingsList.Add(PublishingSystemFactory.CreateMapping(PublishingConstants.FieldItemHash, TransparentTranslator.TranslatorName, false, PublishingConstants.FieldItemHash));
        mappingsList.Add(PublishingSystemFactory.CreateMapping(PublishingConstants.FieldPipeId, TransparentTranslator.TranslatorName, false, PublishingConstants.FieldPipeId));
     
        return mappingsList;
    }
     
    internal protected static IList<IDefinitionField> CreateDefaultPipeDefinitions()
    {
        return new IDefinitionField[]
        {
            new SimpleDefinitionField(PublishingConstants.FieldTitle, Res.Get<PublishingMessages>().ContentTitle),
            new SimpleDefinitionField(PublishingConstants.FieldContent, Res.Get<PublishingMessages>().ContentContent),
            new SimpleDefinitionField(PublishingConstants.FieldItemHash, Res.Get<PublishingMessages>().ItemHash),
            new SimpleDefinitionField(PublishingConstants.FieldPipeId, Res.Get<PublishingMessages>().PipeId),
            new SimpleDefinitionField(PublishingConstants.FieldOriginalContentId, Res.Get<PublishingMessages>().OriginalItemId),
        };
    }
  10. To generate item hash when converting to wrapper object, add the following method:
    protected virtual string GenerateItemHash(XmlDocument item)
    {
        var builder = new StringBuilder(1000);
     
        builder.Append(item.Id);
        builder.Append("|");
        builder.Append(item.Title);
     
        SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider();
     
        byte[] originalBytes = Encoding.UTF8.GetBytes(builder.ToString());
        byte[] encodedBytes = hasher.ComputeHash(originalBytes);
        return Convert.ToBase64String(encodedBytes);
    }

First, you create the XmlDocument class that you use to store an xml document. Then, you create the pipe class, inherit the interfaces and specify the pipe control designer. You create the control designer in Creating the pipe control designer. You define the properties and method that you use to convert the xml string into separate documents and convert the documents to wrapper object. Finally, you define the default mappings and definitions of the pipe.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK