Prash's Blog

Programmatically adding Custom folder content type and Custom document content type in SharePoint document library July 9, 2009

Filed under: SharePoint — prazjain @ 12:38 am
Tags: ,

Part 2 of the Two Part Article Series

In this post I would focus more on creating a custom document library and attaching the custom types to the list, Part 1 of the article explains creating those custom content types.

In this post I would be explaining how to add a custom type extending from Folder type in Sharepoint, to a custom list in my site.
And then I would be show how creation of that folder type can be allowed through the new button in menu, and hiding the other new buttons available there.like : Document, Folder

a) Create feature.xml and put it in 12 hive /TEMPLATE/FEATURES/MyFeature/feature.xml. Remember to change the GUID of feature below to a new GUID, and change the public key token of the assembly mentioned below in Feature tag to the token generated after signing your assembly.

<Feature Id="7a6dE9DB933A-DA89-4E86-A072-6CA8D24A68de"        
 Title="My Feature"        
 Description="Some description to go here"        
 Version="1.0.0.0"        
 Hidden="TRUE"        
 Scope="Web"        
 ReceiverAssembly="Geeks.SharePoint.SampleAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=78A0b8264297E9DB"
 ReceiverClass="Geeks.SharePoint.SampleAssembly.MyClass"        
 xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests></ElementManifests>
</Feature>

b) Create a feature receiver event listener so that you can override the event callback and perform actions when the feature is being activated.

Create a project with name “Geeks.SharePoint.SampleAssembly”, and create a class MyClass in namespace “Geeks.SharePoint.SampleAssembly”. Ofcourse you would have to strong name this assembly and install it in GAC.

The code provided below is fairly well commented so I hope you would not have problem understanding it.


namespace Geeks.SharePoint.SampleAssembly
{
 public class MyClass : SPFeatureReceiver
 {
 public override void FeatureActivated(SPFeatureReceiverProperties properties)
 {
 //get the current web
 SPWeb web = null;
 web = (SPWeb)properties.Feature.Parent;

 //add the document library
 AddDocumentLibs(web);

 //add the content types for the custom document library just created
 AddContentType(web);
 }

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
 {
 //not needed
 }

 public override void FeatureInstalled(SPFeatureReceiverProperties properties)
 {
 //not needed
 }

 public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
 {
 //not needed
 }
 private void AddDocumentLibs(SPWeb web)
 {
 Guid customListGuid = web.Lists.Add("My Custom List", "My Custom List Description", SPListTemplateType.DocumentLibrary);

 web.Update();

 SPList custList = web.Lists[customListGuid];

 // show the list in quick launch
 custList.OnQuickLaunch = true;
 custList.Update();

 // set both properties to true if you want both major and minor versioning to be turned on.
 custList.EnableVersioning = true;
 custList.EnableMinorVersions = true;
 custList.Update();
 }

 private void AddContentType(SPWeb web)
 {
 // get the custom list to which we would register content types
 SPList docList = web.Lists["My Custom List"];

 SPContentType customFolderType = web.Site.RootWeb.ContentTypes["My Custom Folder Type"];

 SPContentType customDocType = web.Site.RootWeb.ContentTypes["My Custom Doc Type"];

 SPContentType DefaultType = docList.ContentTypes["Document"];

 // set this property to allow management of content types programmatically.
 docList.ContentTypesEnabled = true;

 // register content type customDocType as instance of SPContentType
 docList.ContentTypes.Add(customDocType);

 // register content type customFolderType as instance of SPContentType
 docList.ContentTypes.Add(customFolderType);</pre>
 docList.Update();

 // just to show how you can hide the new Folder option from your menu in document list
 HideContentTypes(docList.RootFolder, "Folder");

 // remove the default Document that new menu allows to create, as we want only our custom doc to be created from new menu in this custom list.
 docList.ContentTypes.Delete(DefaultType.Id);

 docList.Update();

 // create a custom folder type programmatically in the document library when this feature is activated
 CreateFolder(docList.RootFolder, "My First Folder", customFolderType.Id);

 }

 // Creates a new folder in the specified root folder with the content type that matches the Id passed as parameter
 private void CreateFolder(SPFolder rootFolder, string folderName, SPContentTypeId contentTypeId)
 {
 SPFolder newFolder = rootFolder.SubFolders.Add(folderName);
 newFolder.Item.SystemUpdate();
 newFolder.Update();
 newFolder.Item[SPBuiltInFieldId.ContentTypeId] = contentTypeId;
 newFolder.Item.SystemUpdate();
 }

 // This method goes through the content types available for creation in the root folder. Then it goes and hides the content type
 // that is passed as argument by just creating a new collection that holds SPContentType and assigning that collection back to
 // root folder's UniqueContentTypeOrder. This is a special property that also allows changing the order of content types that appear in
 // the new menu option.
 private void HideContentTypes(SPFolder rootFolder, string contentTypeName)
 {
 IList<SPContentType> contentTypeCollection = rootFolder.ContentTypeOrder;

 List<SPContentType> contentTypes = new List<SPContentType>();

 foreach (SPContentType contentType in contentTypeCollection)
 {
 if (contentType.Name.Equals(contentTypeName))
 continue;
 else
 contentTypes.Add(contentType);
 }

 rootFolder.UniqueContentTypeOrder = contentTypes;
 rootFolder.Update();
 }

Hope that helps.

Want to go back to Part 1 ?

Advertisements
 

5 Responses to “Programmatically adding Custom folder content type and Custom document content type in SharePoint document library”

  1. […] Part 2 : This would explain about you can associate those content types programmatically […]

  2. Dima Says:

    Respect!!!!

  3. goran krasojevic Says:

    Bravooooo. I need it. Thanks….

  4. jossamah Says:

    Excellent post! I have one question for you. When I deploy my solution my “custom folder content type” appears in the “New Document” button. Question is, Do you know how to bind the “New Folder” button to the “custom folder form” ?

    Thank you!

    • prazjain Says:

      Jossamah, glad you like the post. Mate I can suggest that you add a new menu item with name “New Folder” and then add the “custom folder” option under it. You can check it in my post about how to add new menu item., after that change its name to “New Folder” (attach a new icon if needed).
      But I will not be able to suggest specifics as I did sharepoint for just 3 months, and it has been 2 years I last did sharepoint.
      Hope that helps.


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