Copied to clipboard

Flag this post as spam?

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


  • George Phillipson 108 posts 287 karma points
    Mar 29, 2020 @ 23:19
    George Phillipson
    0

    Add image to Nested Content via API in class library

    ------------FIXED------------------

    I have fixed the problem, should have used media.GetUdi().ToString()

    Hi

    Has anyone managed to add an image via the API when using nested content?

    My code is below, but the problem I have is I get a message saying the image is trashed, but below that is the image.

    I have also attached the image.

    enter image description here

    Code for adding the image to media

     public string ReturnSerializedJsonMediaObject(string excelColumn, string ncContentTypeAlias, string pathToExtractZipFile)
            {
                try
                {
                    if (!string.IsNullOrEmpty(excelColumn))
                    {
    
                        string[] splitExcelColumn = excelColumn.Split(';');
    
                        var nestedContentHelpers = new List<Dictionary<string, string>>();
    
                        foreach (var columnData in splitExcelColumn)
                        {
                            string[] splitHeaderAndBodyText = columnData.Split(',');
    
                            Guid nestedContentKey = Guid.NewGuid();
    
                            string retrieveImage = Directory.EnumerateFiles(pathToExtractZipFile, splitHeaderAndBodyText[0], SearchOption.AllDirectories).First();
    
    
                            using (Stream stream = System.IO.File.OpenRead(retrieveImage))
                            {
                                string imageExtension = Path.GetExtension(retrieveImage);
    
                                IMedia media = _iMediaService.CreateMediaWithIdentity(nestedContentKey.ToString(), -1, "Image");
                                media.SetValue(_iContentTypeBaseServiceProvider, Constants.Conventions.Media.File, $"{nestedContentKey.ToString()}{imageExtension}", stream);
                                _iMediaService.Save(media);
    
                                nestedContentHelpers.Add(new Dictionary<string, string>()
                                {
                                    {"key", nestedContentKey.ToString()},
                                    {"name", splitHeaderAndBodyText[0]},
                                    {"ncContentTypeAlias", ncContentTypeAlias},
                                    {"image", media.Path},
                                    {"imageAltText", splitHeaderAndBodyText[1]},
                                });
                            }
                        }
    
                        return JsonConvert.SerializeObject(nestedContentHelpers);
                    }
    
                    return string.Empty;
                }
                catch (Exception e)
                {
                    Current.Logger.Error(System.Reflection.MethodBase.GetCurrentMethod().GetType(), $"Error adding Nested Content {e}", e);
                    return string.Empty;
                }
            }
    

    Code for adding content which is a foreach loop

     var nestedContentImage = _iNestedContentMediaPropertiesHelper.ReturnSerializedJsonMediaObject(images, "nestedImagesDocType", pathToExtractZipFile);
    
                                        content.SetValue("images", nestedContentImage);
    
                                        _iContentService.SaveAndPublish(content);
    
Please Sign in or register to post replies

Write your reply to:

Draft