I am trying to add a new column to a contentytpe and type note but with rich text enabled.

It doesnt throw any exceptions: In the site columns definition it appears as Multiple lines of text with rich text enabled.

However when I place the cursor inside the field, the ribbon is not chaning to show the bold,italic, etc.

the code is as follows:

using (SPSite spsite = new SPSite(strurl))
                    using (SPWeb currentWeb = spsite.OpenWeb())
                            currentWeb.AllowUnsafeUpdates = true;
                            SPContentType agendaPointsProposedCT = currentWeb.ContentTypes[Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME];
                            string reasonReturnedFieldName = currentWeb.Fields.Add(Meetings.Common.Constants.FIELDS_AGENDAPOINTSREASONRETURNED_NAME, SPFieldType.Note, false);
                            SPFieldMultiLineText reasonReturnedField = currentWeb.Fields.GetFieldByInternalName(reasonReturnedFieldName) as SPFieldMultiLineText;

                            reasonReturnedField.Group = "$Resources:XXX,Field_XXXColumns_Group";
                            string schemaXmlWithResourceTokens = reasonReturnedField.SchemaXmlWithResourceTokens;
                            string displaynamelocalized = "$Resources:XXX,Field_ReasonReturned_Name";
                            string returnValue;
                            int indexOfAttributeName = schemaXmlWithResourceTokens.IndexOf("DisplayName", StringComparison.InvariantCultureIgnoreCase);
                            int indexOfAttibuteValueBegin = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttributeName);
                            int indexOfAttributeValueEnd = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttibuteValueBegin + 1);
                            returnValue = schemaXmlWithResourceTokens.Substring(0, indexOfAttibuteValueBegin + 1) + displaynamelocalized + schemaXmlWithResourceTokens.Substring(indexOfAttributeValueEnd);
                            reasonReturnedField.SchemaXml = returnValue;
                            reasonReturnedField.RichText = true;
                            reasonReturnedField.RichTextMode = SPRichTextMode.FullHtml;
                            reasonReturnedField.AllowHyperlink = true;

                            agendaPointsProposedCT.AddFieldRefFromContentType(currentWeb, reasonReturnedField, true);
                            MoveFieldInColumnOrderToLastPosition(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, reasonReturnedField.InternalName);

  public static void AddFieldRefFromContentType(this SPContentType contentType, SPWeb web,SPField field, bool pushChanges)
            SPFieldLink fieldLink = new SPFieldLink(web.AvailableFields.GetField(field.InternalName));
            //Check if the Field reference exists
            if (!contentType.Fields.ContainsField(field.Title))
                //Do Nothing
  • I found this, social.technet.microsoft.com/Forums/en/…, but I am getting the field after adding it, so no sure how to implement the solution here – Luis Valencia Sep 14 '12 at 14:45
  • I'd be curious to see what the line:contentType.Fields.ContainsField(field.Title)) returns. – Derek Gusoff Sep 14 '12 at 17:32
  • ...because if it returns false your update code never gets hit – Derek Gusoff Sep 14 '12 at 17:34
  • it gets hit, the field is in the list. – Luis Valencia Sep 14 '12 at 18:52

Finally, I was able to solve it:

Solution I found is here: http://levalencia.wordpress.com/2012/09/17/add-spfieldmultilinetext-html-field-by-code-wont-show-html-ribbon-tools/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.