Code Play and Learn

Create Site Column Programmatically in SharePoint

Advertisements

Hi

Today I am going to explain you How to create Site column Programmatically (xml)

It should be also created through OOTB and PowerShell

What is site column

Site column is pre define site column which are reused in various content type and List, Libraries

For e.g. you can create a site column “Sample” with a Single Line of text data type . This column then can be added to any content type in your list or library.

We can achieve it two ways

Using Declarative XML

code (Feature Activation Event Receiver)

In this blog we can achieve it through xml..

Ok let`s start

Step1:

Open VS with Run As administrator

Step2:

Create Empty Sharepoint project

Step3:

Add a New Item by choosing Template of Type “Empty Element” and name it as “Site Columns” in our case.(you can give as per your choice) When you open Element.XML file it will look as follows,(VS2012/3 add Sitecolumn and in VS2010 add Element.xml)

<?xml version=”1.0″ encoding=”utf-8″?>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>

//We have to define our column over here…
 
<Field ID =”{fc571c6c-51ee-48df-953d-2464628d741b}”// GUID
       Type =”Text”//Data Type
       Name =”Answer”//Internal Name
       DisplayName =”Answer”//Display Name
       Group =”SiteColumnGrp”//Group Name of the Site Columns
       Required =”FALSE” //Mandatory Column or not
       AllowDeletion =”TRUE” //allows deletion
       Overwrite =”TRUE” //overwrites changes
       OverwriteInChildScopes=”TRUE”>
</Field>
 
</Elements>

Once your declaration is completed build and deploy your solution.
Navigate to your site  open site setting- site column .Find Group Name SiteColumnGrp

You can Find Column named Answer
Enjoy…

Below is Some More type example of Site column

Multiline of text

<Field ID =”{860deaeb-80cd-4a22-ab0d-31e49b67ac90}”
        Type =”Note”
        Name =”ColumnName2″
        DisplayName =”ColumnName2″
        Group =”SiteColumnGrp”
        Required =”TRUE”
        AllowDeletion =”TRUE”
        Overwrite =”TRUE”
        OverwriteInChildScopes=”TRUE”>
</Field>
 

Choice

<Field ID =”{a51e39c7-0e12-4359-961d-3bcb718ad8bd}”
       Type =”Choice”
       Name =”ColumnName3″
       DisplayName =”ColumnName3″
       Group =”SiteColumnGrp”
       Required =”FALSE”
       AllowDeletion =”TRUE”
       Overwrite =”TRUE”
       RichText=”FALSE”
       OverwriteInChildScopes=”TRUE”>
      <CHOICES>
        <CHOICE>Yes</CHOICE>
        <CHOICE>No</CHOICE>
      </CHOICES>
 </Field>
 

User

<Field ID =”{453a69be-01af-4399-a3df-22ad55de646d}”
       Type =”User”
       Name =”ColumnName4″
       DisplayName =”ColumnName4″
       Group =”SiteColumnGrp”
       Required =”FALSE”
       AllowDeletion =”TRUE”
       Overwrite =”TRUE”
       OverwriteInChildScopes=”TRUE”>
</Field>

Calculated

<Field ID=”{c63ca9d6-542a-489e-ac91-8b4399a2e62b}”
       Type=”Calculated”
       Name=”ColumnName7″
       DisplayName=”ColumnName7″
       Format=”DateOnly”
       ResultType=”Text”
       Group=”SiteColumnGrp”
       Required=”TRUE”
       AllowDeletion=”TRUE”
       Overwrite=”TRUE”
       OverwriteInChildScopes=”TRUE”>
       <Formula>=DATEDIF(Col1,Col2,”d”)</Formula>
       <FieldRefs>
         <FieldRef Name=”Col1″></FieldRef>
         <FieldRef Name=”Col2″></FieldRef>
       </FieldRefs>
 </Field>

Checkbox(Yes/No)

<Field ID =”{f54c8ddf-94c5-4ea1-88a4-689ca89b2a1f}”
       Type =”Boolean”
       Name =”ColumnName10″
       DisplayName =”ColumnName10″
       Group =”SiteColumnGrp”
       Required =”FALSE”
       AllowDeletion =”TRUE”
       Overwrite =”TRUE”
       OverwriteInChildScopes=”TRUE”>
</Field>

In my Next Blog  I will Explain you Create Content type programatically with reference of this blog

Thanks SPS

Advertisements