Friday, 6 January 2012

Adding a Custom Content Type to a document library with Powershell in Sharepoint 2010

Sometimes you will want to add your own Content Types (custom content types) to your list or document libraries (a list basically…). You can do it with the web interface, it is pretty easy, but what about if you need to perform this for 30,000 sites… you don’t want to spend the rest of the year clicking everywhere.

I have created a function , AddContentTypeToDocumentLibraryRemovingOtherContentType, which allows you to Add a Content Type (“DMSDocument”) and remove the default one (“Documents”). Be aware I will remove “Documents” from my list and add the content type from the site. Whart I am doing here is replacing the default “Document” content type with sophisticated Document content type I developed before called “DMSDocument”

You only need to pass:

  1. Name of the site URL
  2. Name of the Document Library
  3. Name of the Content Type to be added
  4. Name of the Content Type you want to remove from your Document Library.
 $_CurrentURL =  "http://sp_server"
 $_DocumentLibraryName = "Document Centre"
 $_ContentTypeToBeRemoved = "Document"
 $_ContentTypeToBeAdded = "DMSDocument"
 
 Function AddContentTypeToDocumentLibraryRemovingOtherContentType($_sCurrentURL,$_sDocumentLibraryName,$_sContentTypeToBeRemoved, $_sContentTypeToBeAdded)
 { 
 		## Getting the website...
  		$SPWeb = Get-SPWeb -Identity $_sCurrentURL		
		
		## Getting the Document Library (list) and enable Content Types
		$List = $SPWeb.Lists[$_sDocumentLibraryName]
		$List.ContentTypesEnabled = $true
		$List.update()
		
		## Getting the content types to be removed
		## NOTE: This particular one, "Document" lives in our list,
		##       so we remove it from there
		$DocumentCT = $List.ContentTypes[$_sContentTypeToBeRemoved]		
		## This Content type lives in the site, so we get it from there
    	         $CustomCT = $SPWeb.ContentTypes[$_sContentTypeToBeAdded]
    	         $List.ContentTypes.Delete($DocumentCT.Id)
		$List.ContentTypes.Add($CustomCT) 		
		$List.update()	
		
		## Disposing SPWEB object to avoid memory leaks.
		$SPWeb.Dispose()
}
AddContentTypeToDocumentLibraryRemovingOtherContentType $_CurrentURL $_DocumentLibraryName $_ContentTypeToBeRemoved $_ContentTypeToBeAdded

Conclusion: Powershell will bring you a high rate of productivity, but don’t forget to dispose the objects, otherwise you could cause memory leaks in your farm.

3 comments:

ninest123 said...

replica watches, louis vuitton, air jordan pas cher, longchamp pas cher, ray ban sunglasses, nike air max, oakley sunglasses, ugg boots, oakley sunglasses, nike free, nike roshe run, louboutin shoes, michael kors, louboutin, longchamp outlet, louis vuitton outlet, polo ralph lauren outlet, longchamp outlet, tiffany jewelry, tiffany and co, jordan shoes, tory burch outlet, ugg boots, air max, louis vuitton, replica watches, nike free, louis vuitton, uggs on sale, sac longchamp, burberry, chanel handbags, oakley sunglasses, polo ralph lauren outlet, louboutin outlet, longchamp, louis vuitton outlet, prada outlet, cheap oakley sunglasses, oakley sunglasses, christian louboutin outlet, louboutin pas cher, ralph lauren pas cher, ray ban sunglasses, kate spade outlet, ray ban sunglasses, nike air max, prada handbags, gucci outlet, nike outlet

ninest123 said...

burberry outlet online, nike air max, coach purses, nike air max, michael kors, timberland, new balance pas cher, true religion jeans, kate spade handbags, nike free run uk, north face, true religion jeans, ralph lauren uk, air force, hollister pas cher, ray ban uk, converse pas cher, lacoste pas cher, michael kors outlet, michael kors outlet, ugg boots, michael kors outlet, michael kors outlet, coach outlet, vans pas cher, hollister, burberry, coach outlet, nike blazer, oakley pas cher, vanessa bruno, mulberry, ugg boots, lululemon, hermes, tn pas cher, ray ban pas cher, sac guess, hogan, true religion jeans, abercrombie and fitch, true religion outlet, michael kors, nike air max, michael kors outlet, replica handbags, michael kors, nike roshe, north face, michael kors

ninest123 said...

barbour jackets, toms shoes, lancel, supra shoes, juicy couture outlet, moncler, moncler, louis vuitton, thomas sabo, hollister, canada goose uk, canada goose, louis vuitton, doudoune canada goose, converse outlet, canada goose outlet, replica watches, bottes ugg, canada goose outlet, nike air max, gucci, moncler, pandora jewelry, canada goose, karen millen, marc jacobs, converse, moncler, ray ban, wedding dresses, montre pas cher, ugg,ugg australia,ugg italia, moncler, pandora charms, canada goose, moncler, canada goose, ugg,uggs,uggs canada, moncler, vans, swarovski crystal, sac louis vuitton pas cher, pandora jewelry, juicy couture outlet, louis vuitton, louis vuitton, moncler outlet, coach outlet, swarovski, ugg boots uk, links of london, ugg pas cher, pandora charms