Search This Blog

Tuesday, March 17, 2015

Develop SharePoint 2010 Content Type


We can create a Content Type using Visual studio 2010 in SharePoint 2010. Follow the below Steps:

Step 1: Starting Project

1) Start Visual Studio 2010
2)Select New from the File menu then select Project
3) From the Installed Templates section, expand Visual Basic or C#, expand SharePoint, and then click 2010.
4) From template pane, select Content Type.
5) In the Name box, type any name of the project. For example - ContentTypeProjectDemo
6) Then click OK.
Visual Studio 2010 New Project
Visual Studio 2010 New Project

Step 2: Setting up Customization Wizard

1) In the What local site do you want to use for debugging? box, select your site.
2) Select the Deploy as a farm solution box. Then, click Next.
SharePoint Customization Wizard: Site and Security Level
SharePoint Customization Wizard: Site and Security Level
3) In the Choose Content Type Settings dialog box, in which base content type should this content type inherit from? list, select Document.
4) Click Finish.
SharePoint Customization Wizard: Content Type Settings
SharePoint Customization Wizard: Content Type Settings

Step 3: Editing content type details in Elements.xml

1) In Solution Explorer, expand ContentType1 and then open Elements.xml.
2) Edit the Name attribute in the <ContentType> tag and type Official Documents.
3) Edit the Description attribute in the <ContentType> tag and type Official Document Content Type.
The markup should look similar to the following.
<?xml version="1.0" encoding="utf-8"?>
<Elements xlms="http://schemas.microsoft.com/sharepoint/" >
<ContentType ID="0x01010055a134438a8544eaa18477c8f21894d0"
  Name="Official Documents"
  Group="Custom Content Types"
  Description="Official Document Content Type"
  Inherits="TRUE"
  Version="0">
<FieldRefs>
</FieldRefs>
</ContentType>
</Elements>

Step 4: Adding field to the Elements.xml file

1. Above the <ContentType> tag, add the following markup. Notice that this includes a new field that will be identified with a new GUID. For this exercise, you can either use the GUID in the following code or create your own GUID.
2. Between the <FieldRefs> tags in the <ContentType> tag, add the following <FieldRef> tag. Ensure that the GUID matches that of the <Field> in the previous step.
<?xml version="1.0" encoding="utf-8"?>
<Elements xlms="http://schemas.microsoft.com/sharepoint/" >
  <Field ID="{30C3D21A-A7C9-410E-A896-82875475F697}" Name="CheckStatus" DisplayName="Check Status" Type ="Choice" >
    <CHOICES>
      <CHOICE>Analysis</CHOICE>
      <CHOICE>Design</CHOICE>
      <CHOICE>Coding</CHOICE>
    </CHOICES>
  </Field>
  <ContentType ID="0x01010055a134438a8544eaa18477c8f21894d0"
    Name="Official Documents"
    Group="Custom Content Types"
    Description="Official Document Content Type"
    Inherits="TRUE"
    Version="0">
    <FieldRefs>
      <FieldRef ID="{30C3D21A-A7C9-410E-A896-82875475F697}" Name="CheckStatus" DisplayName="Check Status"/>
    </FieldRefs>
    </ContentType>
</Elements>

Step 5: To deploy the project

1) In Solution Explorer, right-click the project, and then click Deploy.
2) In SharePoint, in Quick Launch, click Shared Documents.
3) On the Server ribbon, in the Library Tools tab group, click the Library tab.
4) On the ribbon, in the Settings section, click Library Settings.
5) In the General Settings section, click Advanced Settings.
6) In the Content Types section, in the Allow Management of Content Types option, click Yes.
7) At the bottom of the form, click OK.
8) On the List Information page, in the Content Types section, click Add from existing site content types.
Add from existing site content types
Add from existing site content types
9) In the Select site content types from drop-down list, click Custom Content Types.
10) In the Available Site Content Types list, click Official Documents, and then click Add. Then, click OK.
11) On the List Information page, in the Content Types section, click Change new button order and default content type.
12)   Clear the Visible check box for the Document content type, and then click OK.
List of available site content types
List of available site content types

Step 6: Finally

Go to the Shared Document library, click on Add new item; after that upload a document and then in the properties page update the properties of the document, similar to the figure below.
Shared Documents
Shared Documents

To Create Custom Content Type

1. Right-click on the Features folder in Solution Explorer and then click Add Feature.
2. Right-click Feature1 and then click Add Event Receiver. Visual Studio adds a feature event receiver to Feature1.
3. Right-click Feature1.EventReceiver.cs and then click View Code.
4. Uncomment the FeatureActivated method in the Feature1EventReceiver class.
5. Insert the following code in the FeatureActivated method.
using (SPWeb sp = properties.Feature.Parent as SPWeb)
{
    SPContentType newProject = sp.ContentTypes
        .Cast<SPContentType>()
        .FirstOrDefault(c => c.Name == "New Checkpoint");
    if (newProject != null)
    {
        newProject.Delete();
    }
 
    SPField spField = sp.Fields
        .Cast<SPField>()
        .FirstOrDefault(f => f.StaticName == "Projects");
    if (spField != null)
    {
        spField.Delete();
    }
 
    SPContentType contentType =
        new SPContentType(sp.ContentTypes["Checkpoint"], 
            spWeb.ContentTypes, "New Checkpoints");
    contentType.Group = "Custom Content Types";
 
    sp.Fields.Add("Projects", SPFieldType.Text, true);
    SPFieldLink projLink = new SPFieldLink(sp.Fields["Projects"]);
    contentType.FieldLinks.Add(projLink);
 
    SPFieldLink topicFieldLink = new SPFieldLink(sp.Fields["Topic"]);
    contentType.FieldLinks.Add(topicFieldLink);
 
    sp.ContentTypes.Add(contentType);
    contentType.Update();
}
The FeatureActivated method is run when Feature1 is started. This code does the following:
  • Deletes the content type New Announcements and the field Team Project, if they exist.
  • Creates a parent content type Announcement based on the New Announcementscontent type.
  • Creates a text field, which is titled Team Project, and then adds it to the content type.
  • Adds an existing field, which is titled Company, to the content type.
6. UncommenttheFeatureDeactivating method.
7. InsertthefollowingcodeintheFeatureDeactivating method.
using (SPWeb sp = properties.Feature.Parent as SPWeb)
{
    SPContentType contentType = sp.ContentTypes["New Checkpoints"];
    sp.ContentTypes.Delete(contentType.Id);
    sp.Fields["Projects"].Delete();
}

The FeatureDeactivating method is run when Feature1 is deactivated. This code does the following:
  • Deletes the content type New Announcements.
  • Deletes the text field Team Project.
8. In Solution Explorer, right-click CreateContentType and then click Deploy.

Verify That the Project Works Correctly

In this task, you verify the presence of the content type and the two fields.

To Test the Project

1. Start Internet Explorer and browse to the Web site that you specified previously.
2. At the upper-left section of the screen, click Site Actions, and then click Site Settings.
3. Under Galleries, click Site Columns.
4. In the Show Group options, click Custom Columns. You should see the new field Team Project.

Conclusion

SharePoint Products and Technologies come in handy to deliver business critical needs through its enterprise-scale features. Visual Studio 2008 has more templates than Visual Studio 2010. Simplification of creating web part, site definition, content types, etc. makes Visual Studio 2010 an excellent tool in helping to enable more informed-decision making.

No comments:

Post a Comment