Copied to clipboard

Flag this post as spam?

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


  • Brett Ward 3 posts 73 karma points
    Feb 09, 2023 @ 22:49
    Brett Ward
    0

    Problem with creating content and media programmatically

    Hi Guys,

    Stumped. Using Umbraco 9.5.2

    I have a form where a user submits a resume and and some basic info. I take the info and the resume formfile (pdf, docx, etc) and create content programmatically. I can successfully create the media file and save it to a media folder. I can successfully create the content and save to the CMS, with the exception of the media picker property which I am passing the reference to the media file I created... Umbraco will not populate that property with the created media file. I thought maybe scoping and have moved references all around, etc., to no avail. What am I missing and thanks for any help you can provide.

    string nodeName = $"{RemoveSpecialCharacters(model.FirstName)} {RemoveSpecialCharacters(model.LastName)}";

                        var resume = _contentService.Create(nodeName, model.ParentId, "resumeSubmission");
    
                        resume.SetValue("firstName", model.FirstName);
                        resume.SetValue("lastName", model.LastName);
                        resume.SetValue("email", model.Email);
                        resume.SetValue("phone", model.PhoneNumber);
                        resume.SetValue("position", model.Position);
                        resume.SetValue("message", model.Message);
    
                        //media file
    
                        var folderId = _mediaService.GetRootMedia().FirstOrDefault(x => x.Name == "Resumes");
                        var resumeFile = _mediaService.CreateMedia(model.Resume.FileName, folderId, Constants.Conventions.MediaTypes.File);
    
                        if (model.Resume.Length > 0)
                        {
                            using (Stream s = model.Resume.OpenReadStream()) 
                            {
                                resumeFile.SetValue(
                                    _mediaFileManager,
                                    _mediaUrlGeneratorCollection,
                                    _shortStringHelper,
                                    _contentTypeBaseServiceProvider,
                                    "umbracoFile",
                                    model.Resume.FileName,
                                    s);
    
                                var umbracoResumeFile = _mediaService.Save(resumeFile);
    
                                resume.SetValue("resumeFile", resumeFile);
    
                                _contentService.Save(resume);
                            }                           
                        }
    
                        return true;
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 10, 2023 @ 08:59
    Dave Woestenborghs
    0

    Hi Brett,

    What datatype is your resumeFile property ?

    Dave

  • Brett Ward 3 posts 73 karma points
    Feb 10, 2023 @ 14:20
    Brett Ward
    0

    Solved this, by creating and passing a Udi to the SetValue method. I don't know how I missed this page in the documentation:

    https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/media-picker-3

    Here is the complete code for reference:

    public async Task<bool> ResumeSubmission(ResumeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError("Resume Submission Form Error: Model State Invalid");
                return false;
            }
    
            var isCaptchaValid = await IsCaptchaValid(model.GoogleToken);
    
            if (!isCaptchaValid)
            {
                _logger.LogError("Failed Captcha");
                return false;
            }
    
            try
            {
                string nodeName = $"{RemoveSpecialCharacters(model.FirstName)} {RemoveSpecialCharacters(model.LastName)}";
    
                var resume = _contentService.Create(nodeName, model.ParentId, "resumeSubmission");
    
                resume.SetValue("firstName", model.FirstName);
                resume.SetValue("lastName", model.LastName);
                resume.SetValue("email", model.Email);
                resume.SetValue("phone", model.PhoneNumber);
                resume.SetValue("position", model.Position);
                resume.SetValue("message", model.Message);
    
                if (model.Resume.Length == 0)
                {
                    _logger.LogInformation("Resume with no attachment");
                    _contentService.Save(resume);
    
                    return true;
                }
    
                //media file
    
                var folderId = _mediaService.GetRootMedia().FirstOrDefault(x => x.Name == "Resumes");
                var resumeFile = _mediaService.CreateMedia(model.Resume.FileName, folderId, Constants.Conventions.MediaTypes.File);
    
                using (Stream s = model.Resume.OpenReadStream())
                {
                    resumeFile.SetValue(
                        _mediaFileManager,
                        _mediaUrlGeneratorCollection,
                        _shortStringHelper,
                        _contentTypeBaseServiceProvider,
                        "umbracoFile",
                        model.Resume.FileName,
                        s);
    
                    var operationResult = _mediaService.Save(resumeFile);
    
                    if (operationResult.Success)
                    {
                        var udi = Umbraco.Cms.Core.Udi.Create(Constants.UdiEntityType.Media, resumeFile.Key);
    
                        resume.SetValue("resumeFile", udi.ToString());
                    }
                    else
                    {
                        _logger.LogError(operationResult.Exception, "Error saving media file");
                    }
                }
    
                _contentService.Save(resume);
    
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failure Sending Saving Resume Information");
                return false;
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft