4

I'm adding the publication title in config file and from there i want to call that title in the resolved item, but in the below code I'm able to read the Pub ID but it will change across the environment for that reason i want to get the publication title/name.

    public class ChildPublicationsOnlyResolver : IResolver
        {
     private const string ConfigFile= "ChildPublicationsOnlyResolver.config";
            /// <summary>
            /// Website Structure Publication TCMURI in config file
            /// </summary>
     **private const string WebsiteStructurePublicationFieldName = "IgnorePublications";
            private readonly List<TcmUri> _websiteStructurePublicationUri;**
            public ChildPublicationsOnlyResolver()
            {
       _websiteStructurePublicationUri=GetWebsiteStructurePublicationUri();
            }        /// For Publish and UnPublish, remove all items from the website structure Publication from the list.
            /// Website structure Publication URI is read from the config file.
            /// </summary>
                    /// <param name="resolvedItems">List of items that are currently to be rendered and published (added by previous resolvers in the chain)</param>
   public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet<ResolvedItem> resolvedItems)
            {
            List<ResolvedItem> itemsToRemove = new List<ResolvedItem>();
                StringBuilder infoMessage = new StringBuilder();
                infoMessage.AppendLine(string.Format("Removed the following items from a {0} Transaction to {1}:", instruction.Purpose,          context.PublicationTarget.Title));
                // check for items from the list of publications to ignore (these do not need to be published or unpublished) 
                foreach (ResolvedItem resolvedItem in resolvedItems)
                {
                    // mark all items from IgnorePublications for removal
      **var publicationUri = resolvedItem.Item.Id.GetContextRepositoryUri();    var publicationTitle = resolvedItem.Session.GetObject(publicationUri).Title;**
           if (WebsiteStructurePublicationFieldName.Contains(publicationTitle))
                    {
                        itemsToRemove.Add(resolvedItem);
                    }

                }          
  foreach (ResolvedItem itemToRemove in itemsToRemove)
                {
                    infoMessage.AppendLine(string.Format("{0}: {1} ({2})", itemToRemove.Item.Id.ItemType, itemToRemove.Item.Title, itemToRemove.Item.Id));
                    resolvedItems.Remove(itemToRemove);
                }
                if (itemsToRemove.Count > 0)
                {
                    // log info mesage about which items have been removed (optionally this can be logged as a warning to stand out in the logfile)
                    Logger.Write(infoMessage.ToString(), "ChildPublicationsOnlyResolver", LoggingCategory.General, TraceEventType.Warning);
                }
            }
3

Untested, but try:

var publicationUri = resolvedItem.Item.Id.GetContextRepositoryUri();
var publicationTitle = resolvedItem.Session.GetObject(publicationUri).Title;

UPDATE:

As in:

// _websiteStructurePublicationUri is now _websiteStructurePublicationTitles
// (a List<String>) as you want it to be environment agnostic
foreach (ResolvedItem resolvedItem in resolvedItems)
{
    var publicationUri = resolvedItem.Item.Id.GetContextRepositoryUri();
    var publicationTitle = resolvedItem.Session.GetObject(publicationUri).Title;
    // mark all items from IgnorePublications for removal
    if (_websiteStructurePublicationTitles.Contains(publicationTitle))
    {
        itemsToRemove.Add(resolvedItem);
    }
    // remove all items that we need to discard
    foreach (ResolvedItem itemToRemove in itemsToRemove)
    {
        infoMessage.AppendLine(string.Format("{0}: {1} ({2})", itemToRemove.Item.Id.ItemType, itemToRemove.Item.Title, itemToRemove.Item.Id));
        resolvedItems.Remove(itemToRemove);
    }
    if (itemsToRemove.Count > 0)
    {
        // log info mesage about which items have been removed (optionally this can be logged as a warning to stand out in the logfile)
        Logger.Write(infoMessage.ToString(), "ChildPublicationsOnlyResolver", LoggingCategory.General, TraceEventType.Warning);
    }
}
  • I'm not able to pass the publicationUri as in the publicaitontitle. In my code at the top i have declared variable private readonly List<TcmUri> _websiteStructurePublicationUri; if i pass _websiteStructurePublicationUri in the var publicationTitle = resolvedItem.Session.GetObject(_websiteStructurePublicationUri ).Title; throwing me overload error – k951 Feb 18 '16 at 10:30
  • Use resolvedItem.Item.Id.GetContextRepositoryUri() instead of _websiteStructurePublicationUri. Transform List<TcmUri> to List<string> and compare you list with publication titles not TcmUris – Ognjen Ivetic Feb 18 '16 at 10:44
  • Why are you trying to pass _websiteStructurePublicationUri in? Look at both lines... I'm showing you how to get the publication title from the resolved item, which you can then match with your config file – David Forster Feb 18 '16 at 10:49
  • @DavidForster I have updated my code above in the question can you go through and pls let me know the changes i have done is correct or not. Thanks in advance – k951 Feb 18 '16 at 11:21
  • private const string ConfigFile = "ChildPublicationsOnlyResolver.config"; private const string WebsiteStructurePublicationFieldName = "IgnorePublications"; private readonly List<TcmUri> _websiteStructurePublicationUri;public ChildPublicationsOnlyResolver() { foreach (ResolvedItem resolvedItem in resolvedItems) { var publicationUri = resolvedItem.Item.Id.GetContextRepositoryUri(); var publicationTitle = resolvedItem.Session.GetObject(publicationUri).Title; if (WebsiteStructurePublicationFieldName.Contains(publicationTitle)) { itemsToRemove.Add(resolvedItem);} – k951 Feb 18 '16 at 11:41
1

Try something like this

Publication publication = (Publication)engine.GetObject(resolvedItem.Item.Id.GetContextRepositoryUri());
string publicationTitle = publication.Title;
  • 4
    is Engine available in a custom resolver? – David Forster Feb 18 '16 at 10:46
  • You got me there David :) – Ognjen Ivetic Feb 18 '16 at 10:55
  • @DavidForster we want to check from each resolved items the publication name is present in the config or not . whatever the above code you have mentioned it will work for this scenario also right – k951 Feb 18 '16 at 13:24

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.