0

I am not allowed to change the Content Type of a file using the Quick Edit.

I was wondering if there is any JavaScript code that I can add as a Web Part to the document Library.

This code should change the content type of multiple files.

  • Which SharePoint version are you using? – UBK Jun 5 at 15:33
0

For SharePoint Online, I suggest you change the content type using PnP Powershell.

#Set Variables
$SiteURL = "https://tenant.sharepoint.com/sites/projects"
$ListName = "Project Proposal"
$OldContentTypeName = "Crescent Proposal V1"
$NewContentTypeName = "Crescent Project Proposal V2"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the New Content Type from the List
$NewContentType = Get-PnPContentType -List $ListName | Where {$_.Name -eq $NewContentTypeName}

#Get List Items of Old content Type
$ListItems = Get-PnPListItem -List $ListName -Query "<Query><Where><Eq><FieldRef Name='ContentType'/><Value Type='Computed'>$OldContentTypeName</Value></Eq></Where></Query>"
Write-host "Total Number of Items with Old Content Type:"$ListItems.count

ForEach($Item in $ListItems)
{
    #Change the Content Type of the List Item
    Set-PnPListItem -List $ListName -Identity $Item -ContentType $NewContentType
}

Reference: SharePoint Online: Change Content Type of List Items using PowerShell

If you can't use PowerShell, we can also use JSOM to achieve it. The following code snippet for your reference.

<script type="text/javascript" language="javascript">
ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
function runCode(){   
    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');
            alert(listsInfo.toString());
        }),
        Function.createDelegate(this, function(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        })
    );
}
</script>
  • I was hoping it to be something that I ca run within sharepoint as a webpart. I don't have access to Powershell – Ed1995 Jun 6 at 9:40
  • If you can't use PowerShell, we can also use JSOM to achieve it, check my reply. – LZ_MSFT Jun 7 at 0:51
  • I'm out of house for a couple of days. I will try it as soon as I'm back. Can you tell me how this code would work. Do I just have to copy the code to webpart inside document library and ofc change the content type id – Ed1995 Jun 7 at 9:57
  • when I add the code in the scripteditor on the snippet; do I have to add at the beggining of the code ''<script type="text/javascript">'' . How is the code supposed to work, how will it change the content type ? – Ed1995 Jun 11 at 8:32
  • I modify the JSOM code in the reply, you can check it, add the code into script editor web part. – LZ_MSFT Jun 11 at 9:14

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.