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 using Microsoft.SharePoint.Client.Taxonomy;
9
10 namespace CreateSPManagedMetadataField
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 List<string[]> spSiteColumns = new List<string[]>();
17 //Site Column Internal Name, Display Name, Type, Term Set Name
18 spSiteColumns.Add(new string[] { "JobTitle", "Job Title", "TaxonomyFieldType", "TermSet1" });
19 CreateSPTaxFields(spSiteColumns);
20 }
21
22 static void CreateSPTaxFields(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 == "TaxonomyFieldType")
50 {
51 string columnTaxonomyGenericSchema = @"<Field Type='TaxonomyFieldType' Name='{0}' DisplayName='{1}' ShowField='Term1033' />";
52 string columnTaxonomySchema = string.Format(columnTaxonomyGenericSchema, spColumn[0], spColumn[1]);
53
54 var session = TaxonomySession.GetTaxonomySession(clientContext);
55 var store = session.TermStores.GetByName(ConfigurationConstants.XYZTermStoreName);
56 var group = store.Groups.GetByName(ConfigurationConstants.XYZTermGroupName);
57 var set = group.TermSets.GetByName(spColumn[3]);
58
59 clientContext.Load(store, s => s.Id);
60 clientContext.Load(set, s => s.Id);
61 clientContext.ExecuteQuery();
62
63
64 var taxField = web.Fields.AddFieldAsXml(columnTaxonomySchema, false, AddFieldOptions.DefaultValue);
65 clientContext.Load(taxField);
66 clientContext.ExecuteQuery();
67
68 var taxfield2 = clientContext.CastTo<TaxonomyField>(taxField);
69 taxfield2.SspId = store.Id;
70 taxfield2.TermSetId = set.Id;
71 taxfield2.Update();
72 clientContext.ExecuteQuery();
73 //LogMessages.Instance.LogMessageToTextFile(" Site column " + spColumn[0] + " type " + spColumn[2] + " succesfully created ");
74 }
75
76 }
77 else
78 {
79 //LogMessages.Instance.LogMessageToTextFile(" Site column " + spColumn[0] + " was already created ");
80 }
81 }
83 }
84 public class ConfigurationConstants
85 {
86 public static string XYZLogFileLocation = @"C:\\LogFile.txt";
87 public static string XYZSharepointSiteCollectionUrl = "http://sharepointservername:100/";
88 public static string XYZSharepointDomain = "domainName";
89 public static string XYZSharepointAdminUserName = "UserName";
90 public static string XYZSharepointAdminUserPassword = "Password";
91 public static string XYZSharepointSiteColumsGroupName = "XYZ Site Columns";
92 public static string XYZTermStoreName = "Managed Metadata Service";
93 public static string XYZTermGroupName = "HR";
94
95 }
96 }
97 }
98