csharp 使用SDL Tridion Core Service发布页面。此示例显示如何在没有配置文件和使用B的情况下使用Core Service API

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 使用SDL Tridion Core Service发布页面。此示例显示如何在没有配置文件和使用B的情况下使用Core Service API相关的知识,希望对你有一定的参考价值。

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

以上是关于csharp 使用SDL Tridion Core Service发布页面。此示例显示如何在没有配置文件和使用B的情况下使用Core Service API的主要内容,如果未能解决你的问题,请参考以下文章