Quantcast
Channel: Yet Another Tridion Blog
Viewing all articles
Browse latest Browse all 215

Create and Publish Page for Component in Workflow using Core Service

$
0
0
It seems like this use case keeps coming back time and time again. The requirement: when a new Component is in workflow, part of the approval workflow on the Component should be an automatic Page generation (where the Component is placed on the Page) and possibly publishing of the Page for the purpose of previewing the new Component in a Page context. All this while Component is in workflow, so approver can actually see how the Component looks like before approving/rejecting it.

The only issue, as described in the previous post, is that it is not possible to add a v0.x Component to a Page, when the Page is in a child Publication. The solution is to do all this programmatically.

In the following example, I use the Core Service to create a new Page (with values taken from a Folder metadata, for example) and place the v0.x Component on it.

using (CoreServiceSessioncoreService = newCoreServiceSession())
{
    if (component.Version < 1)
    {
        TcmUri pageTcmUri = coreService.CreatePage(title, fileName, sgTcmUri, ptTcmUri, localComponentTcmUri, ctTcmUri);
        PublisherHelper publisherHelper = newPublisherHelper();
        publisherHelper.Publish(pageTcmUri, targetTcmUri, true);
    }
}

The Page is first created, then published (as described in my earlier posts about "How to Publish Stuff Programmatically"). The CoreServiceSession method CreatePage is the following:

publicTcmUriCreatePage(String title, String fileName, TcmUrisgTcmUri, TcmUri ptTcmUri, TcmUri componentTcmUri, TcmUrictTcmUri)
{
    PageData pageData = newPageData();
    pageData.Id = TcmUri.UriNull.ToString();
    pageData.Title = title;
    pageData.FileName = fileName;

    LinkToPageTemplateData linkToPageTemplateData = newLinkToPageTemplateData();
    linkToPageTemplateData.IdRef = ptTcmUri.ToString();
    pageData.PageTemplate = linkToPageTemplateData;
    pageData.IsPageTemplateInherited = false;

    LinkToOrganizationalItemDatalinkToOrganizationalItemData = newLinkToOrganizationalItemData();
    linkToOrganizationalItemData.IdRef = sgTcmUri.ToString();
    LocationInfo locationInfo = newLocationInfo();
    locationInfo.OrganizationalItem = linkToOrganizationalItemData;
    pageData.LocationInfo = locationInfo;

    ComponentPresentationData[] cps = newComponentPresentationData[1];
    cps[0] = newComponentPresentationData();
    LinkToComponentData linkToComponentData = newLinkToComponentData();
    linkToComponentData.IdRef = componentTcmUri.ToString();
    cps[0].Component = linkToComponentData;
    LinkToComponentTemplateDatalinkToComponentTemplateData = newLinkToComponentTemplateData();
    linkToComponentTemplateData.IdRef = ctTcmUri.ToString();
    cps[0].ComponentTemplate = linkToComponentTemplateData;
    pageData.ComponentPresentations = cps;

    IdentifiableObjectData objectData = _coreServiceClient.Create(pageData, ReadOptions);

    returnnewTcmUri(objectData.Id);
}



Viewing all articles
Browse latest Browse all 215

Trending Articles