Sunday 18 March 2012

Custom Site Column

First we need to create an empty element object, To do this right click in your Solution explorer, select add a new item and choose an empty element



Click the "Add" button and presto, your ready to make a Site column

Once you have your empty elements file open you're ready to create your custom site column(s) you can start creating fields (columns), below I've created five all with unique ID's

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 
  <Field ID ="{9749FAFE-B58C-4945-85B9-C360C5E309C1}"
         Name ="CoffeeID"
         DisplayName ="Coffee ID"
         Group="Coffee"
         Type="Text"
         EnforceUniqueValues="TRUE"
         Required="TRUE"
         Indexed="TRUE"
           ShowInDisplayForm="TRUE"
         ShowInNewForm="TRUE"
         ShowInEditForm="TRUE"/>
  
  <Field ID="{FE2F4BE0-6FB3-4EC6-922B-FA3641745EC5}"
         Name ="CoffeeRegion"
         DisplayName ="Coffee Region"
         Group ="Coffee"
         Type ="Choice"
         Required ="True"
           ShowInDisplayForm="TRUE"
         ShowInNewForm="TRUE"
         ShowInEditForm="TRUE">
    <CHOICES>
      <CHOICE>Africa</CHOICE>
      <CHOICE>Asia</CHOICE>
      <CHOICE>Europe</CHOICE>
      <CHOICE>North America</CHOICE>
      <CHOICE>Oceania</CHOICE>
      <CHOICE>South America</CHOICE>
    </CHOICES>
  </Field>
 
  <Field ID ="{A4AC9AEE-3FD3-457F-A8D6-50C0E86363B6}"
       Group ="Coffee"
       DisplayName ="Coffee Supplier"
       Name ="CoffeeSupllier"
       Type ="Lookup"
       List="Lists/CoffeeSupplier"
       OverwriteInChildScopes="TRUE"
       Overwrite="TRUE" />
 
  <Field ID ="{4206013B-2B16-441B-A6AD-2E1DAE6C4A86}"
     Name ="CoffeeStartDate"
     DisplayName ="Coffee StartDate"
     Group="Coffee"
     Type="DateTime"
     Format="DateTime">
    <Default>[today]</Default>
  </Field>
 
  <Field ID ="{EFE8441B-8A51-427D-ABF4-D9D4ECB1F2E9}"
         Name ="CoffeePrice"
         DisplayName ="Coffee Price"
         Group ="Coffee"
         Type="Number"
         Required="TRUE" />
</Elements>

And it's that that easy, there are numerous type's of Columns, refer to MSDN for a full list