Navigate Up
Sign In
Supporters of Developer
Web

Attaching Content Types to a List programmatically using the object model

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Attaching Content Types to a List programmatically using the object model

The below PowerShell shows how to add a Content Type to a List programmatically using the object model.

$12HivesDir = "${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12\"
[System.Reflection.Assembly]::LoadFrom("$12HivesDir\ISAPI\Microsoft.SharePoint.dll")

$siteUrl = "http://win-0xqbnqw37uf:8226/";
$listName = "Documents";
$contentTypeId = "0x01";

$site = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$siteUrl";
$web = $site.OpenWeb();

$newContentType = $devweb.ContentTypes[$contentTypeId];
$list = $web.Lists[$listName];
$list.ContentTypesEnabled = $true;
$list.ContentTypes.Add($newContentType);
$list.Update();
$web.Dispose();
$site.Dispose();
Default Content Type
To make a Content Type the default for the list, it has to be at position 0 in the SPContentTypeCollection. You can reorder the ContentTypes, by creating a new SPContentTypeCollection and adding them in the order you want and then set the SPList.ContentTypes property.
Categories:
No categories were selected

Comments

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: