Showing posts with label content type. Show all posts
Showing posts with label content type. Show all posts

Tuesday, 2 December 2014

Create Content Types in SharePoint 2010 Programmatically in VS 2010



Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint 2010.
 
Question: What is a content type?

In simple terms "Content types are a reusable set of columns which can be used across multiple lists or libraries. It is called central component to manage metadata".

I think we are now good to go and implement this wonderful concept.

Step 1: Open SharePoint 2010 Central Administration and navigate to the specific site.

Step 2: Open up Visual Studio 2010 and try to select an empty sharepoint project:


 Output1.jpg

Step 3: Select deploy as a farm solution and click on the next - button. Now an empty project will be created:

Output2.jpg

Step 4: Add a new visual webpart for that project.

Output3.jpg

Step 5: The complete code of the visualwebpart1usercontrol.ascx looks like this:

<%@ Assembly="" Name="$SharePoint.Project.AssemblyFullName$" %>
       <%@ Assembly="" Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
              <%@ Register="" TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
                     <%@ Register="" TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
                           <%@ Register="" TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
                                  <%@ Import="" Namespace="Microsoft.SharePoint" %>
                                         <%@ Register="" TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
                                                <%@ Control="" Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs"Inherits="ContentTypesProgramaticallyApp.VisualWebPart1.VisualWebPart1UserControl" %>
                                                       <div>
                                                              <center>
                                                                     <table style="font-family: Verdana; font-weight: bold; color: brown;">
                                                                           <tr>
                                                                                  <td>
                                                                                         <asp:Label ID="Label5" runat="server" Text="Please Enter Content Type Name"></asp:Label>
                                                                                  </td>
                                                                                  <td>
                                                                                         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                                                                  </td>
                                                                           </tr>
                                                                           <tr>
                                                                                  <td>
                                                                                         <asp:Label ID="Label1" runat="server" Text="Please Enter First Column Name"></asp:Label>
                                                                                  </td>
                                                                                  <td>
                                                                                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                                                                  </td>
                                                                           </tr>
                                                                           <tr>
                                                                                  <td>
                                                                                         <asp:Label ID="Label2" runat="server" Text="Please Enter Second Column Name"></asp:Label>
                                                                                  </td>
                                                                                  <td>
                                                                                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                                                                  </td>
                                                                           </tr>
                                                                           <tr>
                                                                                  <td>
                                                                                         <asp:Label ID="Label3" runat="server" Text="Please Enter Third Column Name"></asp:Label>
                                                                                  </td>
                                                                                  <td>
                                                                                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                                                                  </td>
                                                                           </tr>
                                                                           <tr>
                                                                                  <td colspan="2">
                                                                                         <asp:Button ID="Button1" runat="server" Text="Create Content Types" BackColor="Orange" onclick="Button1_Click" />
                                                                                  </td>
                                                                           </tr>
                                                                           <tr>
                                                                                  <td colspan="2">
                                                                                         <asp:Label ID="Label4" runat="server"></asp:Label>
                                                                                  </td>
                                                                           </tr>
                                                                     </table>
                                                              </center>
                                                       </div>
 
Step 6: The complete code of the visualwebpart1usercontrol.ascx.cs looks like this:

using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace ContentTypesProgramaticallyApp.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox4.Focus();
        }
        public void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text) || string.IsNullOrEmpty(TextBox4.Text))
            {
                Label4.Text = "Please Enter Some Values";
                Label4.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                SPSite site = new SPSite("http://win-5d57sl2rvt1:9107/");
                SPWeb web = SPContext.Current.Web; SPContentType contentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Item];
                SPContentType content = new SPContentType(contentType, web.ContentTypes, TextBox4.Text);
                web.Fields.Add(TextBox1.Text, SPFieldType.Text, true);
                web.Fields.Add(TextBox2.Text, SPFieldType.Text, true);
                web.Fields.Add(TextBox3.Text, SPFieldType.Number, true);
                SPFieldLink link = new SPFieldLink(web.Fields[TextBox1.Text]);
                SPFieldLink link1 = new SPFieldLink(web.Fields[TextBox2.Text]);
                SPFieldLink link2 = new SPFieldLink(web.Fields[TextBox3.Text]);
                content.FieldLinks.Add(link);
                content.FieldLinks.Add(link1);
                content.FieldLinks.Add(link2);
                web.ContentTypes.Add(content);
                content.Update();
                Label4.Text = "Content Type Created Successfully";
                Label4.ForeColor = System.Drawing.Color.Green;
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
            }
        }
    }
}

Step 7: Deploy the solution file and add the created webpart to the sharepoint site:

Output4.png
 


Step 8: Adding a new item to the list:

Output5.png
 

Step 9: The item added to the list looks like this:

Output7.jpg
 
 I hope this article is useful for you.

Reference:

