Copied to clipboard

Flag this post as spam?

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


  • Bobby Wallace 5 posts 75 karma points
    Oct 09, 2021 @ 21:40
    Bobby Wallace
    0

    Programmatically modify Content - Page Title

    Umbraco version 8.16.0

    I have found some ideas, but still no success.

    One example uses: @inject IContentService Services

    The code fails with: The name 'inject' does not exist in the current

    Link to example: Code

    Another example uses:

    IContentService contentService = new ApplicationContext.Current.Services.ContentService();

    The code fails with:

    1. The type or namespace name 'IContentService' could not be found
    2. The type or namespace name 'ApplicationContext' could not be found

    Link to second example: code

    Ultimately, I would like to resolve all of these reference issues, but I am open to other suggestions as well.

  • Marc Goodson 1653 posts 10865 karma points MVP 6x c-trib
    Oct 17, 2021 @ 18:02
    Marc Goodson
    0

    Hi Bobby

    Your first example with @inject is 'how to do things' for Umbraco V9 which is the most recent release of Umbraco based on .NET 5, which allows you to use the 'inject' keyword...

    ... but if you are running Umbraco 8.16, then it's the second approach and the ContentService you are after.

    https://our.umbraco.com/Documentation/Reference/Management/Services/ContentService/index-v8

    programatically updating textbox example:

    https://our.umbraco.com/Documentation/Fundamentals/Backoffice/Property-Editors/Built-in-Property-Editors/Textbox/index-v8

    The key thing is how to get access to the ContentService, and this depends a little on where you are writing the code, eg a View or Controller or in a custom c#class...

    If you are in a controller or view you can access the ContentServices, from a special 'Services' gateway property... eg

    var contentService = Services.ContentService;

    but if you are in a custom c# class this is possible via dependency injection (the ApplicationContext example would be the V7 way)

    There is an example of how to inject a service here into a c#class:

    https://our.umbraco.com/Documentation/Implementation/Services/index-v8#accessing-core-services-and-helpers-when-there-is-no-umbracocontext-eg-in-a-component-or-c-class

    Secondly when you use class names in .net, eg 'IContentService' or 'ContentService' or 'ApplicationContext' you have to tell your code where to get these things from, and to do this you use 'using statements'...

    ... annoyingly using statements are often missing from examples!

    but I think if you have

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Core.Services.Implement;
    

    at the top of your file, that would cover the things you might be trying to do!

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft