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.

SharePoint 2010 Certified

After taking the 71-573, got a new SharePoint 2010 Certification. Learn how to get it too.

Hi folks,

Today I received the outcome of the SharePoint Development BETA exams (71-573 / 71-576) I took last month and for my surprise I succeed them. The tests were the Microsoft SharePoint 2010, Application Development and the Designing and Developing Microsoft SharePoint 2010 Applications. Both of them were tough tests with more than 80 questions, comprising the following technical tasks below:

(71-573) TS: Microsoft SharePoint 2010, Application Development

  • Working with the SharePoint User Interface
  • Developing Web Parts and Controls
  • Developing Business Logic
  • Working With SharePoint Data
  • Stabilizing and Deploying SharePoint Components

(71-576) PRO: Designing and Developing Microsoft SharePoint 2010 Applications

  • Creating an Application Design
  • Designing UX
  • Managing Application Development
  • Optimizing SharePoint Application Design
  • Designing SharePoint Composite Applications
  • Designing SharePoint Solutions and Features

From now on these tests are going to have fewer questions, because they have evoluted to a regular exam. The exam numbers have changed to 70-573 and 70-576 as well. If you want to know more about them, get more details in the following links:
http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-573
http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-576


Now I am a SharePoint 2010 Certified! Awesome!





Cheers,

Marcel Medina

Click here to read the same content in Portuguese.

Site Definitions – A legacy

Site Definition and its legacy. Experiences with the MOSS 2007 Upgrade to SharePoint 2010.

Hi folks,

In this post I want to talk a little about Site Definitions and the legacy you get when using them.

Site Definitions are present in all SharePoint installations, considering the last recent versions:

  • WSS 3.0 and MOSS 2007 – available at the directory %\12\TEMPLATE\SiteTemplates

  • SharePoint Foundation and Server 2010 - available at the directory %\14\TEMPLATE\SiteTemplates

So using these templates for customisation and extension is really normal, right?

That's not quite right…its utilisation at first seems to be the best solution, because it allows us to create definitions of the objects that will compose the Web Site. Besides we can make it available in Tabs at the moment of creating a Web Site, which makes easy for users to select the correct Template.

The main issue is the legacy that this solution gives you. Currently I am working with the Upgrade of SharePoint 2010 Solutions developed for MOSS 2007 and have seen how complex is to upgrade Heavy Site Definitions. First you laugh, then you cry! :)

As a developer I always chase a way to create reusable code, and the utilisation of Site Definitions allows us that.  You must know that is more productive the development based on something that already exists rather than creating something from the scratch.

Nevertheless it is important to understand this legacy and its futures consequences. This concern applies to all solutions in any technology! I agree that in most of the cases the SharePoint development comes from experience, because in many cases documentation is scarce.

Best practices are important, in this specific case I give you some tips:

  • Use Light Site Definitions, that contain few objects (just the essential ones).

  • After creating a Site Definition, use Feature Stapling to add other objects.

  • If your solution works without using a Custom Site Definition, probably you don’t need it.

Unlike those that come to abolish such use [nothing against the IT guys :)], I am for it because I have the following thinking: if the technology allows us to use this resource, so it means we can use it. Just be responsible and pay attention to its purpose and the right utilisation.

Be welcome to comment if you wish!

Please find below some links related to this post:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;898631
http://msdn.microsoft.com/en-us/library/aa978512.aspx
http://msdn.microsoft.com/en-us/library/aa979683.aspx
http://msdn.microsoft.com/en-us/library/ms454677.aspx
http://spg.codeplex.com

Cheers,

Marcel Medina

Click here to read the same content in Portuguese.