jan-h
11/15/2013 - 10:18 AM

Publish a page using SDL Tridion Core Service. This example shows how to use the Core Service API without configuration file and using the B

Publish a page using SDL Tridion Core Service. This example shows how to use the Core Service API without configuration file and using the BasicHTTP binding. There are other options too, like using the configuration file instead of in code configuration and using NetTcp instead of BasicHTTP.

using System;
using System.ServiceModel;
using System.Xml;
using Tridion.ContentManager.CoreService.Client;

namespace TridionCoreserviceSession
{
    /* SDL Tridion Core Service sesion.
     * Based on     http://yatb.mitza.net/2012/03/core-service-client-sample-code.html
     */
    public class CoreServiceSession : IDisposable
    {
        private readonly CoreServiceClient _client;
        private const string ServiceUrl = "http://{0}/webservices/CoreService2011.svc/basicHttp" ;
        private const int MaxMessageSize = 10485760;
           
        public CoreServiceSession(string hostname)
        {
            _client = CreateBasicHttpClient(hostname);
        }
       
        public CoreServiceSession(string hostname, string username, string password)
        {
            _client = CreateBasicHttpClient(hostname);

            _client.ChannelFactory.Credentials.Windows.ClientCredential =
                new System.Net.NetworkCredential (username, password);
        }

        private CoreServiceClient CreateBasicHttpClient(string hostname)
        {
            var basicHttpBinding = new BasicHttpBinding
            {
                MaxReceivedMessageSize = MaxMessageSize,
                ReaderQuotas = new XmlDictionaryReaderQuotas
                {
                    MaxStringContentLength = MaxMessageSize,
                    MaxArrayLength = MaxMessageSize
                },
                Security = new BasicHttpSecurity
                {
                    Mode = BasicHttpSecurityMode.TransportCredentialOnly,
                    Transport = new HttpTransportSecurity
                    {
                        ClientCredentialType = HttpClientCredentialType.Windows
                    }
                }
            };
            var remoteAddress = new EndpointAddress( string.Format(ServiceUrl, hostname));
            return new CoreServiceClient(basicHttpBinding, remoteAddress); ;
        }

        public void Dispose()
        {
            if (_client.State == CommunicationState .Faulted)
            {
                _client.Abort();
            }
            else
            {
                _client.Close();
            }
        }

        #region Core Service calls
        // Calls to the Core Service client library. Extend the Core service calls as needed.

        public UserData GetCurrentUser()
        {
            return _client.GetCurrentUser();
        }

        public PublishTransactionData [] Publish(string[] itemUris, PublishInstructionData publishInstructionData,
            string[] destinationTargetUris, PublishPriority publishPriority, ReadOptions readOptions)
        {
            return _client.Publish(itemUris, publishInstructionData, destinationTargetUris, publishPriority, readOptions);
        }

        public IdentifiableObjectData Read(string id, ReadOptions readOptions)
        {
            return _client.Read(id, readOptions);
        }
        #endregion Core Service calls
    }
}

using System;
using System.ServiceModel;
using Tridion.ContentManager.CoreService.Client;
using TridionCoreserviceSession; 

...

// Make a Core Service connection
CoreServiceSession session = new CoreServiceSession( "localhost", "[username]" , "[password]");

try
{
    // Display the current user
    Console.WriteLine( "Current user is: " + session.GetCurrentUser().Title);
               
    // Get a page
    PageData page = ( PageData) session.Read("tcm:69-6943-64" , new ReadOptions());
    Console.WriteLine( "Page title: " + page.Title);

    // Publish the page
    string[] pageUris = { "tcm:69-6943-64"};
    string[] destinationTargetUris = { "tcm:0-1-65537"};
    var publishInstruction = new PublishInstructionData
                                {
                                    RenderInstruction = new RenderInstructionData(),
                                    ResolveInstruction = new ResolveInstructionData()
                                };
    PublishTransactionData[] publishTransactions = session.Publish(pageUris, publishInstruction,
                                                                    destinationTargetUris, PublishPriority .Normal,
                                                                    new ReadOptions());
    Console.WriteLine( "Published page; transaction id: " + publishTransactions[0].Id);
}
catch (FaultException <CoreServiceFault> e)
{
    // handle the exception
    Console.WriteLine( String.Format("Error; Core Service said: {0}; {1} ", e.Detail.ErrorCode,
                                    string.Join(", " , e.Detail.Messages)));
}