1
votes

I am programmatically importing data from Excel sheet to Sitecore items. The data in the description section has HTML tags in it. Is there a way to programmatically map the data to the HTML tab of Sitecore Rich text editor instead of the normal text tab?

1
Can you show your current code? Inserting the HTML into the Field's value should get you the desired effect - Jonathan Robbins
When you say programmatically importing data, are you doing this as some external script to populate a set of items (if so, @jRobbins answer below should do it), or do you actually want a custom button in the Rich Text Editor you can click while editing an individual item's rich text field? - Matthew Dresser

1 Answers

4
votes

Setting the Rich Text Editor field can be done via code below

string html = "<div></div>";

//Account with permission to edit item
var user = Sitecore.Security.Accounts.User.FromName("username",false);

using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
    try
    {
        item.Editing.BeginEdit();

        //Get the RichTextEditor field
        Sitecore.Data.Fields.HtmlField htmlField = item.Fields.FirstOrDefault(f => f.Name.Trim().ToLower() == fieldName.Trim().ToLower());

        if (htmlField != null)
        {
            htmlField.Value = html; 
        }     
    }
    catch (Exception ex)
    {
        Log.Error(ex.Message, ex, this);
        throw ex;
    }
    finally
    {
        item.Editing.EndEdit();
    }
}