Set default contenttype on list, from JSOM
-
03-10-2020 - |
Question
I got some JavaScript that is creating some Lists and Content Types. I am able to set the Content Types on the Lists, however I am not sure how I am suppose to set it as the Default Content Type. So I am pretty much just looking for code snippet that illustrates this.
Solution
Default Content Type is the First Content Type in the list of content types.
You can use set_uniqueContentTypeOrder
for changing the order.
var context = SP.ClientContext.get_current();
var contentTypes = context.get_web().get_lists().getByTitle('ListTitle').get_contentTypes();
var rootFolder = context.get_web().get_lists().getByTitle('ListTitle').get_rootFolder();
context.load(contentTypes);
context.load(rootFolder, 'ContentTypeOrder', 'UniqueContentTypeOrder');
context.executeQueryAsync(onSuccess, onFailed);
function onSuccess(){
contentTypes = context.get_web().get_lists().getByTitle(current.ListTitle).get_contentTypes();
var ctOrder = new Array();
var enum = contentTypes.getEnumerator();
while (enum.moveNext()) {
//Populate ctOrder in your new order by making default content type as the first item
}
rootFolder.set_uniqueContentTypeOrder(ctOrder);
rootFolder.update();
context.executeQueryAsync(success, failed);
};
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange