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

Forums / Project Feather / Adding custom field to documents and new widget template

Adding custom field to documents and new widget template

5 posts, 1 answered
  1. Bil
    Bil avatar
    51 posts
    Registered:
    27 Aug 2012
    12 Oct 2015
    Link to this post

    Hi guys,

    I'm adding a thumbnail custom field to the documents and files part of Sitefinity (8.1) and then creating a new widget template to display them. So far so good but I don't know how to get my custom field in my new razor view.

    Here's what I did so far:

    1. Added a new custom field to the documents and files called "Thumbnail" and set it to be an image

    2. Modified a document and set the field to be an image in my system.

    3. Copied the MVC/Views/DocumentsList/List.DocumentsList.cshtml to my own view (List.DocumentsGrid.cshtml) in the ResourcePackages folder

    4. Dropped the document list widget on a page and set the view to my DocumentsGrid view

    Everything renders fine but I'm trying to use the new custom field called Thumbnail. I thought it would be simple (ha) so I could do this:

    <div class="thumbnail">

    @item.Fields.Thumbnail

    </div>

    But that isn't rendering anything.

    I can't seem to find any example where this is done. Anyone know how to access a custom field in a custom view?

    Thanks!

     

     

  2. Bil
    Bil avatar
    51 posts
    Registered:
    27 Aug 2012
    15 Oct 2015
    Link to this post
    Just a note, I am able to get a custom field if it's a text field. Create a custom field called "Test" and in the view I can use @item.Fields.Test and it displays fine. Just having problems when it's an Image field.
  3. Velizar Bishurov
    Velizar Bishurov avatar
    143 posts
    Registered:
    01 Dec 2016
    15 Oct 2015
    Link to this post
    Hi,

    If you have specified the custom field to have only one image you can use the following code:

    @item.Fields.Thumbnail.Fields.MediaUrl

    If you have not explicitly specified that you want only one image Sitefinity automatically sets the connection as multiple images and you will need to specify which image you want like so:

    @item.Fields.Thumbnail[0].Fields.MediaUrl

    Regards,
    Velizar Bishurov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
    Answered
  4. Bil
    Bil avatar
    51 posts
    Registered:
    27 Aug 2012
    15 Oct 2015 in reply to Velizar Bishurov
    Link to this post

    Hi Velizar,

    This looks good but something is amiss because the @item.Fields.Thumbnail field is always null and not working.

    I've checked the custom field and it looks right. I don't seem to have any tools in the view to be able to figure out what fields are available (like a debug dump of the fields) and without building an entire widget or something I'm not sure how to debug something like this?

    I've called the field "Thumbnail" and made it of type Related Media. Attached are the settings for my custom field. Looks okay to me but am I missing something?

    Thanks

  5. Bil
    Bil avatar
    51 posts
    Registered:
    27 Aug 2012
    27 Oct 2015
    Link to this post

    Just a note I actually submitted a ticket to get this resolved as it didn't make sense and wasn't working for me.

    Turns out that I was using a field named "Thumbnail" and there's already a field called Thumbnail in the documents but it's not serializeable and doesn't appear in the backend UI. When you create a field with the same name it overrides the default one, but that field wasn't serializeable (like the original one) and was causing the lookup to fail.

    Simply renaming my field to something like Preview resolved the issue and everything works perfectly now.

    Thanks!

5 posts, 1 answered