Adding custom field to documents and new widget template

Posted by Community Admin on 05-Aug-2018 20:00

Adding custom field to documents and new widget template

All Replies

Posted by Community Admin on 12-Oct-2015 00:00

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!

 

 

Posted by Community Admin on 15-Oct-2015 00:00

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.

Posted by Community Admin on 15-Oct-2015 00:00

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
 

Posted by Community Admin on 15-Oct-2015 00:00

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

Posted by Community Admin on 27-Oct-2015 00:00

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!

Posted by Community Admin on 10-Mar-2017 00:00

Did you have to add or change the namespace or model for the template for this to work?

I have:

@model Telerik.Sitefinity.Frontend.Media.Mvc.Models.DocumentsList.DocumentsListViewModel

@using System;
@using Telerik.Sitefinity.Frontend.Mvc.Helpers;
@using Telerik.Sitefinity.Frontend.Media.Mvc.Models.DocumentsList;

<img src="@item.Fields.PDFThumbnail.Fields.MediaUrl" />

This thread is closed