Copied to clipboard

Flag this post as spam?

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


  • Angelo 111 posts 260 karma points
    Apr 16, 2019 @ 15:42
    Angelo
    0

    UmbracoContext.Current.PageId.Value

    sorry for the stupid question but

    i have an extension that was something like that :

      public static int GetMultiLanguageRelatedNode(this IContent content, int nodeId = 0) 
      {   
                       var a =    UmbracoContext.Current.PageId.Value;
                       return a;  
      }
    

    it works perfect on U7 but why the hell in U8 "UmbracoContext.Current.PageId.Value" does not work ? im i doing something wrong ?

    always saying that "UmbracoContext" does not exists in the current context but i checked the documentation and it says that nothing changed in this method ... or im i wrong ?

    thank you

    Angelo

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 16, 2019 @ 16:20
    Alex Skrypnyk
    0

    Hi Angelo

    Can you show all method "GetMultiLanguageRelatedNode"?

    Alex

  • Angelo 111 posts 260 karma points
    Apr 16, 2019 @ 16:39
    Angelo
    1

    Hello Alex thans for the answer :) here it goes:

    public static IContent GetMultiLanguageRelatedNode(this IContent content, int nodeId = 0)
            {   
                string sLanguage = Cultures.GetCurrentCultureName().ToLower();
                int iPageId = nodeId.IfZeroReturn(UmbracoContext.Current.PageId.Value);
                var relationService = ApplicationContext.Current.Services.RelationService;
                var relations = relationService.GetByParentOrChildId(iPageId).Where(r => r.RelationType.Alias == "relateDocumentOnCopy");
                IEnumerable<int> ids = relations.SelectMany(r => new[] { r.ChildId, r.ParentId }).Distinct();
                var contentService = ApplicationContext.Current.Services.ContentService;
                var nodes = contentService.GetByIds(ids);
                foreach (var node in nodes)
                {
                    if (sLanguage == Contexts.GetLanguageByPageId(node.Id).ToLower())
                    {
                        content = node;
                        break;
                    }
                }
    
                return content;
            }
    

    some of the methods are mine ... but "ApplicationContext" and "UmbracoContext" dont work :(

    let me say also that i only was able to run U8 with .NET 4.7.2 i dont know if it helps ...

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 16, 2019 @ 21:24
    Alex Skrypnyk
    0

    U8 works only with .NET 4.7.2 it's fine

    Probably, you need to rewrite this method totally.

    As I see this method uses ContentService, it looks like not the best performance. When do you use this method?

    in U8 there is absolutely different support of multi-languages so maybe you need to rethink an architecture?

  • Angelo 111 posts 260 karma points
    Apr 17, 2019 @ 07:54
    Angelo
    0

    Alex let me reformulate the question

    the problem its not the method it self ...because i know that the language engine has changed ! ... the problem is not getting access to ApplicationContext and UmbracoCOntext

    so ... if i have a static class with several extensions ...how can i get the contexts without passing as parameter the service it self ?

    in the U8 documentation it says in the last example that we can do like that :

    IUserService us = ApplicationContext.Current.Services.UserService
    

    https://our.umbraco.com/documentation/Reference/Management/Services/UserService/

    the problem as i said its not the method, its the static instatiation inside a static class ( ApplicationContext.Current.Services.UserService) not being recognized

    i dont know if i was more precise now ...

    than you

    AS

  • Dave de Moel 122 posts 574 karma points c-trib
    Apr 17, 2019 @ 14:05
    Dave de Moel
    1

    You will want to use the following namespace in Umbraco 8:

    using Umbraco.Core.Composing
    

    Then you can access services like:

    Current.Services.UserService
    

    Umbraco 8 uses dependency injection and on top of that a composition layer. The above code allows you to get services (among other things) straight from the IoC container.

    btw: The documentation sadly is not yet up to date with Umbraco 8. Whenever looking through the documentation look for the note at the top of the page that says "This article has not yet been verified against Umbraco 8.". That basically means that possibly something changes that impact the accuracy of the article. Sadly in your case you ran into one of the more massive (but good!) changes.

  • Angelo 111 posts 260 karma points
    Apr 17, 2019 @ 14:26
    Angelo
    0

    Hello Dave fantastic ... im elucidated now :)

    is there a way to do this in the "correct way" ?

    thank you

    AS

  • Dave de Moel 122 posts 574 karma points c-trib
    Apr 17, 2019 @ 14:41
    Dave de Moel
    2

    I don't think there really is a way to not do this in the correct way :P

    Just add the namespace to the using at the top of your file and change all occurrences of "ApplicationContext.Current.Services.---" to "Current.Services.---" and you should be good to go.

    Also instead of using "UmbracoContext.Current.PageId.Value" you can use "Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem.Id"

    Having that said, you want to add "Umbraco.Web.Composing" as the using on top of the page instead of "Umbraco.Core.Composing" as they both provide the same services, but Umbraco.Web.Composing also exposes the UmbracoHelper.

  • Angelo 111 posts 260 karma points
    Apr 17, 2019 @ 15:00
    Angelo
    0

    ok

    i read this article https://our.umbraco.com/documentation/reference/Common-Pitfalls/

    this is why im asking ..

    but i will give it a try ... thank you a lot Dave ! :)

    Angelo

  • Dave de Moel 122 posts 574 karma points c-trib
    Apr 17, 2019 @ 15:08
    Dave de Moel
    0

    Ah okay,

    Well in that article it is recommended you use UmbracoContext.Current which basically the same as what I suggested above. The difference is that in the recommendation above you do use the IoC container that Umbraco now utilizes :)

  • Angelo 111 posts 260 karma points
    Apr 18, 2019 @ 08:22
    Angelo
    0

    ok dave thank you a lot :)

    all the best

    Angelo

Please Sign in or register to post replies

Write your reply to:

Draft