1

I have a Document Library with classic Experience with a lot of file who's content type I want to change automatically in bulk

Due to the fact that I dont have access to PowerShell I was wondering is there is any way I can change the content type of multiple files using JSOM:

  • Have you tried the code snippet below ? Please share the updates if possible. Thanks – Jerry_MSFT Jun 17 at 0:52
1

As we talked yesterday, here is the modified code snippet to update multiple file item content type:

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(getItemsToBeUpdated, "sp.js");
    var collListItemToBeUpdated = "";
    var listItemToBeUpdated = "";
    function getItemsToBeUpdated() 
    {
         var clientContext = new SP.ClientContext.get_current();
         var oList = clientContext.get_web().get_lists().getByTitle('doc2');
         var camlQuery = new SP.CamlQuery();
         camlQuery.set_viewXml('<View><Query><Where><Eq>' +
            '<FieldRef Name=\'FSObjType\'/><Value Type=\'Integer\'>0</Value>' +
            '</Eq></Where></Query></View>');
         collListItemToBeUpdated = oList.getItems(camlQuery);
         clientContext.load(collListItemToBeUpdated);
         clientContext.executeQueryAsync(getItemsToBeUpdatedSuccess, getItemsToBeUpdatedFailed);
    }
    function getItemsToBeUpdatedSuccess()
    {
         ListItemToBeUpdated= collListItemToBeUpdated.getEnumerator();
         updateMultipleListItems();
    }
    function getItemsToBeUpdatedFailed(sender, args)
    {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    function updateMultipleListItems()
    {   
        var itemArray = [];
        var clientContext = SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('doc2');

         while(ListItemToBeUpdated.moveNext())
         {
            var oItem = ListItemToBeUpdated.get_current();
            var oListItem = oList.getItemById(oItem.get_id());
            oListItem.set_item('ContentTypeId', '0x0101009148F5A04DDD49CBA7127AADA5FB792B00AADE34325A8B49CDA8BB4DB53328F2140047E654AF66AF2C4699BC331533868D61');  
            oListItem.update();
            itemArray.push(oListItem);
            clientContext.load(itemArray[itemArray.length-1]);
         }  
        clientContext.executeQueryAsync(updateMultipleListItemsSuccess, updateMultipleListItemsFailed);
    }
    function updateMultipleListItemsSuccess() 
    {    
        alert('Items Updated');
    }

    function updateMultipleListItemsFailed(sender, args) 
    {    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>

In the code snippet, pass content type Id and list Title we talked yesterday, here is the test result: enter image description here

  • The code seems to be working fine. I changed the library name and the content type I want to change the files into and it updated the files. This content type Id has some columns inside and they seem to be filled randomly using this code. Is there a way I can also choose which option do these columns take ? – Ed1995 Jun 17 at 6:39
  • If you want to change the columns coming from Content Type, you can still use oListItem.set_item('field internalname','value'), you can see this demo for updating column value with JSOM: docs.microsoft.com/en-us/previous-versions/office/developer/… – Jerry_MSFT Jun 18 at 5:43
  • And if the code snippet is working to update content type for Multiple file items, you could accept as answer, so that it could also help others in the forum. Thanks very much. – Jerry_MSFT Jun 18 at 5:44

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.