Geeks With Blogs

News Cloud development with Azure, Visual Studio and SharePoint Online!
Thorvald Bøe
I recently needed to create a rich text field in a SharePoint list programatically. Since I am using REST all over the place, it would be practical to continue that now as well.

I was able to find out how to create a new field. It is done by posting to this url:

    reqUrl = appweburl +
            "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/fields?@target='" +
            hostweburl + "'";


For example like this:

$.ajax({
  url: reqUrl,
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.Field' }, 'Title': 'Comments', 'FieldTypeKind': 3 }",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "accept": "application/json; odata=verbose",
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>
  },
  success: successHandler,
  error: errorHandler
});

Note the FieldTypeKind which is set to 3, which refers to the multi line text field. 
The problem is that by default, the field is created as a plain text field. I needed rich text, so I started searching for a solution. I did not get many hits. I found some hints, e.g. there seems to be a "RichText" property that seems promising:



But I was not sure how to use it.
Finally, I found the solution in the Fields REST API reference (see "MERGE request example")


$.ajax({
  url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')
    /fields('1d22ea11-1e32-424e-89ab-9fedbadb6ce1')",
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.FieldMultiLineText' }, 'RichText': true }",
  headers: {
    "X-RequestDigest": <form digest value>,
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>,
    "X-HTTP-Method": "MERGE"
  },
  success: successHandler,
  error: errorHandler
});

By slightly modifying this example, I was able to make it work. I already tried adding 'RichText': true to my data object, but got an error message. The key point here is that you need to combine RichText=true with the type "SP.FieldMultiLineText" instead of the regular "SP.Field".

The final result of my reqData variable:

var reqData = JSON.stringify({ '__metadata': { 'type': 'SP.FieldMultiLineText' } ,'FieldTypeKind': 3 ,'RichText': true ,'Title': 'RichTextColumn' });

This shows not only how to create rich text fields, but how to set other field properties as well (this link, remember?) It seems there are a rich set of properties accessible to those who need to create complex schemas.





Posted on Thursday, January 14, 2016 12:32 PM jquery , sharepoint , REST | Back to top


Comments on this post: Create a multi line rich text field in a SharePoint list using REST

# re: Create a multi line rich text field in a SharePoint list using REST
Requesting Gravatar...
fdgdfg
fdgdg
Left by fg on Apr 25, 2018 8:50 AM

Your comment:
 (will show your gravatar)


Copyright © Thorvald Bøe | Powered by: GeeksWithBlogs.net