I was looking for ways I can automatically change the content type of my files inside SharePoint online. I go a code from Here
I'm not sure how the code is supposed to work. Added a Webpart, scripteditor on my document library.
At the part in the code where is says contenttypeId, I've pasted the content type id I want the files to be change to.
Nothing is happening. Not sure how the code is supposed to work.
Here is the code:
var contenttypeId = '0x01040005B1FCA568800F4FB4162C7A09742E67';
var lisTitle = "TestNewList";
var listitemId = 1;
UpdateListItem(lisTitle, listitemId, contenttypeId);
function UpdateListItem(listTitle, listitemId, contenttypeId) {
var clientContext = new SP.ClientContext();
oList = clientContext.get_web().get_lists().getByTitle(listTitle);
oListItem = oList.getItemById(listitemId);
oListItem.set_item('ContentTypeId', contenttypeId);
oListItem.update();
clientContext.load(oListItem, 'Id', 'Title', 'ContentTypeId');
clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
var listsInfo = '';
listsInfo += oListItem.get_item('Title') + '\t' + oListItem.get_item('ContentTypeId');
console.log(listsInfo.toString());
}),
Function.createDelegate(this, function(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
})
);
}