Add SPFieldMultiLineText html field by code wont show HTML Ribbon tools

Posted on Updated on

Well, this was a problem where I lost almost 2 days, and in theory it should just work.  Adding a field to a contenttype, set field properties, and then push down the changes to the lists.  Piece of cake, isnt it?  Well, it is, but not when you are adding a SPFieldMultiLineText and specially if you want that multiline to use FullHtml controls in the ribbon.

Well, if you ask me, an experience sharepoint and .net developer, this has to be a BUG from the product, there is no other explanation, and the issue is:

1. I added a spfield multiline, type: note to field collection

2. Then I added that field to a contenttype and pushed down the changes to the lists

3. The field gets added to all the lists, however, when the field has the focus, the ribbon tools, (bold, italic, etc), they just wont appear.

The explanation is:

Sharepoint sets the RichtText to False, when you are adding the field to the FieldsCollection of the web.

You have to set it AGAIN to true when adding it to the list, or after its added to the list.

This post helped me a little bit, even though, this posts talks about Publishing HTML fields, which are different, but the same thing happens:

The solution, I implemented was a little bit different to the post, because the field in a multiline rich text field is not HTML but Note.

Here is the code:

  /// <summary>
        /// Sharepoint after adding a multiline field to the fields collection, it sets the richtext back to false.
        /// </summary>
        /// <param name="strurl">Web url</param>
        /// <param name="liststr">List Name</param>
        private void processweb(string strurl, string liststr)
        {           
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite spsite = new SPSite(strurl))
                    {
                        using (SPWeb currentWeb = spsite.OpenWeb())
                        {
                            try
                            {
                                currentWeb.AllowUnsafeUpdates = true;
                                SPList list = currentWeb.GetSafeListByName(liststr);
                                SPField field;
                                for (int i = 0; i < list.Fields.Count; i++)
                                {
                                    field = list.Fields[i];
                                    //to work around a sharepoint defect ... make html fields work in richtext mode
                                    if (field != null && 
string.Compare(field.TypeAsString, "Note", true) == 0 &&
 (field as SPFieldMultiLineText).RichTextMode == SPRichTextMode.FullHtml)
                                    {
                                        (field as SPFieldMultiLineText).RichText = true;
                                        field.Update(true);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw;
                            }
                            finally
                            {
                                currentWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s