http://www.c-sharpcorner.com/UploadFile/54db21/create-content-types-in-sharepoint-2010-programmatically-in/

Content Types in SharePoint 2010


Let's start discussing content types in SharePoint 2010. Content Type Hub is a new concept in SharePoint 2010. But before going into more details, let's first discuss briefly what the content types are.

So our agenda for the discussion is:

  1. What are the Content Types
  2. Problem with Content Types in MOSS 2007
  3. Content Type Hub in SharePoint 2010
  4. How to publish content types on Content Type hub
  5. How to expose published content types for the web applications (How to subscribe the published content types)

What are the Content Types?
  1. Content Types are very important and powerful features in SharePoint.
  2. A Content type is a group of site columns (or metadata), workflows and other settings for the item in the list or document in document library or for SharePoint page.
  3. Content types are reusable component, they are not specific to any list or library or page layout.
  4. Content Type represents group of information with common properties. For example, you are maintaining the list of the employees for your company, but there are different details for the employees who are onsite. So here you can create two different content types; one for onsite employees and another for local employees. You can associate these content types with the Employee list. So whenever you enter the details for an onsite employee you will create the list item from onsite content type. Also by going one more step ahead we can create base content type with common fields (site columns) and derive two content types from base content type.

Some More Details:
  1. We can create content types in SharePoint by the following 3 ways:

    a. From UI
    b. Using CAML (Collaborative Application Markup Language)
    c. Using SharePoint Object Model (i.e. Programmatically)
     
  2. We can associate one content types to multiple lists and libraries
  3. SharePoint comes with several OOB content types.
  4. Content types are always inherited; content types are never created from scratch. So an existing content type is always selected as a base content type for creating a new content type. So we automatically get all the columns from the base content type for the new one.
  5. Content Types are stored in the SharePoint database. Databases are created for the web application. We can view all the content types from content type gallery (Site Action >> Site Settings >>Galleries>>Site Content Types). Content Type gallery is the document library.

    Content1.gif

    Figure. Content Type Gallery option

Problem with Content Types in MOSS
  1. Now suppose I have created a few content types for my site collection in WebApplication1. And now I want to use some of the content types created in webApplication1 for WebApplication2. But I don't have such an option to share the content types in multiple web applications. I need to copy and paste all the content types with code and separately deployment on the WebApplication2.
     
  2. Also then there is no synchronization mechanism between the content types. If I do some modifications in content types for web application1 then I need to do these changes also content types available for web application2.

Content Type Hub in SharePoint 2010
  1. To overcome the above problem SharePoint 2010 introduces a new concept called Content Type Hubs.
  2. Content Type Hub is the centralized place where all content types are published and the web applications which will require the content types will subscribe to the content type hub and can get the published content types from the hub.
  3. Web application which subscribes the published content types also can receive the updates on published content types.
  4. So Content Type hub is nothing but the separate common site collection where all content types will get created.
  5. Now to act as this site collection as a content type hub we need to activate the "Content Type Syndication Hub" site level feature.

    Content2.gif

    Figure. Feature activation required for Content Type hub
     
  6. Once we have activated this feature we will get a link to "Manage publishing for this content type" for every content type setting page as:

    Content3.gif

    Figure. Link for Manage Publishing for this content type

Exposing the Content Types published on Content Type Hub to the web applications
  1. Now to expose our content type created on the hub we need to configure Managed Metadata Service.
  2. Enterprise Metadata Management Service (Metadata Service) allows us to share the content types across the farm.
  3. Next, we need to bind the content type hub with the meta data service, for this go to
    Central Administration >> Application Management>>Manage Service Application.

    Content4.gif

    Figure. Manage service applications option
    If metadata service is already created then we will get two entries, one is the service and one is connection

    Content5.gif

    Figure. Manage Metadata Service Application and Connection Links

    Select the metadata service, click on the Meta data service and click properties button.

    On the properties window, put the URL of content type hub site collection.

    Content6.gif

    Figure. Content Type Hub URL option
    Also, click on metadata connection, click on properties button and make sure that "Consumes content types from the Content Type Gallery at <URL of your content type hub site collection>" check box is checked.

    Content7.gif

    Figure. Managed metadata connection

Publishing the Content Type
  1. Now we can publish any content type from the content type hub site collection, to be used in the other web applications for which this Meta data connection is associated.
  2. Go to the content type setting page, click on the link "Manage Publishing for this content type", and make sure that Publish option button is selected.

    Content8.gif

    Figure. Content Type Publishing options
     
  3. Now these publishing of content types are managed by the timer job which is executed after every 15 minutes and then available to the subscriber web application.

    a. Content Type Hub (Server Job)
    b. Content Type Subscriber (Job for the web application which is subscribing the content types)

Reference:

http://www.c-sharpcorner.com/uploadfile/prasham/content-types-in-sharepoint-2010/