Part III: Using Feature Event Receiver to create a library and associate a content typeIn previous post we showed how to use ContentTypeBinding to bind the content type to an existing document library. In this post, we will learn how to achieve similar results but using the feature event receiver. In this post we will create a new document library, remove all existing content types from the created library and then add our content type to the list. Instead of create a new library from the code, you could also reference an existing one that’s already on the SharePoint server. In this example we will use the same project we created in Part I. So, let's start by opening that project which contains the content type and a document. When project opens, expand the Features folder, right click the Feature1 folder and finally select the Add Event Receiver option to add a feature event receiver to the project. Next, uncomment the FeatureActivated method and add the following code:
SPWeb web = properties.Feature.Parent as SPWeb;
string libraryName = "Sample library name - " + DateTime.Now.Millisecond.ToString ();
Guid listGuid = web.Lists.Add(libraryName, string.Empty, SPListTemplateType.DocumentLibrary);
SPList list = web.Lists[listGuid];
// Delete all content types from the list
for (int i = 0; i < list.ContentTypes.Count; i++)
SPContentType myContentType = web.ContentTypes[new SPContentTypeId("0x010100a9fe27b0556b4de8a60d9e04c6db71de")];
list.ContentTypesEnabled = true;
In the above code we are adding a library to the SharePoint site. The Add method returns the Guid of the created list/library. In the for statement, we iterate through all the content types that are in the list (since we created a new document library there should be only 1 content type (Document) in the library) and we delete them.
After we removed all content types from the list, we add a new content type to the list (similar as in Part II, we are referring to the content type by its ID). You can use the Server Explorer to get the content type ID. Finally, we enable the content type management in the list and we update the list to save the changes.
If you press F5 you will notice that the "My New Document Library" is created on SharePoint. If you click the Documents tab and then open the New Document menu you will notice a document we associated with the content type shows up.
The Visual Studio team that brought you the SharePoint developer tools in Visual Studio 2010 is happy to announce we have created a set of power tools that make developing SharePoint 2010 sandboxed solutions even easier. Below is the list of features included in the current release of the power tools.
Sandboxed-compatible Visual Web Part This item template enables you to use a visual designer to create SharePoint web parts that can be deployed in a SharePoint 2010 sandboxed solution.
Sandboxed Compilation This extension displays build errors when you use types or members in a SharePoint 2010 sandboxed project which are not allowed in the SharePoint sandbox environment.
The Visual Studio 2010 SharePoint Power Tools are available immediately on the Visual Studio Gallery.
Mike Morton - Program Manager - Visual Studio
Using ContentTypeBinding to associate content types with lists
In previous post we showed how to deploy a document and associate it with a content type. Even though the deploying and associating the document with content types was fairly easy we still had to manually associate the content type with desired document library. In this post I will show you how to use the ContentTypeBinding element to associate a content type to the existing document library.
You can use the content type project we created in previous post. Currently, we are deploying the document and a content type and then with the help of the DocumentTemplate element we are associating it to the content type. In order to automatically associate or bind the content type with an existing document library we need to create an Empty Element project item.
1. Right click the project name and select AddàNew Item.
2. From the list of templates, select Empty Element.
3. Name the project item "ContentTypeBinding" and click OK to create it.
A new folder with an empty Elements.xml file is added to the project. Double click the Elements.xml in the ContentTypeBinding folder we just added. As mentioned at the beginning, we are going to use the ContentTypeBinding element. This element can be used to bind a specific content type to an existing list or library. Let's create a new Document Library from the SharePoint home page and call it "My Documents". Switch back to the Visual Studio 2010 to add the ContentTypeBinding element to the empty Elements.xml file:
<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><ContentTypeBinding ListUrl="My Documents" ContentTypeId="[CONTENT TYPE ID]"/> </Elements>
Explanation for the above snippet is very simple. We are pointing to the list and the content type ID we want to associate to that list. To get the content type ID you can either open the Elements.xml file inside the content type folder or use the Server Explorer:
1. In Visual Studio 2010, click Tools à Add SharePoint Connection item.
2. In the dialog that opens type the SharePoint URL and click OK to open the Server Explorer.
3. In the Server Explorer window expand the Team Site and Content Types node.
4. Locate the content type you want to associate (e.g. MyContentType - ContentType1).
5. With the desired content type selected, click F4 to open the Properties Window.
6. Locate the ID property - that's the content type ID we need for ContentTypeBinding.
7. Copy the ID and paste it to the empty elements' Elements.xml file to replace the [CONTENT TYPE ID] value.
We are all done! Press F5 to start debugging and navigate to the "My Documents" library. Click the Documents tab and then the New Document menu. You will notice the document we deployed with our solution now shows up in the menu which means that the content type was successfully associated with this document library. In the next post we will show how to achieve similar functionality (associate a content type with a document library) using feature event receivers.