+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Problem setting Generic Content metafields programmatically

Problem setting Generic Content metafields programmatically

2 posts, 1 answered
  1. Keith
    Keith avatar
    67 posts
    Registered:
    13 Sep 2010
    18 Oct 2010
    Link to this post
    Hi,

    I have some server side code that is programmatically adding some files to the Images & Documents module.  The code runs fine and the images are created, but when I go to the module to edit an individual document that I have uploaded using this code, the description field is blank.  The text in this field shows up in an overview of an item (not editing it), but when you click edit, the description field is blank.  I also have several custom controls that access that field and they display the text from this field without any issues.

    This would raise an issue if an end user came in and edited one of these items, as they would save the content item and overwrite the existing metadata with the blank field in the edit form!

    The code which programmatically adds the files:
    01.public string Add(string Provider,       // always set to "Libraries"
    02.                  string LibraryName,    // name of library to store to
    03.                  string filename,       // path to file to upload
    04.                  string ContentType,    // mime type of file
    05.                  string Name,           // Name of document
    06.                  string Author,         // (haven't really tested this field)
    07.                  string Description,    // <-- problem field!
    08.                  string Category,       // (haven't tested this one either)
    09.                  List<string> Tags)     // These are storing properly
    10.    {
    11. 
    12.        LibraryManager libraryManager = new LibraryManager(Provider);
    13.        ILibrary library = libraryManager.GetLibrary(LibraryName);
    14.        ContentManager contentManager = new ContentManager(Provider);
    15. 
    16.        IContent upload = null;
    17.        using (var src = File.OpenRead(filename))
    18.        {
    19.            upload = libraryManager.UploadFile(src, Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename), ContentType, library);
    20.        }
    21.        IContent image = contentManager.GetContent(upload.ID);
    22.        image.SetMetaData("Name", Name);
    23.        image.SetMetaData("Author", Author);
    24.        image.SetMetaData("Description", Description); // Problem field
    25.        image.SetMetaData("Category", Category);
    26.        contentManager.SaveContent(image);
    27.        if (Tags.Count > 0)
    28.            contentManager.SaveTags(image.ID, Tags); // Store tags
    29. 
    30.        return image.ID.ToString();
    31.    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 Oct 2010
    Link to this post
    Hi Keith,

    1. Get the current state of the item

    using (var src = File.OpenRead(filename))
           {
               upload = libraryManager.UploadFile(src, Path.GetFileNameWithoutExtension(filename), Path.GetExtension(filename), ContentType, library);
           }
           IContent image = contentManager.GetCurrentState(upload.ID);
           image.SetMetaData("Name", Name);
           image.SetMetaData("Author", Author);
           image.SetMetaData("Description", Description); // Problem field
           image.SetMetaData("Category", Category);
           contentManager.SaveContent(image);
           if (Tags !=null && Tags.Count > 0)
               contentManager.SaveTags(image.ID, Tags); // Store tags

    2. Make sure that "Description" is added in the lines below

    <libraryInfo>
                <add name="Image" title="Image Library" defaultExtenstions=".png, .jpg, .jpeg, .gif" metaKeys="Description, Author, AlternateText, Extension, Name, Height, Width, Size, Category"></add>
                <add name="Document" title="Document Library" defaultExtenstions=".doc, .docx, .rtf, .txt, .pdf, .ppt, .pps, .pptx, .html, .xls, .xlsx" metaKeys="Description, Author, Description, Extension, Name, Size, Category"></add>
                <add name="Custom" title="Custom Library" defaultExtenstions=".*" metaKeys="Description, Author, Description, Extension, Name, Size, Category"></add>
                <add name="Video" title="Video" defaultExtenstions=".wmv" metaKeys="Description, Author, Extension, Name, Size, Category, Description"></add>


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
2 posts, 1 answered