Content Types

SharePoint mixing up Folders and Document Sets with WebDAV

When you create a document library, the document library (by default) has two content types – Document, and Folder. Folder is hidden from you – but when you list the content types of the library in Powershell, the following will be reported:

  • Document
  • Folder

When you create a folder in WebDAV, the Folder Content Type is chosen by SharePoint automatically. Interestingly, if you use powershell to remove the Folder content type, and then create another folder via WebDAV, it will have NO content type listed, even though the folder will still work as a folder in the library.

Here’s the catch – and how you can confuse SharePoint:

If you remove the Folder Content Type, and add a Document Set Content Type, and then go to WebDAV to create a folder, the folder will get the Document Set content type. The reason for this appears to be in the way SharePoint chooses the folder content type – Folders and Document Set IDs (GUIDs) both begin “0x0120”. It looks like SharePoint finds the document set content type, and uses it for the folder. It even does it if you add the folder content type back, which points towards SharePoint interrogating the content types in the order they were applied to the library.

The solution is to re-write the UniqueContentTypeOrder for the RootFolder of the library. It is a generic list of Content Type IDs – and you must be careful to use the IDs of the List Content Types – not the Web or Site Content Types, because their GUIDs will differ.

Posted by Jonathan Beckett in Notes, 0 comments

Creating SharePoint Content Types with PowerShell

The following snippet creates a Content Type in SharePoint on-prem. Notice that the code checks for the existence of the content type before creating it – because typically you will be using this in a larger provisioning script, and it makes sense to let your code run more than once – so as you build a solution, you can just add new elements and run it again.

# Connect to SharePoint Site
$web = Get-SPWeb "https://server/sites/sitecollection/subsite"

# Set Content Type name and group
$content_type_name = "My Content Type"
$content_type_group = "My Content Types"

if ($web.ContentTypes[$content_type_name]){
    
    # get the existing content type
    $content_type = $web.ContentTypes[$content_type_name]

} else {
    
    # create a new content type
    $content_type_parent = $web.AvailableContentTypes["Item"]
    $content_type = new-object Microsoft.SharePoint.SPContentType($content_type_parent, $web.ContentTypes, $content_type_name)
    $content_type.Group = $content_type_group
    $web.ContentTypes.Add($content_type) > $null
    $content_type = $web.ContentTypes[$content_type_name]
}

# Release resources
$web.Close()
$web.Dispose()

Also notice that the content type will be provisioned in the web you connect to – but must be uniquely named across the entire site collection.

Posted by Jonathan Beckett in Notes, 0 comments