9

I am writing a core service to publish the entire pages in a structure group. I have written a recursive method to get all pages in a structure group.

I am loading all pages in a list and publishing them.

I am able to get all pages in a list, but unable to publish them.

My entire code is here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tridion.ContentManager.CoreService.Client;
using System.Xml.Linq;
namespace CoreServiceApp
{
    class Program
    {
        static int countPage = 0;
        static List<string> pageArray = new List<string>();
        static void Main(string[] args)
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2011");
            //get the structure group TCM ID
            Console.Write("Enter TCMID of structure Group:");
            string structureGroupTcmID = Console.ReadLine();
            String targetTcmId = "tcm:0-70-65538";
            Program prog = new Program();
            //Call the recursive method to get all pages in structure group
            prog.PagePublish(structureGroupTcmID, client);
            PublishInstructionData pubData = new PublishInstructionData();
            //convert the pages to string
            string[] pages = pageArray.ToArray();
            //publish
            try
            {
                client.Publish(pages, pubData, new[] { targetTcmId }, PublishPriority.Low, new ReadOptions());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Publish: Unable to publish Page"+ex.StackTrace, ex);
            }
            Console.WriteLine("Total No of Pages" + pages.Length);
            client.Close();
        }

        private void PagePublish(string tcmID, SessionAwareCoreServiceClient client)
        {
            ReadOptions readOptions = new ReadOptions();
            OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData();
            var elements = client.GetListXml(tcmID, filter);
            foreach (var element in elements.Elements())
            {
               String pageCheck= element.Attribute("ID").Value;
               String specID = pageCheck.Substring(pageCheck.LastIndexOf('-') + 1, 1);
                //check for structure group
                //If structure group call method again
               if (String.Equals(specID, "4"))
               {
                   PagePublish(pageCheck, client);
               }
               //its page
               else
               {
                   pageArray.Add(pageCheck);
               }

            }

        }
    }
}

I am getting error while publishing.

The exception is

Publish: Unable to publish Page
Server stack trace:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRunt
ime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean on
eway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan tim
eout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCall
Message methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage req
Msg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgDa
ta, Int32 type)
   at Tridion.ContentManager.CoreService.Client.ISessionAwareCoreService.Publish
(String[] ids, PublishInstructionData publishInstruction, String[] targets, Null
able`1 priority, ReadOptions readOptions)
   at Tridion.ContentManager.CoreService.Client.SessionAwareCoreServiceClient.Pu
blish(String[] ids, PublishInstructionData publishInstruction, String[] targets,
 Nullable`1 priority, ReadOptions readOptions)
   at CoreServiceApp.Program.Main(String[] args) in H:\Documents\Visual Studio 2
010\Projects\PagePublish\PagePublish\Program.cs:line 28
Total No of Pages2

And the exception message is

Value cannot be null.
Parameter name: publishInstruction.ResolveInstruction.

Please let me know is there any better way to do this and fix to resolve the exception.

15

Looks like you need to add a resolve instruction object to your publishing.

I did something similar when I wrote the PagePublish Power tool (you can view the full code here: https://code.google.com/p/tridion-2011-power-tools/source/browse/trunk/PowerTools.Model/Services/PagePublisher.svc.cs)

The main bits you need are:

The call to publish :

coreService.Publish(pageIds, GetPublishInstructionData(parameters), parameters.TargetUri, parameters.Priority, new ReadOptions());

The second param is an object of PublishInstructionData, which is return from the following:

private PublishInstructionData GetPublishInstructionData(PagePublisherParameters parameters)
{
    ResolveInstructionData resolveInstruction = new ResolveInstructionData();
    resolveInstruction.IncludeChildPublications = parameters.PublishChildren;
    resolveInstruction.IncludeComponentLinks = parameters.IncludeComponentLinks ? true : false; 
    resolveInstruction.StructureResolveOption = parameters.IncludeStructureGroups ? StructureResolveOption.ItemsAndStructure : StructureResolveOption.OnlyItems;
    resolveInstruction.IncludeWorkflow = parameters.IncludeWorkflow ? true : false;
    resolveInstruction.Purpose = parameters.Republish ? ResolvePurpose.RePublish : ResolvePurpose.Publish;
    RenderInstructionData renderInstruction = new RenderInstructionData();
    renderInstruction.RenderMode = RenderMode.PreviewDynamic;

    PublishInstructionData instruction = new PublishInstructionData();
    instruction.RollbackOnFailure = true;
    instruction.MaximumNumberOfRenderFailures = 1;
    instruction.ResolveInstruction = resolveInstruction;
    instruction.RenderInstruction = renderInstruction;
    return instruction;
}

Hope this helps. It helped me, i've seen a few sketchy bits in the page publisher that need a bit of refactoring :)

  • Thank you for replying. Could you also help me to create parameters. – Patan May 15 '13 at 5:47
  • 1
    Editing PublishInstructionData did the job. PublishInstructionData pubData = new PublishInstructionData { ResolveInstruction = new ResolveInstructionData() { IncludeChildPublications = true }, RenderInstruction = new RenderInstructionData() }; – Patan May 15 '13 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.