/// <summary> /// Implement the resolver function to control the resolving /// </summary> /// <param name="item">the item published</param> /// <param name="instruction">Resolve Instructiuons</param> /// <param name="context">Publish Context</param> /// <param name="resolvedItems">Collection of Resolved Items</param> public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet <ResolvedItem> resolvedItems) { // Array of schemas which are supposed to publish independently with out resolved items string[] lookupSchemas = { "Schema Title1", "Schema Title2" }; // If published item is a component and it's type belongs to look up schemas array, start the process if (item is Component) { Component comp1 = (Component)item; if (lookupSchemas.Contains(comp1.Schema.Title)) { // Temp collection of resolved items List <ResolvedItem> tempItems = new List <ResolvedItem>(); // Loop through resolved items foreach (var resolvedItem in resolvedItems) { // If the resolved item is the published item // Allow it to be published by adding to temp items collection if (resolvedItem.Item is Component) { Component compResolved = (Component)resolvedItem.Item; if (resolvedItem.Item.Id == item.Id) { tempItems.Add(resolvedItem); } } } // Delete all resolved items resolvedItems.Clear(); // Add temp items(needs to be published) to resolvedItems collection foreach (ResolvedItem tempResolvedItem in tempItems) { resolvedItems.Add(tempResolvedItem); } } } }
/// <summary> /// Entry point for resolver. /// </summary> /// <param name="item">Item to resolve</param> /// <param name="instruction">Resolve instruction</param> /// <param name="context">Publish context</param> /// <param name="resolvedItems">Final items to resolve</param> public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet <ResolvedItem> resolvedItems) { if (instruction.Purpose != ResolvePurpose.Publish && instruction.Purpose != ResolvePurpose.RePublish || _maxRecurseDepth == 0) { return; } _log.Debug("DXA Custom Resolver started..."); _log.Debug("Loading global app data:"); const string appId = "dxa:CustomResolver"; try { var appData = context.Session.SystemManager.LoadGlobalApplicationData(appId); if (appData != null) { _log.Debug("Found configuration for custom resolver in global app data. Attempting to parse."); string xml = string.Empty; if (appData.TypeId?.StartsWith("XmlElement:") ?? false) { _log.Debug($"Found configuration using XElement appData type."); string @string = Encoding.UTF8.GetString(appData.Data); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(@string); xml = xmlDocument.DocumentElement?.OuterXml; } if (string.IsNullOrEmpty(xml)) { _log.Debug("Assuming string content for xml."); xml = Encoding.Unicode.GetString(appData.Data); } _log.Debug($"xml={xml}"); XElement parsedXml = XElement.Parse(xml); foreach (XElement xe in parsedXml.Elements()) { if (!xe.Name.LocalName.Equals("RecurseDepth")) { continue; } _log.Debug($"Found RecurseDepth value of '{xe.Value}'."); _maxRecurseDepth = int.Parse(xe.Value); break; // only one setting at moment } } else { _log.Debug( $"Custom resolver configuration not found in global app data for '{appId}'. Using default settings."); _maxRecurseDepth = -1; } } catch (Exception e) { _log.Debug( $"Exception occured when reading global app data for application id '{appId}'. Using default settings."); _log.Debug(e.Message); _maxRecurseDepth = -1; } _log.Debug($"Using _maxRecurseDepth={_maxRecurseDepth}"); try { var sourceItem = (RepositoryLocalObject)item; _log.Debug($"Analyzing source item of type: {item.GetType().Name} with id: {item.Id} and title: {item.Title}"); var contextPublication = (Publication)sourceItem.ContextRepository; var filter = new ComponentTemplatesFilter(item.Session) { AllowedOnPage = false, BaseColumns = ListBaseColumns.IdAndTitle }; const string dataPresentationTemplateTitle = "Generate Data Presentation"; var dataPresentationTemplate = contextPublication.GetComponentTemplates(filter).FirstOrDefault( ct => ct.Title == dataPresentationTemplateTitle); if (dataPresentationTemplate == null) { _log.Debug("No 'Generate Data Presentation' component template found. Skipping custom resolver."); return; } var resolved = new HashSet <IdentifiableObject>(); Stopwatch t = new Stopwatch(); t.Start(); HashSet <TcmUri> alreadyResolved = new HashSet <TcmUri>(); foreach (var x in resolvedItems) { alreadyResolved.Add(x.Item.Id); } List <ResolvedItem> resolvedItemsCopy = new List <ResolvedItem>(resolvedItems); foreach (var resolvedItem in resolvedItemsCopy) { foreach (var dataPresentation in ResolveDataPresentations(resolvedItem.Item, dataPresentationTemplate, resolved, 0)) { if (alreadyResolved.Contains(dataPresentation.Item.Id)) { _log.Debug($" > Already resolved item '{dataPresentation.Item.Title}' with id: {dataPresentation.Item.Id}"); continue; } _log.Debug($" > Resolved item '{dataPresentation.Item.Title}' with id: {dataPresentation.Item.Id}"); alreadyResolved.Add(dataPresentation.Item.Id); resolvedItems.Add(dataPresentation); } } t.Stop(); _log.Debug($"DXA Custom Resolver took {t.ElapsedMilliseconds}ms to complete."); } catch (Exception e) { _log.Debug($"Exception occured: {e.Message}"); _log.Debug($"Stacktrace: {e.StackTrace}"); throw; } }