0

I've published a component and verified that it's in the broker database:

Screenshot of Broker DB's CP table

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?

3
  • 2
    The component ID in the code is 916, while the database screenshot shows 917. That couldn't by any chance explain why the second code snippet doesn't work, could it?
    – Quirijn
    Commented Aug 24, 2017 at 15:15
  • That's embarrassing. I'm getting a different error regarding an exception during a web client request but I'll investigate that first.
    – Tim Alonso
    Commented Aug 24, 2017 at 15:30
  • 2
    Component type ID should be 16 not 32, not required to pass var componentUri = "tcm:18-916"
    – Velmurugan
    Commented Aug 24, 2017 at 18:04

1 Answer 1

2

Tcm ID we're incorrect.

private DynamicComponent GetDynamicComponentPresentation()
{
    var componentPresentationFactory = DependencyResolver.Current.GetService<IComponentPresentationFactory>();
    var componentUri = "tcm:18-917-16";
    var componentPresentation = componentPresentationFactory.GetComponentPresentation(componentUri);
    var viewModelFactory = DependencyResolver.Current.GetService<IViewModelFactory>();
    var model = viewModelFactory.BuildViewModel<DynamicComponent>(componentPresentation);
    return model;
}

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.