Question

I would like to be able to store reusable html-formatted text in Sitecore and reference in codebehind for inclusion in a custom user control. What is the best practice for doing this? If a user selects option A, for example, I would reference standard text A in my control. Any examples for how to accomplish this are appreciated. Thanks.

Was it helpful?

Solution

You have a couple options:

  1. Store the text in the Standard Values of the same template that defines your option list. That makes it available on the same item, but standard for all items. Use security to lock down the field if you are worried about it being edited. This could also be accomplished with the new "cloning" feature in 6.4, I believe.
  2. Create a structure outside of your Home element for storing this data. Based on the option selected, find an item in your content tree which corresponds to the selected item, and read the text off of it. You would need to find this item either relative to /sitecore/Content, or relative to your website root if multi-site support is a requirement.

No.2 in pseudo-code:

//get the item where we have the text values
Item textBase = Sitecore.Context.Database.SelectSingleItem(textBasePath);
//find the child w/ the same name as the selected option
Item textItem = textBase.Axes.GetChild(selectedOptionValue);
string value = textItem["text"];

OTHER TIPS

I think I would do something like techphoria414's 2. option:

ie you have your normal "page" templates as per usual, but then you have some fields (multilist, treelist fields), where you put the source pointing to your other items contain the different texts.

then you basically just have to get the items from the current item (with some very quick'n'dirty code/pseudocode):

    var CurrentItem = Sitecore.Context.Item;
    Sitecore.Data.Fields.MultilistField mlf1 = CurrentItem.Fields["myExternalTexts"];
    if(mlf1 != null)
    {
        foreach (Item itm in mlf1.GetItems())
        {
            lit += Sitecore.Web.UI.WebControls.FieldRenderer.Render(itm, "richtext");
        }
    } 

You ofc shouldn't just add them to a literal and you should you Sitecore built in Field renderes if using Sitecore 6 or above and it's a Rich text field.

I hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top