SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Creating Site Columns Programmatically via XML

In this post I demonstrate how to create Site Columns Programatically via XML (Quick Note)

Hi folks,

From now on I will also post some quick notes of SharePoint development. I believe they are going to be very useful in our daily activities.

In this post I demonstrate how to create Site Columns programatically via XML.

Consider the following Site Columns XML:

Code Snippet
  1. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  2.   <Field Type="Choice" Description="List of countries" Required="TRUE" Format="Dropdown" FillInChoice="FALSE" Group="World Cup 2010 Columns" ID="{05A571AD-F9D1-4aab-A703-1AF3AE393FBF}" StaticName="Country" Name="Country" DisplayName="Country" Version="1">
  3.     <CHOICES>
  4.       <CHOICE>Algeria</CHOICE>
  5.       <CHOICE>Argentina</CHOICE>
  6.       <CHOICE>Australia</CHOICE>
  7.       <CHOICE>Brazil</CHOICE>
  8.       <CHOICE>Cameroon</CHOICE>
  9.       <CHOICE>Chile</CHOICE>
  10.       <CHOICE>Côte d'Ivoire</CHOICE>
  11.       <CHOICE>Denmark</CHOICE>
  12.       <CHOICE>England</CHOICE>
  13.       <CHOICE>France</CHOICE>
  14.       <CHOICE>Germany</CHOICE>
  15.       <CHOICE>Ghana</CHOICE>
  16.       <CHOICE>Greece</CHOICE>
  17.       <CHOICE>Honduras</CHOICE>
  18.       <CHOICE>Italy</CHOICE>
  19.       <CHOICE>Japan</CHOICE>
  20.       <CHOICE>Korea DPR</CHOICE>
  21.       <CHOICE>Korea Republic</CHOICE>
  22.       <CHOICE>Mexico</CHOICE>
  23.       <CHOICE>Netherlands</CHOICE>
  24.       <CHOICE>New Zealand</CHOICE>
  25.       <CHOICE>Nigeria</CHOICE>
  26.       <CHOICE>Paraguay</CHOICE>
  27.       <CHOICE>Portugal</CHOICE>
  28.       <CHOICE>Serbia</CHOICE>
  29.       <CHOICE>Slovakia</CHOICE>
  30.       <CHOICE>Slovenia</CHOICE>
  31.       <CHOICE>South Africa</CHOICE>
  32.       <CHOICE>Spain</CHOICE>
  33.       <CHOICE>Switzerland</CHOICE>
  34.       <CHOICE>Uruguay</CHOICE>
  35.       <CHOICE>USA</CHOICE>
  36.     </CHOICES>
  37.   </Field>
  38.   <Field Type="Choice" Description="List of host cities" Required="TRUE" Format="Dropdown" FillInChoice="FALSE" Group="World Cup 2010 Columns" ID="{AF008077-3A7E-41d9-AACA-2B0997BB5E25}" StaticName="HostCity" Name="HostCity" DisplayName="City" Version="1">
  39.     <Default>Johannesburg</Default>
  40.     <CHOICES>
  41.       <CHOICE>Cape Town</CHOICE>
  42.       <CHOICE>Durban</CHOICE>
  43.       <CHOICE>Johannesburg</CHOICE>
  44.       <CHOICE>Mangaung/Bloemfontein</CHOICE>
  45.       <CHOICE>Nelson Mandela Bay/Port Elizabeth</CHOICE>
  46.       <CHOICE>Nelspruit</CHOICE>
  47.       <CHOICE>Polokwane</CHOICE>
  48.       <CHOICE>Rustenburg</CHOICE>
  49.       <CHOICE>Tshwane/Pretoria</CHOICE>
  50.     </CHOICES>
  51.   </Field>
  52.   <Field Type="Choice" Description="List of players" Required="TRUE" Format="Dropdown" FillInChoice="FALSE" Group="World Cup 2010 Columns" ID="{EA4B814A-A18B-403a-A33A-8EA5436CB540}" StaticName="Position" Name="Position" DisplayName="Position" Version="1">
  53.     <CHOICES>
  54.       <CHOICE>Defensive Midfielder</CHOICE>
  55.       <CHOICE>Forward</CHOICE>
  56.       <CHOICE>Goalkeeper</CHOICE>
  57.       <CHOICE>Left Back</CHOICE>
  58.       <CHOICE>Left Midfielder</CHOICE>
  59.       <CHOICE>Right Back</CHOICE>
  60.       <CHOICE>Right Midfielder</CHOICE>
  61.       <CHOICE>Stopper</CHOICE>
  62.       <CHOICE>Striker</CHOICE>
  63.       <CHOICE>Sweeper</CHOICE>
  64.     </CHOICES>
  65.   </Field>
  66.   <Field Type="Text" Description="The name of the player" Required="TRUE" MaxLength="255" Group="World Cup 2010 Columns" ID="{04555083-EC04-4c20-A609-42A283428374}" StaticName="PlayerName" Name="PlayerName" DisplayName="Player Name"></Field>
  67.   <Field Type="Number" Description="The age of the player" Required="FALSE" Decimals="0" Group="World Cup 2010 Columns" ID="{2F2ABEB9-ED3D-41d9-BBDE-03D1150396A1}" StaticName="PlayerAge" Name="PlayerAge" DisplayName="Player Age"></Field>
  68.   <Field Type="DateTime" Description="The date of arrival" Required="FALSE" Format="DateOnly" Group="World Cup 2010 Columns" ID="{9910FA0D-C7E8-464a-9607-CABA1502B7DC}" StaticName="Arrival" Name="Arrival" DisplayName="Arrival" />
  69. </Elements>

 

In order to add the Site Columns above, the code snippet below must be used:

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.IO;
  7. using Microsoft.SharePoint;
  8.  
  9. namespace SiteColumns
  10. {
  11.     public class Program
  12.     {
  13.         public static void Main(string[] args)
  14.         {
  15.             using (SPSite site = new SPSite("http://localhost/"))
  16.             {
  17.                 XmlDocument xmlDoc = new XmlDocument();
  18.  
  19.                 xmlDoc.Load(Path.GetFullPath("SiteColumns.xml"));
  20.  
  21.                 foreach (XmlElement fieldNode in xmlDoc.DocumentElement.ChildNodes)
  22.                 {
  23.                     site.RootWeb.Fields.AddFieldAsXml(fieldNode.OuterXml);
  24.                 }
  25.             }
  26.         }
  27.     }
  28. }

 

In the end the Site Columns will be created according the Figure below:


Figure 1 – Creating Site Columns

Download the solution here.

Cheers

Marcel Medina

Click here to read the same content in Portuguese.

blog comments powered by Disqus