Jey
Jey

Reputation: 2127

Using SDL Tridion 2011 Core Service to create Components programatically

I have seen some of the questions/answers related to this topic here, however still I am not getting the suggestion which I want. So I am posting my question again here, and I would be thankful for your valuable time and answers.

I would like to create “Component, Page, SG, Publication, Folders “ via programmatically in SDL Tridion Content Manager, and later on, I would like to add programmatically created components in Page and attach CT,PT for that page, and finally would like to publish the page programmatically.

I have done these all the activities in SDL Tridion 2009 using TOM API (Interop DLL's), and I tried these activities in SDL Tridion 2011 using TOM.Net API. It was not working and later on I came to know that, TOM.Net API will not support these kinds of works and it is specifically only for Templates and Event System. And finally I came to know I have to go for Core services to do these kinds of stuffs.

My Questions:

  1. When I create console application to create component programmatically using core service, what are the DLL’s I have to add as reference?
  2. Earlier, I have created the exe and ran in the TCM server, the exe created all the stuffs, can I used the same approach using core services too? Will it work?
  3. Is BC still available or Core Service replaced BC? (BC-Business Connector)
  4. Can anyone send some code snippet to create Component/Page (complete class file will be helpful to understand better)

Upvotes: 5

Views: 4257

Answers (3)

Arjen Stobbe
Arjen Stobbe

Reputation: 1684

From How can i use engine object in my console application

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.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

  1. You will only need to reference Tridion.ContentManager.CoreService.Client.dll. You may want to reference Tridion.Common.dll to get access to some helpful classes such as TcmUri, but it is not needed.
  2. You client program will make an explicit connection with the core service on a machine that you specify. If done properly, you can run the client both on the same machine as the Tridion Content Manager or on a different machine.
  3. The Business Connector is still available, but has been superseded by the Core Service.
  4. Have a look at these links:

Updating Components using the Core Service in SDL Tridion 2011

In SDL Tridion 2011, how can I process metadata on an item using the Core Service?

And the standard documentation on the topic connecting to the Core Service from .NET.

If you need more help with the code, I suggest you show us the code you've already written and explain what isn't working.

Upvotes: 6

Andrey Marchuk
Andrey Marchuk

Reputation: 13483

I will try to answer your questions:

  1. You have to reference Tridion.ContentManager.CoreService.Client and add some stuff to app.config. It's described here

  2. It will work from CM server, as well as from any other machine, provided it can access CoreService

  3. CoreService is replacement for BC. BC is deprecated and will be dropped soon
  4. You will get all the basic info from here.

This should be enough for you to start. If you will have specific problems - post them as a seperate questions.

Upvotes: 4

Related Questions