Copied to clipboard

Flag this post as spam?

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


  • Splinx 49 posts 184 karma points
    Mar 04, 2018 @ 15:02
    Splinx
    0

    Accessing Terratype.Models from App_Code folder

    Firstly, thank you for an amazing plugin.

    Like others here, I to have found myself upgrading an existing project from your previous AngularMaps to the new Terratype maps.

    The issue I have is that the previous coders have a bunch of code in the App_code folder that creates a large JSON file containing lots of coordinates based upon (previously) Angular Maps locations.

    The existing code simply used "GetPropertyValue" to retun the laat and lng in the string format "xxxxxx,yyyyyyy".

    Terratype, however doesn't support this return and I can see in a Visual Studio watch that it is returning a type of "Terratype.Models.Model".

    The trouble is, I cannot seem to work out what code I need in place to be able to get at all of the enhanced properties inside this from the "App_Code" folder.

    Could a kindly soul please help me out here.

    Thanks in advance, Splinx

  • Splinx 49 posts 184 karma points
    Mar 04, 2018 @ 19:05
    Splinx
    0

    Hi Guys,

    This is my really hacky temporary fix until I get some help from someone who knows how to do this the correct way.

    I think that I need to import/use something or perhaps describe the TerraType model in some way, but when I use the model from the excellent documentation, I have lots of errors. I'm sure that this is because my code is in the App_Code folder and not within a view.

    Please help! :)

    Cheers, Splinx

    private string GetCoordinates(object terraTypeMap)
                {
                    Type t = terraTypeMap.GetType();
                    PropertyInfo[] pia = t.GetProperties();
                    //string message = "";
                    foreach (PropertyInfo b in pia)
                    {
                        if (b.Name == "Position")
                        {
                            return b.GetValue(terraTypeMap).ToString();
                        }
                        //message += String.Format("Name : {0}, Value : {1}\r\n", b.Name, b.GetValue(coords, null));
                    }
    
                    return null;
                }
    
  • Matthew Wise 263 posts 1239 karma points MVP 2x c-trib
    Mar 04, 2018 @ 19:23
    Matthew Wise
    0

    Hey,

    What errors are you getting?

    Matt

  • Splinx 49 posts 184 karma points
    Mar 04, 2018 @ 19:51
    Splinx
    0

    Hey Matt,

    The code I am upgrading previoulsy had a line like this:

    maplocation = GetCoordinates(o.GetPropertyValue("officeLocation"))
    

    Where o is an item from:

     IEnumerable<IPublishedContent> allOffices = umbracoHelper
    .TypedContentAtRoot()
    .SelectMany(root => root.Descendants())
    .Where(x => x.DocumentTypeAlias == "OfficeDocType")
    .ToList();
    

    So, you see, this code would have pulled back the simple coordinates previously from the Google Angular Maps.

    With the new TerraType map object, these values (amongst many other useful pieces of data) are all hidden within properties of the object.

    The trouble is, I do not understand how to get at them because my code simply doesn't understand what the TerraType object is :(

    I have tried using syntax from the documentation and other coders posts but everything I try fails simple because I don't have access to content.Model and the Terratype object.

    I basically want to know what the syntax is (and any using statements) required to get at the properties here:

    enter image description here

    Please accept my apologies in advance if I'm just being an idiot ;)

    Cheers, Splinx

  • Splinx 49 posts 184 karma points
    Mar 04, 2018 @ 20:04
    Splinx
    0

    To clarify...

    I've gone through the documentation here: https://github.com/Joniff/Terratype/blob/master/docs/manual.pdf

    I've tried to include the Terratype.Models.Model within my custom class (which resides in the App_Code folder) but I fear that I'm heading in the wrong direction here as I just get more type unknown errors from the Terratype model itself.

    I'm certain that this is more of a .Net C# experience thing than any issue with the excellent plugin - I just can't figure this one out!

    Cheers, Splinx

  • Matthew Wise 263 posts 1239 karma points MVP 2x c-trib
    Mar 05, 2018 @ 08:22
    Matthew Wise
    1

    This should return the correct model.

    o.GetPropertyValue<Terratype.Models.Model>("officeLocation")
    

    Also one side note is

     IEnumerable<IPublishedContent> allOffices = umbracoHelper
    .TypedContentAtRoot()
    .SelectMany(root => root.Descendants())
    .Where(x => x.DocumentTypeAlias == "OfficeDocType")
    .ToList();
    

    could be faster using

     IEnumerable<IPublishedContent> allOffices = umbracoHelper
    .TypedContentAtXpath("//OfficeDocType") .ToList();
    

    Using Descendants is an expensive call on as it loads the whole site into memory whereas the above uses XPath and only loads the resulting OfficeDocType

    Hope this helps

    Matt

  • Splinx 49 posts 184 karma points
    Mar 05, 2018 @ 08:58
    Splinx
    0

    Thanks for coming back to me Matt, I reallyappreciate it.

    I'm certain that your code is right as it matches to many examples in the Umbraco documentation and forum posts... the issue is that I cannot seem to use this format in the app_Code folder in the classes set up by the previous developers.

    I have previously attempted to get the Terratype object as you have given in your example but "Terratype" is highlighted by intellisense with the error: The type of namespace "Terratype" could not be found....

    Am I missing something really basic here?


    Thanks for the tip on the TypedContentAtXPath. I thought that I could not get this working at first (since there was a typo in your example (lowercase "p" in TypedContentAtXpath). I edited my initial reply accordingly.

    Thanks again buddy, Splinx

  • Splinx 49 posts 184 karma points
    Mar 05, 2018 @ 09:36
    Splinx
    0

    Some further insight that may help reveal my mistake...

    I tried to see if I could describe the Terratype model manually by creating models for it based on the source code.

    This had the effect of allowing me to use the statement you provided of

    var terraTypeCoords2 = myOffice.GetPropertyValue<Terratype.Models.Model>("officeLocation");
    

    BUT...

    then resulted in the compiler error below:

    Compiler Error Message: CS0433: The type 'Terratype.Models.Model' exists in both '...\AppData\Local\Temp\Temporary ASP.NET Files\vs\a63db768\d29af7a0\assembly\dl3\e9aebd0f\6ff579f2_63b4d301\UmbracoCMS.DLL' and '...\AppData\Local\Temp\Temporary ASP.NET Files\vs\a63db768\d29af7a0\assembly\dl3\e8173b13\007242ec_6d94d301\Terratype.DLL'
    

    I'm thinking that I need to reference the Terratype DLL somehow, right?

    It's in the project (imported using nuget): enter image description here

    ...but, I cannot get a using statement to this at the top of my class :( enter image description here

    This must be something I have (or perhaps haven't done) or a complete misunderstanding of .Net.

    Cheers,

    Splinx

  • Splinx 49 posts 184 karma points
    Mar 05, 2018 @ 09:49
    Splinx
    100

    Okay.... I think I have a solution but I'm not sure that I am knowledgeable enough to understand how....

    My fix was to...

    1. Used Nuget package manager to uninstall Terratype version 1.0.16
    2. Used Nuget package manager to install Terratype version 1.0.15
    3. Restart Visual studio and clean/rebuild project.

    Everything now works and now this is easy to get working from here!

    :-)

    Is it possible that there's something wrong with 1.0.16 or is this just something screwy with my local environment or install process?

    Cheers,

    Splinx

Please Sign in or register to post replies

Write your reply to:

Draft