Tuesday, December 10, 2013

Tridion 2013 SP1 - API improvements

Continuing with my Tridion 2013 SP1 posts I will talk about some API improvements. Below a list containing API improvements.

StreamDownload endpoint can now download external items

Sample:
private static StreamDownloadClient downloadChannel { get; set; }

using (downloadChannel = new StreamDownloadClient("streamDownload_basicHttp_2013")) {
    try {
Stream stream = downloadChannel.DownloadExternalBinaryContent("http://www.sdl.com/Content/themes/common/images/sdl-logo.png");
MemoryStream ms = new MemoryStream();

int b;
while ((b = stream.ReadByte()) != -1) {
ms.WriteByte((byte)b);
}

using (FileStream fs = new FileStream("C:\\sdl-logo.png", FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (BinaryWriter writer = new BinaryWriter(fs)) {
    try {
               writer.Write(ms.ToArray());
    } finally {
        writer.Close();
        fs.Close();
        ms.Close();
    }
}
    }
    finally {
        if (downloadChannel.State != CommunicationState.Closed) {
            downloadChannel.Close();
        }
    }
}

Schemas can be retrieved by Namespace

This is a change in both Core Services and TOM .NET APIs. In this post I will show a Core Service sample.

Sample:

private static CoreServiceClient channel { get; set; }

using (channel = new CoreServiceClient("basicHttp_2013"))
    try {
SchemaData schema = GetSchemaFromNamespace();
    }
    finally {
        if (channel.State != CommunicationState.Closed) {
            channel.Close();
        }
    }
}  

public static SchemaData GetSchemaFromNamespace() {
    LinkToSchemaData schema = channel.GetSchemasByNamespaceUri(PublicationId, "http://www.tridion.com/ContentManager/5.0/DefaultMultimediaSchema", null).FirstOrDefault();
    if (schema != null) {
        return (SchemaData)channel.Read(schema.IdRef, new ReadOptions());
    }
    return null;
}

This change is very important since now we can retrieve schemas by using namespaces (XML like functionality) without having the need to specify tcm uris or webdav urls.

Multimedia Components can be created without specifying a Multimedia Type

This is an small change but it will save you several lines of code J, if the file extension that you are using to create the multimedia component can be mapped to an existing multimedia type, then the API will do it for you.

Sample:

private static CoreServiceClient channel { get; set; }

using (channel = new CoreServiceClient("basicHttp_2013"))
    try {
SchemaData schema = GetSchemaFromNamespace();
CreateMultimediaComponent(schema);
    }
    finally {
        if (channel.State != CommunicationState.Closed) {
            channel.Close();
        }
    }
}  

public static void CreateMultimediaComponent(SchemaData schema) {
    ComponentData multimediaComponent = new ComponentData() {
        Id = TcmUri.UriNull.ToString(),
        Title = Guid.NewGuid().ToString(),
        Schema = new LinkToSchemaData() { IdRef = schema.Id },
        LocationInfo = new LocationInfo() { OrganizationalItem = new LinkToOrganizationalItemData() { IdRef = FolderId } },
        ComponentType = ComponentType.Multimedia,
        BinaryContent = new BinaryContentData() {
            Filename = "sdl-logo.png",
            UploadFromFile = "C:\\sdl-logo.png"
        }
    };

    channel.Create(multimediaComponent, new ReadOptions());
}

Decommissioning Publication Targets

This is a very important change, and it will allow us to mark items that were published to a publication target as unpublished to that publication target in one single operation. It is useful in case you have a publication target that is no longer used and can block other items to be deleted or moved.

Sample:

public static void DecommissioningPublicationTarget(string publicationTargetId) {
    channel.DecommissionPublicationTarget(publicationTargetId);
}

1 comment:

  1. We were spoiled by this feature already in the Content Manager Explorer: "Multimedia Components can be created without specifying a Multimedia Type." Nice to have it in the API as well. :-)

    ReplyDelete