4
 public void PublishComponent()
    {
        string domainName = System.Configuration.ConfigurationSettings.AppSettings["domainName"];
        string userName = System.Configuration.ConfigurationSettings.AppSettings["userName"];
        string passowrd = System.Configuration.ConfigurationSettings.AppSettings["passowrd"];
        timestamp = DateTime.Now.ToString("dd:MM:yy h:mm:ss tt").Replace(":", "_").Replace(" ", "");
        string content = string.Empty;
        client = CoreServiceClientFactory.GetClient();
        string[] pageList = System.IO.File.ReadAllLines(Environment.CurrentDirectory + "\\" + "pagepublish.txt");
        //string[] pageList = System.IO.File.ReadAllLines(Environment.CurrentDirectory + "\\" + "publish.txt");
        // string[] publishTargets = new string[] { "live", "preview" };
        string[] publishTargets = new string[] { "tcm:0-1109-1" };
        // string[] publishTargets = { "tcm:0-30-1" };
        int i = 1;
        int breakValue = 0;
        foreach (var tcmid in pageList)
        {
            string componenttcmid = tcmid;
            string[] page = new string[] { tcmid };
            Console.Write("Start- " + componenttcmid + " - " + i + " of " + pageList.Count());
            Console.WriteLine();

            PublishInstructionData publishInstructionData = new PublishInstructionData
            {
                RenderInstruction = new RenderInstructionData(),
                ResolveInstruction = new ResolveInstructionData()
            };
            if (breakValue == 10)
            {
                breakValue = 0;
                Thread.Sleep(5000);
            }
            try
            {
                client.Publish(page, publishInstructionData, publishTargets, PublishPriority.Normal, new ReadOptions());
                Console.WriteLine(componenttcmid + "Published");
                Log(componenttcmid, "publish");
                Console.WriteLine();

            }
            catch (Exception ex)
            {
                Log(componenttcmid + " - " + ex.Message + ex.StackTrace, "publisherror");
                Log(componenttcmid, "failedpublish");
                //client.Update(ComponentContent, new ReadOptions());
                Console.WriteLine("Failed" + componenttcmid);
            }
            breakValue++;
            i++;
        }
    }

Error:

Unable to cast object of type 'Tridion.ContentManager.CommunicationManagement.Publication' to type 'Tridion.ContentManager.CommunicationManagement.PublishingTarget'

2 Answers 2

3
string[] publishTargets = new string[] { "tcm:0-1109-1" };

This is the problem in your code. Id you are sending is definitely TcmId of publication, not publication target. TcmId of Publication Target ends up with 65537, so TcmId looks like this: tcm:0-XXX-65537.

This is the issue you observe when you try to send ID of Publication instead of Publishing target. To get Id of the target, check id in Administration tab: enter image description here

1

Marko is correct about the error and what is causing it, but you can only check for the Publication Target the way he describes if you are on version 2013 SP1 at the latest, or a greater version if you have legacy publishing enabled.

If you are on Web 8 and up, you can also publish to:

  • Purpose, which is a string value. To see which Purposes you have open the relevant BPT in your Publication
  • Target Type. To get the ID of the Target Type, open the publish dialog and hover on the Target's name.

Both approaches are illustrated below:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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