7

I have some Tridion 2011 Core Service code to get a list of all PublishTargets for a given Publication. However, in Tridion 2013 it appears the PublicationTargetData to be null. I am not adding any additional columns to my PublicationTargetsFilterData. But, I feel like if I add the right column I will have the data there. Any idea what column to use?

Here is my working 2011 code:

public static List<PublishTarget> GetPublishTargets(string publicationUri)
{
    //var connection = new Connection("http://localhost/Signalr4Tridion/channel");

    // Caching
    if (pubTargets.ContainsKey(publicationUri))
        return pubTargets[publicationUri];

    List<PublishTarget> localPublishTargets = new List<PublishTarget>();

    string binding = "wsHttp_2013";
    //string binding = "netTcp_2013";

    using (var client = new SessionAwareCoreServiceClient(binding))
    {
        client.Impersonate("Dev\\Admin");// (HttpContext.Current.User.Identity.Name); 
        var readoptions = new ReadOptions();
        var filter = new PublicationTargetsFilterData();
        var allPublicationTargets = client.GetSystemWideList(filter);

        foreach (PublicationTargetData pubTarget in allPublicationTargets)
        {
            if(!IsValidForPub(pubTarget, publicationUri))
                continue;

            PublishTarget pubTargetObj = new PublishTarget()
            {
                Name = pubTarget.Title,
                URI = pubTarget.Id
            };
            localPublishTargets.Add(pubTargetObj);
        }
        pubTargets[publicationUri] = localPublishTargets;
    }

    return localPublishTargets;
}

public static bool IsValidForPub(PublicationTargetData pubTarget, string publicationUri)
{
    bool hasTarget = false;
    foreach (LinkToPublicationData pub in pubTarget.Publications)
    {
        if (pub.IdRef == publicationUri)
            return true;
    }
    return hasTarget;
}

2 Answers 2

4

I guess you meant that PublicationTargetData.Publications is null rather than PublicationTargetData null. It is expected and there is no filter options that will return the Publications. However, the following code block might help you get what you want.

        string publicationUri = "tcm:0-5-1";
        TargetTypesFilterData targetTypesFilter = new TargetTypesFilterData();
        IEnumerable<TargetTypeData> targetTypes = client.GetSystemWideList(targetTypesFilter).OfType<TargetTypeData>();
        ResolveInstructionData resolveInstruction = new ResolveInstructionData();
        IEnumerable<PublishContextData> publishContextDatas = client.ResolveItems(
            new string[]
            {
                publicationUri
            },
            resolveInstruction,
            targetTypes.Select(tt => tt.Id).ToArray(),
            new ReadOptions());

        return publishContextDatas.Select(pc => pc.PublicationTarget);
2
  • Thanks a lot for the code. I am surprised that in 2011 the data is there and then missing in 2013. I assume it's for performance reasons.
    – robrtc
    Commented Feb 20, 2014 at 17:06
  • Indeed, it's performance related. In 2011 we didn't have proper partially loaded Data Objects for all list yet. For those lists, the Core Service would internally fully load each item individually. In 2013 all lists can return partially loaded Data Object, but as a side effect you may get less data (but much faster if you don't need more data than what's partially loodde). Commented Feb 5, 2015 at 19:51
2

I ran into this same problem with 2013 SP1 and I tried the Client.ResolveItems method as mentioned in this thread. The major problem that I ran into when using that method is that it was extremely slow. For me, it would take anywhere from 45 seconds to over a minute.

To fix this, I used a Client.Read on the PublicationTarget.Id if the Publications property is null (which is always will be). In my case, the results went from taking over a minute to 250ms. I rewrote your original code showing what I'm talking about. It's one more line and unfortunately one more call to Tridion.

foreach (PublicationTargetData pubTarget in allPublicationTargets)
{
    var hydratedPubTarget = Client.Read(pubTarget.Id, new ReadOptions()) as PublicationTargetData;

    if(!IsValidForPub(hydratedPubTarget, publicationUri))
        continue;

    PublishTarget pubTargetObj = new PublishTarget()
    {
        Name = hydratedPubTarget.Title,
        URI = hydratedPubTarget.Id
    };
    localPublishTargets.Add(pubTargetObj);
}

When using the Client.ResolveItems method, if the result is taking a long time and other users are trying to use the CMS, on a system using Oracle, you might end up see errors executing stored procedures because the resource is busy. It causes locking issues for everyone.

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.