Code for adding Content Type through Visual Studio (Windows Form- For SharePoint 2010)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;


namespace Demo
{
    public partial class Form1 : Form
    {
        private const string siteurl = "http://win-5dlgan2f7s8:21905/CrimsonConsultingHR/Demo/";
        public Form1()
        {
            InitializeComponent();
        }
  private void CreatecontentType_Click(object sender, EventArgs e)
        {
            using (var site = new SPSite(siteurl))
            {
                var web = site.RootWeb;

                var empFieldName = web.Fields.Add("Employee", SPFieldType.Boolean, true);
                var employee = web.Fields.GetFieldByInternalName(empFieldName);
                employee.Group = "AayushContentType";
                employee.Update();


                var rateFieldName = web.Fields.Add("Salary/Rate", SPFieldType.Currency, false);
                var rate = web.Fields.GetFieldByInternalName(rateFieldName);
                rate.Group = "AayushContentType";
                rate.Update();

                var bioFieldName = web.Fields.Add("Bio", SPFieldType.Note, false);
                var bio = web.Fields.GetFieldByInternalName(bioFieldName) as SPFieldMultiLineText;
                bio.Group = "AayushContentType";
                bio.NumberOfLines = 6;
                bio.RichText = false;
                bio.Update();

                var item = web.ContentTypes["Item"];
                var author = new SPContentType(item, web.ContentTypes, "Authors");
                author.Group = "AayushContentType";

                var Title = author.FieldLinks["Title"];
                Title.DisplayName = "Name";
                author.FieldLinks.Add(new SPFieldLink(employee));
                author.FieldLinks.Add(new SPFieldLink(rate));
                author.FieldLinks.Add(new SPFieldLink(bio));

                web.ContentTypes.Add(author);
                author.Update();

                listBox1.Items.Add("Content Type created");
            }
        }

        private void DeletecontentType_Click(object sender, EventArgs e)
        {
            using (var site = new SPSite(siteurl))
            {
                var web = site.RootWeb;
                var list = web.ContentTypes["Authors"];


                if (list != null)
                {
                    list.Delete();

                }
                DeleteField(web, "Employee");
                DeleteField(web, "Salary_x002F_Rate");      //_x002F_ is used for '/'
                DeleteField(web, "Bio");
                listBox1.Items.Add("Content Type deleted");

            }
        }
        private void DeleteField(SPWeb web, string FieldName)
        {
            var field = web.Fields.GetFieldByInternalName(FieldName);
            field.Delete();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (var site = new SPSite(siteurl))
            {
                var web = site.RootWeb;

                var ListId = web.Lists.Add("Authors", string.Empty, SPListTemplateType.GenericList);
                var List = web.Lists[ListId];
                List.OnQuickLaunch = true;

                List.ContentTypesEnabled = true;

                var author = List.ContentTypes["Author"];
                List.ContentTypes.Add(author);
                List.ContentTypes.Delete(List.ContentTypes["Item"].Id);
                List.Update();

                var view = List.DefaultView;
                view.ViewFields.Add("Employee");
                view.ViewFields.Add("Salary_x002F_Rate");
                view.Update();

                listBox1.Items.Add("Author list created");
            }
        }
}

Comments

Popular posts from this blog

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

go to top image