Prash's Blog

Programmatically Enabling Content Management, Checkout before edit, Content Approval and Versioning on Documents July 10, 2009

Filed under: SharePoint — prazjain @ 9:53 pm
Tags: ,

In this blog I would be posting about how to programmatically enable Content Management on a document library, how to enable versioning on documents, and making sure that the user checks out a file before it is edited.

These were some of the tasks that I had to perform a few days back and being a sharepoint novice I spent quite some time finding how that could be done.

But to my surprise I found that each of these three can be enabled by setting a simple boolean property.

First get an instance of SPWeb object that you want to work on, then

1) Enabling Content Management on a document library :

// web is instance of SPWeb
SPList docList = web.Lists["Custom List"];
// set the bool property
docList.ContentTypesEnabled = true;
// now you are ready to add your custom content types
// first get instance to your custom content type
SPContentType customContentType = web.Site.RootWeb.ContentTypes["Custom Content Type"];
docList.ContentTypes.Add(customContentType);
// and never forget this
docList.Update();

2) Enabling Versioning on documents :

// get reference to the document library in which you want to enable versioning
// web is instance of SPWeb
SPList docList = web.Lists["Custom List"];
// enable both major and minor versioning flags like this (if you want both major and minor versioning)
docList.EnableVersioning = true;
docList.EnableMinorVersions = true;

// and do not forget this
docList.Update();

3) Enabling Content Approval on documents : (in a later post here I would explain how elevated privileges would need to be used to use content approval correctly)

// get reference to the document library in which you want to enable content approval
// web is instance of SPWeb
SPList docList = web.Lists["Custom List"];
// enable content approval like this
docList.EnableModeration = true;
// and do not forget this
docList.Update();

4) Forcing Checkout of Document before edit :

// get reference to the document library in which you want to enable versioning
// web is instance of SPWeb
SPList docList = web.Lists["Custom List"];
// enable force checkout before edit like this
docList.ForceCheckout = true;
// and do not forget this
docList.Update();
Advertisements
 

One Response to “Programmatically Enabling Content Management, Checkout before edit, Content Approval and Versioning on Documents”

  1. […] 1) To know how to enable content approval on a list programmatically, check the article here. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s