In this blog I would like to share the code for adding the content type in existing SharePoint list using SP hosted app model.
Write the below code on your *.js file,
  1. //get Current Context
  2. var context = SP.ClientContext.get_current();
  3. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  4. $(document).ready(function () {
  5. console.log("page loading...");
  6. $("#btnCreate").click(function () {
  7. addContenType();
  8. });
  9. });
  10. //This method is used to add the content type on exisiting list.
  11. function addContenType() {
  12. //Get host web URL
  13. var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  14. //get app context using host web url
  15. var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
  16. //get current web
  17. var currentWEB = appCtxSite.get_web();
  18. //get list from host web
  19. var lists = currentWEB.get_lists();
  20. //get rootweb
  21. var rootWeb = appCtxSite.get_site().get_rootWeb();
  22. //Get all content types from root web site
  23. var allContentTypeColl = rootWeb.get_contentTypes();
  24. //load the web , lists & content types
  25. context.load(rootWeb);
  26. context.load(allContentTypeColl);
  27. context.load(lists);
  28. context.executeQueryAsync(function () {
  29. var CTypeID;
  30. var contentTypeName = "customTestCT";
  31. //Get the Content type ID , if we know content type id we dont want to get the id again
  32. var contentTypeEnum = allContentTypeColl.getEnumerator();
  33. while (contentTypeEnum.moveNext()) {
  34. var currentCT = contentTypeEnum.get_current();
  35. if (currentCT.get_name() == contentTypeName) {
  36. CTypeID = currentCT.get_stringId();
  37. break;
  38. }
  39. }
  40. var testList = appCtxSite.get_web().get_lists().getByTitle("SampleList1");
  41. //Enable the content type in custom list
  42. testList.set_contentTypesEnabled(true);
  43. testList.update();
  44. context.load(testList);
  45. var webContentTypes = appCtxSite.get_site().get_rootWeb().get_contentTypes();
  46. var listCollectionCT = testList.get_contentTypes();
  47. var customCT = webContentTypes.getById(CTypeID);
  48. //then add the existing content type using content type id
  49. listCollectionCT.addExistingContentType(customCT);
  50. //then load the list collection content types
  51. context.load(listCollectionCT);
  52. var folder = testList.get_rootFolder();
  53. context.load(folder, 'ContentTypeOrder');
  54. context.executeQueryAsync(function () {
  55. //ordered the content type folder.set_uniqueContentTypeOrder(folder.get_contentTypeOrder().reverse());
  56. folder.update();
  57. context.executeQueryAsync(
  58. function () {
  59. console.log("Content type was added");
  60. },
  61. function () {
  62. console.log("Error in Adding Content Type" + args.get_message());
  63. });
  64. }, function (sender, args) { onfail(sender, args); });
  65. }, function (sender, args) { onfail(sender, args); });
  66. }
  67. // This function is executed if the above call fails
  68. function onfail(sender, args) {
  69. alert('Failed to add content type in list. Error:' + args.get_message());
  70. }
  71. // this method used split the query string
  72. function manageQueryStringParameter(paramToRetrieve) {
  73. var params = document.URL.split("?")[1].split("&");
  74. var strParams = "";
  75. for (var i = 0; i < params.length; i = i + 1) {
  76. var singleParam = params[i].split("=");
  77. if (singleParam[0] == paramToRetrieve) {
  78. return singleParam[1];
  79. }
  80. }
  81. }
Note - Please make sure to provide the permission to web in app manifest file
Finally custom content type will be added successfully on your existing list.
Summary
In this blog we have explored how to add the content type on existing SharePoint list form SharePoint hosted app model with JavaScript object model.