0

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());
        })
    );
}
  • 1
    First thing is check your developer console and see if method is getting called. If so, is it going in success delegate or failure.. what is error message...you have to just use debugging technique to check for issue.. – Siddharth Vaghasia Jun 11 at 9:03
1

Before running the code snippet, need to enable the Content Type Management for the library and add the Content Type needed in library settings,for example, I want to set Content Type "Images" for Item 7 which is an image:

enter image description here

enter image description here

enter image description here

enter image description here

Also, make sure passed valid list name and item Id in the code snippet, this is my test snippet(item Id: 7, list name: doc2):

enter image description here

<script type="text/javascript" language="javascript">
ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
function runCode(){   
    var contenttypeId = '0x0101009148F5A04DDD49CBA7127AADA5FB792B00AADE34325A8B49CDA8BB4DB53328F2140047E654AF66AF2C4699BC331533868D61';
    var lisTitle = "doc2";
    var listitemId = 7;

    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') + '\n' + oListItem.get_item('ContentTypeId');
            alert(listsInfo.toString());
        }),
        Function.createDelegate(this, function(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        })
    );
}
</script>

enter image description here

  • What is listitle`= doc2 ? the name of the file who's content type you want to change ? and what about listitemId = 7 ? – Ed1995 Jun 12 at 10:35
  • listtitleis the document library name, and listItemId is the item Id. Both can be got in the library, please check the capture updated in the post. – Jerry_MSFT Jun 12 at 12:30
  • I followed zour steps, writing the document library name, the item id and also giving the content type id but when I stop editing I get this error. Null:0x010109005A... – Ed1995 Jun 12 at 12:53
  • You will need to set value for the library item title field as listsInfo is getting the title field value and then content type id. Otherwise, it will be null. And this means you have set the content type for the specific item correctly, you can check in Properties. – Jerry_MSFT Jun 12 at 13:06
  • I will update the capture to make you understand clearly. – Jerry_MSFT Jun 12 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.