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,
- //get Current Context
- var context = SP.ClientContext.get_current();
- // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
- $(document).ready(function () {
- console.log("page loading...");
- $("#btnCreate").click(function () {
- addContenType();
- });
- });
- //This method is used to add the content type on exisiting list.
- function addContenType() {
- //Get host web URL
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- //get app context using host web url
- var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
- //get current web
- var currentWEB = appCtxSite.get_web();
- //get list from host web
- var lists = currentWEB.get_lists();
- //get rootweb
- var rootWeb = appCtxSite.get_site().get_rootWeb();
- //Get all content types from root web site
- var allContentTypeColl = rootWeb.get_contentTypes();
- //load the web , lists & content types
- context.load(rootWeb);
- context.load(allContentTypeColl);
- context.load(lists);
- context.executeQueryAsync(function () {
- var CTypeID;
- var contentTypeName = "customTestCT";
- //Get the Content type ID , if we know content type id we dont want to get the id again
- var contentTypeEnum = allContentTypeColl.getEnumerator();
- while (contentTypeEnum.moveNext()) {
- var currentCT = contentTypeEnum.get_current();
- if (currentCT.get_name() == contentTypeName) {
- CTypeID = currentCT.get_stringId();
- break;
- }
- }
- var testList = appCtxSite.get_web().get_lists().getByTitle("SampleList1");
- //Enable the content type in custom list
- testList.set_contentTypesEnabled(true);
- testList.update();
- context.load(testList);
- var webContentTypes = appCtxSite.get_site().get_rootWeb().get_contentTypes();
- var listCollectionCT = testList.get_contentTypes();
- var customCT = webContentTypes.getById(CTypeID);
- //then add the existing content type using content type id
- listCollectionCT.addExistingContentType(customCT);
- //then load the list collection content types
- context.load(listCollectionCT);
- var folder = testList.get_rootFolder();
- context.load(folder, 'ContentTypeOrder');
- context.executeQueryAsync(function () {
- //ordered the content type folder.set_uniqueContentTypeOrder(folder.get_contentTypeOrder().reverse());
- folder.update();
- context.executeQueryAsync(
- function () {
- console.log("Content type was added");
- },
- function () {
- console.log("Error in Adding Content Type" + args.get_message());
- });
- }, function (sender, args) { onfail(sender, args); });
- }, function (sender, args) { onfail(sender, args); });
- }
- // This function is executed if the above call fails
- function onfail(sender, args) {
- alert('Failed to add content type in list. Error:' + args.get_message());
- }
- // this method used split the query string
- function manageQueryStringParameter(paramToRetrieve) {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve) {
- return singleParam[1];
- }
- }
- }
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.

Kaviya BalasubramanianPosted Sep 20, 2016, 5:10 AM
Thank you :)
Tarun DPosted Sep 20, 2016, 4:52 AM
Nice.. Thanks
Kaviya BalasubramanianPosted Sep 19, 2016, 7:38 AM
Thanks Mahesh
Mahesh BabuPosted Sep 19, 2016, 3:02 AM
Good One.