public bool TryGetComponent(string componentUri, out IComponent component) { component = null; TcmUri uri = new TcmUri(componentUri); Tridion.ContentDelivery.DynamicContent.ComponentPresentationFactory cpFactory = new ContentDelivery.DynamicContent.ComponentPresentationFactory(PublicationId); Tridion.ContentDelivery.DynamicContent.ComponentPresentation cp = null; IList cps = cpFactory.FindAllComponentPresentations(uri.ItemId); if (cps.Count > 0 && cps[0] != null) { cp = (Tridion.ContentDelivery.DynamicContent.ComponentPresentation)cps[0]; } if (cp != null) { var componentContent = new XmlDocument(); componentContent.LoadXml(cp.Content); var serializer = new XmlSerializer(typeof(Component)); using (var reader = new XmlNodeReader(componentContent.DocumentElement)) { component = (IComponent)serializer.Deserialize(reader); } return(true); } return(false); }
public string GetContent(string uri, string templateUri = "") { LoggerService.Debug(">>GetContent({0})", LoggingCategory.Performance, uri); TcmUri tcmUri = new TcmUri(uri); TcmUri templateTcmUri = new TcmUri(templateUri); T.ComponentPresentationFactory cpFactory = GetComponentPresentationFactory(tcmUri.PublicationId); T.ComponentPresentation cp = null; if (!String.IsNullOrEmpty(templateUri)) { cp = cpFactory.GetComponentPresentation(tcmUri.ItemId, templateTcmUri.ItemId); if (cp != null) { return(cp.Content); } } if (!string.IsNullOrEmpty(selectByComponentTemplateId)) { cp = cpFactory.GetComponentPresentation(tcmUri.ItemId, Convert.ToInt32(selectByComponentTemplateId)); if (cp != null) { LoggerService.Debug("<<GetContent({0}) - by ct id", LoggingCategory.Performance, uri); return(cp.Content); } } if (!string.IsNullOrEmpty(selectByOutputFormat)) { cp = cpFactory.GetComponentPresentationWithOutputFormat(tcmUri.ItemId, selectByOutputFormat); if (cp != null) { LoggerService.Debug("<<GetContent({0}) - by output format", LoggingCategory.Performance, uri); return(cp.Content); } } LoggerService.Debug("GetContent: about to get component presentations with Highst Priority for {0}", LoggingCategory.Performance, tcmUri.ToString()); cp = cpFactory.GetComponentPresentationWithHighestPriority(tcmUri.ItemId); LoggerService.Debug("GetContent: get component presentations with Highst Priority for {0}", LoggingCategory.Performance, tcmUri.ToString()); if (cp != null) { return(cp.Content); } //foreach (Tridion.ContentDelivery.DynamicContent.ComponentPresentation _cp in cps) //{ // if (_cp != null) // { // LoggerService.Debug("<<GetContent({0}) - find all", LoggingCategory.Performance, uri); // return _cp.Content; // } //} LoggerService.Debug("<<GetContent({0}) - not found", LoggingCategory.Performance, uri); return(string.Empty); }
public string GetContent(string uri, string templateUri = "") { TcmUri tcmUri = new TcmUri(uri); TcmUri templateTcmUri = new TcmUri(templateUri); Tridion.ContentDelivery.DynamicContent.ComponentPresentationFactory cpFactory = new ComponentPresentationFactory(PublicationId); Tridion.ContentDelivery.DynamicContent.ComponentPresentation cp = null; if (!String.IsNullOrEmpty(templateUri)) { cp = cpFactory.GetComponentPresentation(tcmUri.ItemId, templateTcmUri.ItemId); if (cp != null) { return(cp.Content); } } if (!string.IsNullOrEmpty(selectByComponentTemplateId)) { cp = cpFactory.GetComponentPresentation(tcmUri.ItemId, Convert.ToInt32(selectByComponentTemplateId)); if (cp != null) { return(cp.Content); } } if (!string.IsNullOrEmpty(selectByOutputFormat)) { cp = cpFactory.GetComponentPresentationWithOutputFormat(tcmUri.ItemId, selectByOutputFormat); if (cp != null) { return(cp.Content); } } IList cps = cpFactory.FindAllComponentPresentations(tcmUri.ItemId); foreach (Tridion.ContentDelivery.DynamicContent.ComponentPresentation _cp in cps) { if (_cp != null) { if (_cp.Content.Contains("<Component")) { return(_cp.Content); } } } return(string.Empty); }