Creating a Site Column in SharePoint programmatically
Creating a Site Column in SharePoint programmatically
Hi Every one, Today we are going to see Creating a Site Column in SharePoint programmatically.
Site Columns can be created through OOB,Programmatically and Using Power shell.
Site Columns :
Site columns are pre-defined data columns which are re-used in various content types/Lists/Libraries.
For e.g. you can create a site column "Category" with a choice data type along with the pre-defined values "IT","HR". This column then can be added to any content type in your list or library.
A site Administrator can define a specific column of data once and manage it from a central location instead of creating that column many times in many lists and libraries.
this can be done in 2 ways through programmatically.
1. Using Declarative XML
2. thru code (Feature Activation Event Receiver)
In current post we are going to look on
Declarative XML
Step1 :
Open Visual Studio in Admin Mode.
Step2 :
Create an Empty Share Point Project
Step3 :
Add a New Item by choosing Template of Type "Empty Element" and name it as "SiteColumns" in our case.(you can give as per your choice) When you open SiteColumns.XML file it will look as follows,
[xml]
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
//We have to define our column over here...
</Elements>
[/xml]
For each of the data type we will see one example
Single Line of Text :
[xml]
<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>
[/xml]
Multi line of Text :
[xml]
<Field ID ="{860deaeb-80cd-4a22-ab0d-31e49b67ac90}"
Type ="Note"
Name ="ColumnName2"
DisplayName ="ColumnName2"
Group ="SiteColumnGrp"
Required ="TRUE"
AllowDeletion ="TRUE"
Overwrite ="TRUE"
OverwriteInChildScopes="TRUE">
</Field>
[/xml]
Choice :
[xml]
<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>
[/xml]
User :
[xml]
<Field ID ="{453a69be-01af-4399-a3df-22ad55de646d}"
Type ="User"
Name ="ColumnName4"
DisplayName ="ColumnName4"
Group ="SiteColumnGrp"
Required ="FALSE"
AllowDeletion ="TRUE"
Overwrite ="TRUE"
OverwriteInChildScopes="TRUE">
</Field>
[/xml]
Calculated Column :
[xml]
<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>
[/xml]
CheckBox(Yes/No) :
[xml]
<Field ID ="{f54c8ddf-94c5-4ea1-88a4-689ca89b2a1f}"
Type ="Boolean"
Name ="ColumnName10"
DisplayName ="ColumnName10"
Group ="SiteColumnGrp"
Required ="FALSE"
AllowDeletion ="TRUE"
Overwrite ="TRUE"
OverwriteInChildScopes="TRUE">
</Field>
[/xml]
Once your declaration is completed build and deploy your solution.
Navigate to your site to see the Site Columns.
Enjoy... :-)