Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mus'ab 157 posts 384 karma points notactivated
    Dec 11, 2019 @ 12:30
    Mus'ab
    0

    How to use umbraco.contnet() in Icomponent ?

    Hi

    i am going to write a method that implement on save and publish so i write a class that inherit from I Component

    public class MyComponent : IComponent

    and start to write my method

    but i cant using umbraco.content() neither umbraco.media() in this class enter image description here

    how can i solve this problem ?

  • Marc Goodson 1700 posts 11285 karma points MVP 7x c-trib
    Dec 11, 2019 @ 20:05
    Marc Goodson
    1

    Hi Mus'ab

    Have a look at this example in the documentation.

    https://our.umbraco.com/Documentation/Implementation/Services/#accessing-published-content-outside-of-a-http-request

    Essentially the 'Umbraco' in Umbraco.Content and Umbraco.Media is an instance of the UmbracoHelper class provided for convenience in Views, Controllers etc - but no so in a Component, where it cannot be guaranteed that an UmbracoContext will exist, the UmbracoHelper relies on their being an UmbracoContext.

    In the example above you can see the use of the UmbracoContextFactory to ensure the context exists, to get a reference to the UmbracoContext to allow you to query the content or media caches.

     IPublishedContentCache contentCache = umbracoContextReference.UmbracoContext.Content;
    IPublishedContent soonToBeUnPublishedItem = contentCache.GetById(item.Id);
    

    if that helps?

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft