Oi pessoal,
De agora em diante também postarei algumas notas rápidas com dicas de desenvolvimento. Acredito que serão bastante úteis no dia-a-dia de cada um.
Nesse post demonstro como criar Site Columns programaticamente via XML.
Considere o seguinte XML de Site Columns:
Code Snippet
- <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
- <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">
- <CHOICES>
- <CHOICE>Algeria</CHOICE>
- <CHOICE>Argentina</CHOICE>
- <CHOICE>Australia</CHOICE>
- <CHOICE>Brazil</CHOICE>
- <CHOICE>Cameroon</CHOICE>
- <CHOICE>Chile</CHOICE>
- <CHOICE>Côte d'Ivoire</CHOICE>
- <CHOICE>Denmark</CHOICE>
- <CHOICE>England</CHOICE>
- <CHOICE>France</CHOICE>
- <CHOICE>Germany</CHOICE>
- <CHOICE>Ghana</CHOICE>
- <CHOICE>Greece</CHOICE>
- <CHOICE>Honduras</CHOICE>
- <CHOICE>Italy</CHOICE>
- <CHOICE>Japan</CHOICE>
- <CHOICE>Korea DPR</CHOICE>
- <CHOICE>Korea Republic</CHOICE>
- <CHOICE>Mexico</CHOICE>
- <CHOICE>Netherlands</CHOICE>
- <CHOICE>New Zealand</CHOICE>
- <CHOICE>Nigeria</CHOICE>
- <CHOICE>Paraguay</CHOICE>
- <CHOICE>Portugal</CHOICE>
- <CHOICE>Serbia</CHOICE>
- <CHOICE>Slovakia</CHOICE>
- <CHOICE>Slovenia</CHOICE>
- <CHOICE>South Africa</CHOICE>
- <CHOICE>Spain</CHOICE>
- <CHOICE>Switzerland</CHOICE>
- <CHOICE>Uruguay</CHOICE>
- <CHOICE>USA</CHOICE>
- </CHOICES>
- </Field>
- <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">
- <Default>Johannesburg</Default>
- <CHOICES>
- <CHOICE>Cape Town</CHOICE>
- <CHOICE>Durban</CHOICE>
- <CHOICE>Johannesburg</CHOICE>
- <CHOICE>Mangaung/Bloemfontein</CHOICE>
- <CHOICE>Nelson Mandela Bay/Port Elizabeth</CHOICE>
- <CHOICE>Nelspruit</CHOICE>
- <CHOICE>Polokwane</CHOICE>
- <CHOICE>Rustenburg</CHOICE>
- <CHOICE>Tshwane/Pretoria</CHOICE>
- </CHOICES>
- </Field>
- <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">
- <CHOICES>
- <CHOICE>Defensive Midfielder</CHOICE>
- <CHOICE>Forward</CHOICE>
- <CHOICE>Goalkeeper</CHOICE>
- <CHOICE>Left Back</CHOICE>
- <CHOICE>Left Midfielder</CHOICE>
- <CHOICE>Right Back</CHOICE>
- <CHOICE>Right Midfielder</CHOICE>
- <CHOICE>Stopper</CHOICE>
- <CHOICE>Striker</CHOICE>
- <CHOICE>Sweeper</CHOICE>
- </CHOICES>
- </Field>
- <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>
- <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>
- <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" />
- </Elements>
Para adicionar os Site Columns acima podemos utilizar o seguinte trecho de código:
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.IO;
- using Microsoft.SharePoint;
-
- namespace SiteColumns
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- using (SPSite site = new SPSite("http://localhost/"))
- {
- XmlDocument xmlDoc = new XmlDocument();
-
- xmlDoc.Load(Path.GetFullPath("SiteColumns.xml"));
-
- foreach (XmlElement fieldNode in xmlDoc.DocumentElement.ChildNodes)
- {
- site.RootWeb.Fields.AddFieldAsXml(fieldNode.OuterXml);
- }
- }
- }
- }
- }
No final os Site Columns serão criados conforme a Figura abaixo:
Figura 1 – Criação dos Site Columns
Baixe a solução aqui.
[]’s
Marcel Medina
Clique aqui para ler o mesmo conteúdo em Inglês.