How can i use engine object in my console application

谁都会走 提交于 2020-01-14 14:01:28

问题


"How can i use engine in my console application"

I shouldn't use the ITemplate-interface and Transform-Method.

I am using Tridion 2011

Could anyone please suggest me.


回答1:


You can't. The Engine class is part of the TOM.NET and that API is explicitly reserved for use in:

  1. Template Building Blocks
  2. Event Handlers

For all other cases (such as console applications) you should use the Core Service.

There are many good questions (and articles on other web sites) already:

  • https://stackoverflow.com/search?q=%5Btridion%5D+core+service
  • http://www.google.com/#q=tridion+core+service

If you get stuck along the way, show us the relevant code+configuration you have and what error message your get (or at what step you are stuck) and we'll try to help from there.




回答2:


From a console application you should use the Core Service. I wrote a small example using the Core Service to search for items in the content manager.

Console.WriteLine("FullTextQuery:");

var fullTextQuery = Console.ReadLine();

if (String.IsNullOrWhiteSpace(fullTextQuery) || fullTextQuery.Equals(":q", StringComparison.OrdinalIgnoreCase))
{
    break;
}

Console.WriteLine("SearchIn IdRef:");

var searchInIdRef = Console.ReadLine();

var queryData = new SearchQueryData
                    {
                        FullTextQuery = fullTextQuery,
                        SearchIn = new LinkToIdentifiableObjectData
                                        {
                                            IdRef = searchInIdRef
                                        }
                    };

var results = coreServiceClient.GetSearchResults(queryData);
results.ToList().ForEach(result => Console.WriteLine("{0} ({1})", result.Title, result.Id));

Add a reference to Tridion.ContentManager.CoreService.Client to your Visual Studio Project.

Code of the Core Service Client Provider:

public interface ICoreServiceProvider
{
    CoreServiceClient GetCoreServiceClient();
}

public class CoreServiceDefaultProvider : ICoreServiceProvider
{
    private CoreServiceClient _client;

    public CoreServiceClient GetCoreServiceClient()
    {
        return _client ?? (_client = new CoreServiceClient());
    }
}

And the client itself:

public class CoreServiceClient : IDisposable
{
    public SessionAwareCoreServiceClient ProxyClient;

    private const string DefaultEndpointName = "netTcp_2011";

    public CoreServiceClient(string endPointName)
    {
        if(string.IsNullOrWhiteSpace(endPointName))
        {
            throw new ArgumentNullException("endPointName", "EndPointName is not specified.");
        }

        ProxyClient = new SessionAwareCoreServiceClient(endPointName);
    }

    public CoreServiceClient() : this(DefaultEndpointName) { }

    public string GetApiVersionNumber()
    {
        return ProxyClient.GetApiVersion();
    }

    public IdentifiableObjectData[] GetSearchResults(SearchQueryData filter)
    {
        return ProxyClient.GetSearchResults(filter);
    }

    public IdentifiableObjectData Read(string id)
    {
        return ProxyClient.Read(id, new ReadOptions());
    }

    public ApplicationData ReadApplicationData(string subjectId, string applicationId)
    {
        return ProxyClient.ReadApplicationData(subjectId, applicationId);
    }

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

When you want to perform CRUD actions through the core service you can implement the following methods in the client:

public IdentifiableObjectData CreateItem(IdentifiableObjectData data)
{
    data = ProxyClient.Create(data, new ReadOptions());
    return data;
}

public IdentifiableObjectData UpdateItem(IdentifiableObjectData data)
{
    data = ProxyClient.Update(data, new ReadOptions());
    return data;
}

public IdentifiableObjectData ReadItem(string id)
{
    return ProxyClient.Read(id, new ReadOptions());
}

To construct a data object of e.g. a Component you can implement a Component Builder class that implements a create method that does this for you:

public ComponentData Create(string folderUri, string title, string content)
{
    var data = new ComponentData()
                    {
                        Id = "tcm:0-0-0",
                        Title = title,
                        Content = content,
                        LocationInfo = new LocationInfo()
                    };

    data.LocationInfo.OrganizationalItem = new LinkToOrganizationalItemData
    {
        IdRef = folderUri
    };

using (CoreServiceClient client = provider.GetCoreServiceClient())
{
    data = (ComponentData)client.CreateItem(data);
}

    return data;
}

Hope this gets you started.



来源:https://stackoverflow.com/questions/11473513/how-can-i-use-engine-object-in-my-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!