Copied to clipboard

Flag this post as spam?

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


  • Robert Dougan 48 posts 154 karma points
    Jan 14, 2013 @ 17:59
    Robert Dougan
    0

    Issue When Creating Media Items Using API

    Hi,

    I'm creating media items as part of an import from a CSV file. Each media item has a number of properties, one of which is "umbracoFile", a property which holds the associated PDF.

    I'm using the API to create the media items like so:

    Media newItem = Media.MakeNew(lineValues[1], MediaType.GetByAlias("MyMediaType"), User.GetUser(0), 5902);

    And then setting the "umbracoFile" property like so:

    newItem.getProperty("umbracoFile").Value = "The path to the file";

    I'm then using File.Move to move the actual PDF file into the correct folder in the "Media" directory.

    This all seems to work, except the file is not shown in the "Link to media item(s)" row when I view the media item, and the document is not being indexed by Examine either.

    I've no idea what's going on. I'm guessing I'm missing something when I create the media item, perhaps I'm not saving it properly, or it needs to be published?

    Can anyone help?

    Robert

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 15, 2013 @ 10:29
    Jeroen Breuer
    100

    Don't see what could be the problem. Here is the code I always use to create media. Always seems to work:

    public Media InsertMedia(int parentId, string fileName, MediaType mediaType, User adminUser, byte[] bytes, bool isImage)
    {
        Media media = Media.MakeNew(fileName, mediaType, adminUser, parentId);
    
        string sql = string.Format(@"
                    select cpd.id as id from cmsPropertyData cpd
                        inner join cmsPropertyType cpt on cpd.propertytypeid = cpt.Id
                        inner join cmsDataType cdt on cpt.dataTypeId = cdt.nodeId
                    where cpd.contentNodeId = {0}
                    and cdt.controlId = '{1}'", media.Id, uploadField.Id);
    
        int propertyId = SqlHelper.ExecuteScalar<int>(sql);
    
        FileInfo file = new FileInfo(IOHelper.MapPath(SystemDirectories.Media + "/" + propertyId + "/" + fileName));
    
        if (!file.Directory.Exists)
        {
            //If the directory doesn't exist then create it.
            file.Directory.Create();
        }
    
        //Write the file to the media folder.
        File.WriteAllBytes(file.FullName, bytes);
    
        string umbracoFile = SystemDirectories.Media + "/" + propertyId.ToString() + "/" + fileName;
        if (umbracoFile.StartsWith("~"))
        {
            umbracoFile = umbracoFile.TrimStart(new char[] { '~' });
        }
    
        if (media.getProperty("umbracoFile") != null)
        {
            media.getProperty("umbracoFile").Value = umbracoFile;
        }
    
        if (media.getProperty("umbracoExtension") != null)
        {
            media.getProperty("umbracoExtension").Value = Path.GetExtension(file.Name).Replace(".", string.Empty);
        }
    
        if (media.getProperty("umbracoBytes") != null)
        {
            media.getProperty("umbracoBytes").Value = bytes.Length;
        }
    
        if (isImage)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(file.FullName);
    
            if (media.getProperty("umbracoWidth") != null)
            {
                media.getProperty("umbracoWidth").Value = image.Width.ToString();
            }
    
            if (media.getProperty("umbracoHeight") != null)
            {
                media.getProperty("umbracoHeight").Value = image.Height.ToString();
            }
    
            //Create a thumbnail from the image.
            string fileNameThumb = Path.Combine(file.Directory.FullName, file.Name.Replace(Path.GetExtension(file.Name), "_thumb.jpg"));
            GenerateThumbnail(image, 100, image.Width, image.Height, fileNameThumb);
    
            //Clean the image.
            image.Dispose();
        }
    
        //Save the media file to update the xml to make sure some event's get called.
        media.Save();
        media.XmlGenerate(new XmlDocument());
    
        return media;
    }

    Jeroen

  • Robert Dougan 48 posts 154 karma points
    Jan 15, 2013 @ 10:37
    Robert Dougan
    0

    Thanks Jeroen,

    Your example is following roughly the same logic as what I'm doing, but mine doesn't work!

    Can you tell me what media.XmlGenerate(newXmlDocument()); does?

    Robert

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 15, 2013 @ 10:38
    Jeroen Breuer
    0

    It updates the media in the cmsContentXml table so when you use library.GetMedia(id, false) it will return the newest xml.

    Jeroen

  • Robert Dougan 48 posts 154 karma points
    Jan 15, 2013 @ 12:21
    Robert Dougan
    0

    I see you're using the ID of the umbracoFile property in the file path instead of the ID of the newly created media item, which is what I was using. I've changed my code to use the umbracoFile ID for the file path but it still doesn't work.

    Very strange.

  • Robert Dougan 48 posts 154 karma points
    Jan 15, 2013 @ 14:36
    Robert Dougan
    0

    Cracked it!

    I wasn't setting the correct path for the umbracoFile property, it's all working fine now!

    Thanks for posting your example, it put me on the right track.

    Robert

Please Sign in or register to post replies

Write your reply to:

Draft