I've published a component and verified that it's in the broker database:
I'd like to retrieve this dynamic component presentation on the web app using DD4T.
My current attempt:
private DynamicContentViewModel GetDynamicComponentPresentation()
{
var publicationResolver = DependencyResolver.Current.GetService<IPublicationResolver>();
var publicationId = publicationResolver.ResolvePublicationId();
const string schemaId = "tcm:3-907-8";
var queryParameters = new ExtendedQueryParameters
{
PublicationId = publicationId,
QuerySchemas = new[] { schemaId }
};
var componentPresentationFactory = DependencyResolver.Current.GetService<IComponentPresentationFactory>();
var componentPresentations = componentPresentationFactory.FindComponentPresentations(queryParameters);
var dynamicContent = new List<DynamicContentViewModel>();
var viewModelFactory = DependencyResolver.Current.GetService<IViewModelFactory>();
foreach (var componentPresentation in componentPresentations)
{
var model = viewModelFactory.BuildViewModel<DynamicContentViewModel>(componentPresentation);
dynamicContent.Add(model);
}
return dynamicContent.FirstOrDefault();
}
This is returning an 0 component presentation. I've attempted to use the schema ID of DynamicComponent.
Another approach:
private DynamicComponent GetDynamicComponentPresentation()
{
var componentPresentationFactory = DependencyResolver.Current.GetService<IComponentPresentationFactory>();
var componentUri = "tcm:18-916-32";
var componentPresentation = componentPresentationFactory.GetComponentPresentation(componentUri);
var viewModelFactory = DependencyResolver.Current.GetService<IViewModelFactory>();
var model = viewModelFactory.BuildViewModel<DynamicComponent>(componentPresentation);
return model;
}
In the second approach, it's throwing an error. After having a look at "DD4T.Factories.ComponentPresentationFactory.GetComponentPresentation(String componentUri, StringtemplateUri)", I found that it's throwing a ComponentPresentationNotFoundException
. Is there anything else that I need to do?