Copied to clipboard

Flag this post as spam?

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


  • pronto 61 posts 172 karma points
    Jun 15, 2015 @ 13:17
    pronto
    0

    Using Umbraco methods in external .cs file

    Hi everyone,

    I'm using an external .cs file in my App_Code folder for some classes and I just wondered if it's possible to use any Umbraco methods from this file?

    So for example, I have an Image property for one of my classes, and I'd like to return a crop of that image instead of the full url. Is this possible?

    Thanks

  • Maff 141 posts 466 karma points
    Jun 15, 2015 @ 15:10
    Maff
    101

    Hi,

    You can access the Umbraco Helper as long as you''re running your code in the Umbraco application - check out https://our.umbraco.org/documentation/reference/querying/UmbracoHelper/ for more info.

    Here's the code you would need to get the Crop URL of a media item:

    UmbracoHelper umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    
    IPublishedContent media = umbracoHelper.TypedMedia(1);
    
    string cropUrl = media.GetCropUrl("alias");
    

    Thanks,

    Maff

  • pronto 61 posts 172 karma points
    Jun 15, 2015 @ 15:29
    pronto
    0

    Thanks Maff! That works perfectly.

    For anyone else with the same problem, here's what I used:

    using Umbraco.Web;

    using Umbraco.Core.Models;

    UmbracoHelper umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current); IPublishedContent media = umbracoHelper.TypedMedia(imageId); string cropUrl = media.GetCropUrl("My Crop Name");

  • pronto 61 posts 172 karma points
    Jun 15, 2015 @ 15:31
    pronto
    0
    using Umbraco.Web;
    using Umbraco.Core.Models;
    
    UmbracoHelper umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current); 
    IPublishedContent media = umbracoHelper.TypedMedia(imageId);
    string cropUrl = media.GetCropUrl("My Crop Name");
    

    (corrected formatting, can't seem to edit my post!)

  • Maff 141 posts 466 karma points
    Jun 15, 2015 @ 15:31
    Maff
    0

    Cool - glad to help! :)

Please Sign in or register to post replies

Write your reply to:

Draft