Creating SharePoint Site Columns Programatically

Leave a Comment
Hi All,

The following example will demonstrate ,how we can create the site columns programatically in efficient way
As we know we can do it by OOB.

In Programming way also this can do done in two ways
1 ) In Declarative XML
2 ) through code(event receiver)
Both can be done in the feature activation.

It is easy with Declarative XML way.
Let us discuss one after the other.

1) Open VS2010 in Admin Mode.
2)Create an Empty sharepoint project
3) add a new template of type "Empty Element"
    here we have elements.xml file.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

We have to define our column over here...

</Elements>

For Each of the Data Type one example is provided.

Single Line of Text:
 <Field ID ="{fc571c6c-51ee-48df-953d-2464628d741b}"// GUID
         Type ="Text"//DataType
         Name ="Answer"//Internal Name
         DisplayName ="Answer"//DisplayName
         Group ="SiteColumnGrp"//GroupName of the Site Columns
         Required ="FALSE" //Mandatory Column or not
         AllowDeletion ="TRUE" //allows deletion
         Overwrite ="TRUE" //overwrites changes
         OverwriteInChildScopes="TRUE"></Field>
in the above example the highlighted(Bold) is the Unique GUID.GUID Generation

MulitiLine 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>

HyperLink:
 <Field ID ="{5e86344f-8a18-4e72-9117-bcee35b72430}"
Type ="URL"
 Name ="ColumnName5"
DisplayName ="ColumnName5"
Format="Image"
Group ="SiteColumnGrp"
Required ="FALSE"
AllowDeletion ="TRUE"
Overwrite="TRUE"
OverwriteInChildScopes="TRUE"></Field>

Publishing Image:
<Field ID ="{de2f4df0-b742-40ee-86d7-2fcd05ecbd8e}"
Type ="Image"
Name ="ColumnName6"
DisplayName ="ColumnName6"
RichText="TRUE"
RichTextMode="ThemeHtml"
Group ="SiteColumnGrp"
Required ="FALSE"
AllowDeletion ="TRUE"
Overwrite ="TRUE"
OverwriteInChildScopes="TRUE"></Field>

Calulated Column:
<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>

DateTime:
<Field ID="{1527fda8-5fa8-482c-a317-f3c331d75a51}"
Type="DateTime"
Name="ColumnName8"
DisplayName="ColumnName8"
Group="SiteColumnGrp"
Required="FALSE"
AllowDeletion="TRUE"
Overwrite="TRUE"
OverwriteInChildScopes="TRUE"
Format="DateOnly"></Field>

MultiUser:
<Field ID ="{54a38554-eede-4691-8f0d-f60a94e78823}"
Type ="UserMulti"
Name ="ColumnName9"
DisplayName ="ColumnName9"
Group ="SiteColumnGrp"
Required ="FALSE"
AllowDeletion ="TRUE"
Overwrite ="TRUE"
OverwriteInChildScopes="TRUE"
Mult="TRUE"
 UserSelectionMode="PeopleAndGroups"></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 Calculated Formula case write formulae according to your requirement.
Elements.xml file will be called when the feature is activated.When the feature is activated at that time site columns will be created.

Related Post

0 comments:

Post a Comment