Posted on:
Categories: SharePoint;PowerShell
Description:

Scenario

​We created a PowerShell script to provision a site with custom columns, content types, lists and libraries. The Document content type was removed from the document libraries and a custom content type was added to them. When testing the newly provisioned site we encountered an issue in which certain document types (ie. .doc, .xls, .msg) uploaded to these document libraries would be missing the expected metadata fields to fill in. Further investigation revealed that these documents were being set as the Folder content type and not the custom content type we added to the document library.

 

The Cause

In our PowerShell script, we were deleting the document library's default Document content type before adding the new content type. When you do this in that order, it causes some document file types to be set as Folder.

This behaviour is applicable to both SharePoint 2010 and SharePoint 2013.

Solution

In our case, we updated our site provisioning script to add the new content type first before deleting the Document content type from the document library.

If your document libraries are already in use, Steven Van de Craen has outlined two solutions in his blog:

Solution 1: Re-add the Document content type to the library and delete it again

Solution 2: Set the default content type by updating the list's content type order

Here is a script to set the default content type of a list in PowerShell:

 

$web = Get-SPWeb "http://siteURL"
$list = $web.Lists["MyDocumentLibrary"]     
$contentTypes = $list.ContentTypes
$result = New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
foreach ($ct in $contentTypes)
{
 if ($ct.Name -eq "MyContentType")
 {
  $result.Add($ct)
 }
}
$list.RootFolder.UniqueContentTypeOrder = $result
$list.RootFolder.Update()

 

 
To avoid this issue in the future, just remember to add new content types first before deleting the default Document content type from a document library. Cheers!

References

Steven Vande Craen's blog
Dot Net Spark code snippet