Copied to clipboard

Flag this post as spam?

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

  • Hugh 30 posts 63 karma points
    Jan 24, 2014 @ 03:52

    Create Media Programmatically in V7

    Can anyone help me with some code to create images programmatically in V7 API?  I have got the Node stuff sorted using Umbraco.Core.Serivices.ContentService, but the media stuff is not being so nice.

    Esentially, I have created a user control to which people can create nodes, then upload images to associate with the node.  I want to add these images to the Media section.

    Thanks in advance.


  • Kevin Jump 1673 posts 10586 karma points MVP 3x c-trib
    Jan 24, 2014 @ 13:08
    Kevin Jump

    Hi i've done something very similar in uSync.ContentEdition so the source might shed light on it


    the create is simple enough

    _mediaService = ApplicationContext.Current.Services.MediaService;
    media = _mediaService.CreateMedia(name, parentId, mediaAlias);

    the import media file (in FileHelper above) was more tricky.

    you need to call IMediaItem.SetValue and pass the filename and stream to upload the umbracoFile

    FileStream s = new FileStream(file, FileMode.Open);
    item.SetValue("umbracoFile", Path.GetFileName(file), s);

    all of this assumes you haven't done something 'special' with the umbracoFile property.

    hope that helps.

  • Kasper 21 posts 112 karma points
    Mar 26, 2019 @ 08:18

    Hi Kevin, I am trying to upload a Image, but I get this error.
    enter image description here

  • Sebastiaan Janssen 4876 posts 14511 karma points MVP admin hq
    Jan 24, 2014 @ 17:33
    Sebastiaan Janssen

    Make sure to call _mediaService.Save(media); after that.

Please Sign in or register to post replies

Write your reply to: