1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Net;
7 using Microsoft.SharePoint.Client;
8
9 namespace CreateSPSiteColumn
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 List<string[]> spSiteColumns = new List<string[]>();
16 //Site Column Internal Name, Display Name, Type, Choice list if any
17 spSiteColumns.Add(new string[] { "FiscalYear", "Fiscal Year", "Choice", "2012;2013;2014" });
18 spSiteColumns.Add(new string[] { "SubmittedDate", "Submitted Date", "DateTime", "" });
19 CreateSPSiteColumns(spSiteColumns);
20 }
21
22 static void CreateSPSiteColumns(List<string[]> listOfColumns)
23 {
24 using (ClientContext clientContext = new ClientContext(ConfigurationConstants.XYZSharepointSiteCollectionUrl))
25 {
26 NetworkCredential credentials = new NetworkCredential(ConfigurationConstants.XYZSharepointAdminUserName,
27 ConfigurationConstants.XYZSharepointAdminUserPassword, ConfigurationConstants.XYZSharepointDomain);
28 clientContext.Credentials = credentials;
29
30 Web web = clientContext.Web;
31 clientContext.Load(web, w => w.AvailableFields);
32 clientContext.ExecuteQuery();
33 FieldCollection collFields = web.AvailableFields;
34
35 foreach (string[] spColumn in listOfColumns)
36 {
37 bool spColumnAlreadyCreated = false;
38 foreach (Field spField in collFields)
39 {
40 if (spColumn[0].ToLower() == spField.InternalName.ToLower())
41 {
42 spColumnAlreadyCreated = true;
43 break;
44 }
45 }
46 if (!spColumnAlreadyCreated) // if sharepoint site column not alredy created, create now
47 {
48 string spColumnType = spColumn[2];
49 if (spColumnType == "Text" || spColumnType == "Currency" || spColumnType == "Boolean" || spColumnType == "Note" || spColumnType == "DateTime"
50 || spColumnType == "Number" || spColumnType == "Integer")
51 {
52 string columnGenericSchema = @"<Field Group='{3}' Name='{0}' DisplayName='{1}' Type='{2}' />";
53 string columnSchema = string.Format(columnGenericSchema, spColumn[0], spColumn[1], spColumnType, ConfigurationConstants.XYZSharepointSiteColumsGroupName);
54 var newField = web.Fields.AddFieldAsXml(columnSchema, false, AddFieldOptions.DefaultValue);
55 clientContext.ExecuteQuery();
56 //LogMessages.Instance.LogMessageToTextFile(" Site column " + spColumn[0] + " type " + spColumn[2] + " succesfully created ");
57 }
58 else if (spColumnType == "Choice")
59 {
60 string choiceFieldListOfValues = spColumn[3];
61 string[] optionSetArray = choiceFieldListOfValues.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
62
63 StringBuilder choiceFieldGenericShema = new StringBuilder();
64 choiceFieldGenericShema.Append("<Field Type='Choice' Format='Dropdown' Group='{2}' Name='{0}' DisplayName='{1}' > <CHOICES> ");
65 foreach (string optionValue in optionSetArray)
66 {
67 if (!string.IsNullOrEmpty(optionValue))
68 {
69 choiceFieldGenericShema.Append("<CHOICE>" + optionValue.Trim() + "</CHOICE> ");
70 }
71 }
72 choiceFieldGenericShema.Append("</CHOICES> </Field> ");
73 string choicFieldSchema = string.Format(choiceFieldGenericShema.ToString(), spColumn[0], spColumn[1], ConfigurationConstants.XYZSharepointSiteColumsGroupName);
74 var newField = web.Fields.AddFieldAsXml(choicFieldSchema, false, AddFieldOptions.DefaultValue);
75 clientContext.ExecuteQuery();
76
77 }
78
79 }
80 else
81 {
82 //LogMessages.Instance.LogMessageToTextFile(" Site column " + spColumn[0] + " was already created ");
83 }
84 }
85 }
86 }
87 public class ConfigurationConstants
88 {
89 public static string XYZLogFileLocation = @"C:\\LogFile.txt";
90 public static string XYZSharepointSiteCollectionUrl = "http://sharepointservername:100/";
91 public static string XYZSharepointDomain = "domainName";
92 public static string XYZSharepointAdminUserName = "UserName";
93 public static string XYZSharepointAdminUserPassword = "Password";
94 public static string XYZSharepointSiteColumsGroupName = "XYZ Site Columns";
95 }
96 }
97 }
98
No comments:
Post a Comment