Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Droplist types in Sitecore template fields

Tags:

sitecore

Is there anyone who knows how to use "Droplist" type in template fields?

I guess "Droplist" is the same as <select><option></option></select> type.

I'd like to specify select list types with static values so that Sitecore editors can select only one of many available lists when they create a page. My plan is to add CSS class names (<option>) in the list (<select>) and editors will use one of styles by selecting one of them.

How to add the values in select list? Do I have to write code?

like image 580
Jay Avatar asked Dec 19 '22 07:12

Jay


2 Answers

The Droplist is similar to the Droplink field type in that they are both dropdowns. The Droplist will only store the name of the item (so it will not have a link to that item), while the Droplink stores the ID of the item. That means if you rename an option, or move it elsewhere in your Content Tree, the Droplist will not update (resulting in possible broken links), the Droplink will update.

You can add values to the Droplist by setting the Datasource field in the templates to something (for instance /sitecore/content/Home/CSS/ if that's where you would like to store your CSS class names).

You can access the Droplist in code like so:

Item item = Sitecore.Context.Item;
string css = item["FieldName"]; // Also possible is item.Fields["Fieldname"].Value;

A Droplink could be accessed like this:

string dropDownItemId = item["Fieldname"]; // Or, again, item.Fields["Fieldname"].Value; if you prefer
var cssItem = Sitecore.Context.Database.GetItem(dropDownItemId); // And now you can
// access any fields in this item.

Edit A good article going into some more detail in the differences between Droplink and Droplist

like image 63
Trayek Avatar answered Dec 28 '22 08:12

Trayek


I would proceed exactly as @Trayek suggested in his solution. To expand on that, this should be your implementation:

You create your own template, maybe called CSS Class, and you will add a Single-Line Text field to it, maybe called Value. You will put the actual CSS classes in that field when you create the items.

Next, you will add a folder item somewhere in the Content tree, and you will add your CSS Class items to that folder. That folder will also be the datasource of your droplist/droplink.

To set your datasource, this blog post should be of help. You will be looking to use number 1 under For Fields without Search (screenshot included, below). I wrote the article a while back, so if you need any additional help just let me know.

enter image description here

like image 25
Zachary Kniebel Avatar answered Dec 28 '22 10:12

Zachary Kniebel