Copied to clipboard

Flag this post as spam?

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


  • AmandaEly 123 posts 379 karma points
    Mar 12, 2015 @ 17:05
    AmandaEly
    0

    MediaService and MemberService in Umbraco 7.2.2

    Hi,

    I have created a lot of members in my new 7.2.2 website using the MemberService from the backend. The members have extra properties, among them a "facePhoto". I used the MediaService to upload them and I can see them just fine on the members page in the back office.

    How do I view these images from a razor macro? Using MemberService I can get a list of members. I can get the Media object just fine as well:

    @if (m.HasProperty("facePhoto"))

            {

                var imageId = m.GetValue<int>("facePhoto");

                if (imageId > 0)

                {

                    var imageInfo = mediaService.GetById(imageId);

                    var imagePath = imageInfo.Path;

                }

            }

    The variable imageInfo is of type Umbraco.Core.Models.Media and contains the filename I expect. The problem is that the imagePath is a set of not very useful numbers ( for example -1,1092,1196). I can find nothing online about this latest version. Many examples cite Umbraco.Media but this seems not to exist in the current version. I feel sure this should be easy but so far it is not at all.

  • AmandaEly 123 posts 379 karma points
    Mar 12, 2015 @ 17:16
    AmandaEly
    0

    Oh, and Umbraco.TypedMedia isn't available either. Not that that is an obvious answer.

  • Sören Deger 733 posts 2844 karma points c-trib
    Mar 12, 2015 @ 17:19
    Sören Deger
    0

    Hi AmandaEly,

    the path is only the path of media node ids in the media section. If you use the default mediatype "image" you can get the default umbraco property "umbracoFile". This property contains the url of your media file.

     

    Best,

    Sören

  • Sören Deger 733 posts 2844 karma points c-trib
    Mar 12, 2015 @ 17:22
    Sören Deger
    100

    With this code you should get the url:

    var imagePath = imageInfo.GetValue<string>("umbracoFile");

                            

    Best,

    Sören

  • AmandaEly 123 posts 379 karma points
    Mar 12, 2015 @ 17:59
    AmandaEly
    0

    Sören, my part-Norwegian heart thrill to your name! And yes, that got it in one,. Whew! I know I shoudl be using MediaService.

    thanks,

    Amanda

  • Sören Deger 733 posts 2844 karma points c-trib
    Mar 12, 2015 @ 18:05
    Sören Deger
    0

    Hi Amanda, 

    great that I could help you :-) Can you mark the post with the solution above as solved please? So other people can easier find the solution.

    Best,  Sören

  • AmandaEly 123 posts 379 karma points
    Mar 12, 2015 @ 18:24
    AmandaEly
    0

    I think I have now done this.

  • Sören Deger 733 posts 2844 karma points c-trib
    Mar 12, 2015 @ 18:33
    Sören Deger
    0

    Hi Amanda,

    that has not work. But our.umbraco.org was just short unavailable. After you have do this you should see the post in a green box. You can click to do this on the green hook in the post with the solution.

    Cheers,

    Sören

  • AmandaEly 123 posts 379 karma points
    Mar 13, 2015 @ 10:13
    AmandaEly
    0

    Hi Sören,

    I was so relieved to get a solution yesterday that I did not pause to think. And in fact the solution is too much like magic for my taste. I accept that I need to remember my own type aliases (at least for now) but why should I have to remember "umbracoFile"? Anyone who uses MediaService is going to want to get a usable path the their images, that is what the service is for, surely? I have written an extension method on MediaService to use for now but await an improved version in 7.3 or whenever.

        public static string GetImagePathById(this IMediaService service, int id)
        {
            if (id > 0)
            {
                var imageInfo = service.GetById(id);
                if (imageInfo != null)
                {
                    return imageInfo.GetValue<string>("umbracoFile");
                }
            }
            return string.Empty;
        }
    

    This is not great, as it assumes the id refers to an image and that nothing will go wrong. But it is good enough for my purposes at present.

    Amanda

  • Sören Deger 733 posts 2844 karma points c-trib
    Mar 13, 2015 @ 10:49
    Sören Deger
    0

    Hi Amanda,

    "umbracoFile" is the default de-facto standard in umbraco for the alias of media type upload fields.

     

    Best,

    Sören

Please Sign in or register to post replies

Write your reply to:

Draft