Tuesday, September 9, 2014

Working With Sitecore List Fields

Working with Single list item

     Sitecore.Data.Fields.ReferenceField refDroplinkField = current.Fields ["Drop Link Field"];
            Sitecore.Data.Fields.ReferenceField refDroplistField = current.Fields["Drop List Field"];
            Sitecore.Data.Fields.ReferenceField refGroupedDroplinkField = current.Fields ["Grouped Droplink Field"];
            Sitecore.Data.Fields.ReferenceField refGroupedDroplistField = current.Fields ["Grouped Droplist Field"];
            Sitecore.Data.Fields.ReferenceField refDroptreeField = current.Fields ["Drop Tree Field"];
            if (refDroptreeField != null && refDroptreeField.TargetItem != null)
            {
                //ltPageData.Text = refDroplinkField.TargetItem.Fields["Category Name"].Value;
                //ltPageData.Text = refDroplistField.Path;
                //ltPageData.Text = refGroupedDroplinkField.TargetItem.Fields["Category Name"].Value;
                // ltPageData.Text = refGroupedDroplistField.Path;
                ltPageData.Text = refDroptreeField.TargetItem.Fields["Category Name"].Value;
            }

Note: Droplist data type only stores the string value of the item that was chosen by the content editor, not the GUID that would allow us to get back to the item and gets its other fields. So aside from doing string matching on the name of the item, there’s no way to get back to the actual item that was chosen.

Working with Multi list item

        Sitecore.Data.Fields.MultilistField refMultilistField = current.Fields["Multi List Field"];
            if (refMultilistField != null)
            {
                Item[] items = refMultilistField.GetItems();
                List<Item> check = items.Where(x => x.Fields["Category Name"].Value == "Liquor").ToList();
            }

Treelist / TreelistEx:

The value of Treelist/ TreelistEx fields are very similar to the value of the Checklist and Multilist field in that it is a pipe-separated list of IDs of the selected items.

For performance, you should use the TreelistEx field type in your data templates instead of the Treelist field type. With a Treelist, the client renders the selection tree and the list of selected items whenever the user selects an item that contains the field. With a TreelistEx, the client renders only the list of selected items, and does not render the selection tree until the user clicks the Edit command above the field.

Source Parameters

DataSource: The root item the field points to. Must be a path.
Datasource=/sitecore/common/categories

ExcludeTemplatesForSelection: This is a comma separated list of template names that should NOT be allowed for selection. These items may need to appear in the treelist to show sub-items, but these cannot actually be selected. For example folders.
DataSource=/sitecore/common/categories&ExcludeTemplatesForSelection=Folder

IncludeTemplatesForSelection: This is a comma separated list of template names that should be allowed for selection. If this is defined, only items of templates defined in this list can be selected.
DataSource=/sitecore/common/categories&IncludeTemplatesForSelection=Category

ExcludeTemplatesForDisplay: This is a comma separated list of templates that should not be displayed in the treelist. Any sub-items of these templates will also not display because they are hidden.
DataSource=/sitecore/common/categories&ExcludeTemplatesForDisplay=Categoty

IncludeTemplatesForDisplay: This is a coma separated list of template names that should be displayed in the treelist. If this is defined, only items of templates defined in this list can be displayed. An important thing to note here is that for any templates in this list, the parent items must at least be displayed in the treelist in order for these to show up.
DataSource=/sitecore/common/categories&IncludeTemplatesForDisplay=Categoty

IncludeItemsForDisplay: This is a comma separated list of GUIDs that should be displayed.
DataSource=/sitecore/common/categories&IncludeItemsForDisplay= {93FBD42C-458A-4C9C-A999-1EE2B651C6E1}, {3ABCX2C-458A-4C9C-A999-1EE2B651C6E1}

ExcludeItemsForDisplay: This is a comma separated list of GUIDs that should NOT be displayed.
DataSource=/sitecore/common/categories&ExcludeItemsForDisplay= {93FBD42C-458A-4C9C-A999-1EE2B651C6E1}, {3ABCX2C-458A-4C9C-A999-1EE2B651C6E1}

AllowMultipleSelection: This defines whether or not an editor can select the same item more than once. It takes a word yes.
DataSource=/sitecore/common/categories&AllowMultipleSelection=yes

DatabaseName: Changes the database name being referenced. Useful if using an external data provider.

Working with Name Value List Field:

The Name Value List field type allows the user to enter values for zero or more arbitrary keys. Sitecore stores the values entered as a list of key=value pairs separated by ampersands “&”.

Option 1:

  Sitecore.Data.Fields.NameValueListField refNameValuelistField = current.Fields["Name Value List Field"];
        if (refNameValuelistField != null )
        {
            NameValueCollection nameValueCollection = refNameValuelistField.NameValues;
            foreach (var nv in nameValueCollection)
            {
                ltPageData.Text += nameValueCollection[nv.ToString()] + "<br/>";
            }
        }

Option 2:

        Sitecore.Data.Fields.MultilistField refMultilistField = current.Fields ["Name Value List Field"];
        if (refMultilistField != null && refMultilistField.List != null)
        {
            string _urlParamsToParse = refMultilistField.List.ToString ();
            NameValueCollection nameValueCollection = Sitecore.Web.WebUtil.ParseUrlParameters(_urlParamsToParse);
            foreach (var nv in nameValueCollection)
            {
                ltPageData.Text += nameValueCollection[nv.ToString()] + "<br/>";
            }
        }

Working with Name Lookup Value List Field:


Name Lookup Value field takes a data source parameter which is used to pick values for entered keys.









        Sitecore.Data.Fields.MultilistField refMultilistField = current.Fields["Name Lookup Value List Field"];
        if (refMultilistField != null && refMultilistField.List != null)
        {
            string _urlParamsToParse = refMultilistField.List.ToString();
            NameValueCollection nameValueCollection = Sitecore.Web.WebUtil.ParseUrlParameters(_urlParamsToParse);
            foreach(var nv in nameValueCollection)
            {
                Item i = master.GetItem(nameValueCollection[nv.ToString()]);
                ltPageData.Text += i.Fields["Category Name"] + "<br/>";
            }
        }

Note: Name Lookup Value List field stores Item Id for each key











No comments:

Post a Comment