Tuesday 12 February 2013

Create Custom Content Type using custom .Net code in SharePoint 2010


Below are the steps to create a custom Content Type using .Net & deploying the package in the SharePoint 2010 Server.

1    1. Open Visual Studio & Select the Empty SharePoint Project.


2. After giving the Proper Name to the Project, the next screen appears asking to deploy the solution as Sandboxed or Farm level. Select Deploy as Farm Solution



3. The Default Solution will look as shown below.


4. In the given solution we need to add a feature to create a custom content type. Right Click on the Features to add a new Feature as shown below.


5. After adding the feature, rename the feature with proper Naming Convention. I have renamed it as Custom_ContentType.


6. We need to add an Event Receiver in the Custom_ContentType feature to create a content type using .Net code.


7. After adding the Event Receiver your solution will look as shown below.


8. We need to write the code in the Custom_ContentType_EventReceiver.cs file to create the content type. The Default code in the Event Receiver file generated on creating it is shown below



Now in the given file we have to add the code to create the feature for activating & deactivating events.

Copy the below given code in the given class file i.e. here it is “public class Custom_ContentTypeEventReceiver : SPFeatureReceiver”

In the below code we have created one custom site column with the unique ID. Hence after copying the below code please create a new ID for site column. Same way we can create a number of custom Site Columns required for the specific content type. We will add this content type to the Announcement list through code itself. To add this content type to other lists we can do so through the site.



public class Custom_ContentTypeEventReceiver : SPFeatureReceiver
{
  //This GUID uniquely idenitifies the custom field
  public static readonly Guid MyFieldId = new Guid("B9EE12F5-B540-4F11-A21B-68A524014C44");
  //This is the XML used to create the field
  public static readonly string MyFieldDefXml ="<Field ID=\"{B9EE12F5-B540-4F11-A21B-68A524014C44}\"" +" Name=\"CustomName\" StaticName=\"CustomName\"" +" Type=\"Text\" DisplayName=\"Custom Name\"" +" Group=\"Custom Content Type Columns\" DisplaceOnUpgrade=\"TRUE\" />";

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
    //Get references to the site and web, ensuring correct disposal
    using (SPSite site = (SPSite)properties.Feature.Parent)
      {
        using (SPWeb web = site.RootWeb)
        {
          //Check if the custom field already exists.
          if (web.AvailableFields.Contains(MyFieldId) == false)
          {
            //Create new field
            web.Fields.AddFieldAsXml(MyFieldDefXml);
            web.Update();
          }
          //Check if the content type already exists.
          SPContentType myContentType = web.ContentTypes["Base Content Type Name e.g Announcements"];
          if (myContentType == null)
          {
            //Our content type will be based on the Annoucement content type
            SPContentType documentcontenttype = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];

            //Create a new content type
            myContentType = new SPContentType(documentcontenttype, web.ContentTypes, "Content Type Using Code 1");

            //Add the custom field to it
            SPFieldLink newFieldLink = new SPFieldLink(web.AvailableFields["Custom Name"]);
            myContentType.FieldLinks.Add(newFieldLink);

            //Add the new content type to the site
            web.ContentTypes.Add(myContentType);

            ////Add it to the Announcements list
            SPList annoucementsList = web.Lists["Announcements"];
            annoucementsList.ContentTypesEnabled = true;
            annoucementsList.ContentTypes.Add(myContentType);
            annoucementsList.Update();
          }

        }
      }
    }

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
      //Get references to the site and web, ensuring correct disposal
      using (SPSite site = (SPSite)properties.Feature.Parent)
      {
        using (SPWeb web = site.RootWeb)
        {
          //get the custom content type
          SPContentType myContentType = web.ContentTypes["Base Content Type Name e.g Announcements"];

          if (myContentType != null)
          {
            ////Remove it from the Announcements list
            SPList annoucementsList = web.Lists["Announcements"];
            annoucementsList.ContentTypesEnabled = true;
            SPContentTypeId ctID = annoucementsList.ContentTypes.BestMatch(myContentType.Id);
            annoucementsList.ContentTypes.Delete(ctID);
            annoucementsList.Update();

            //Remove it from the site
            web.ContentTypes.Delete(myContentType.Id);
            web.Update();

            try
            {
              //Remove the field
              web.Fields.Delete("CustomName");
              web.Update();
            }
            catch
            {
              //Field was not in the collection
            }

          }
        }
      }
    }
}


9. After writing the code, now build & deploy the solution in the SharePoint Site.
In the SharePoint Site after activating the feature, automatically the content type will be created in the site.

No comments:

Post a Comment