-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
209 lines (173 loc) · 6.24 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using EnvDTE;
using Microsoft.SharePoint.Client;
using Debugger = System.Diagnostics.Debugger;
namespace SPListRepository.Generator
{
public static class Helper
{
public static Dictionary<FieldType, string> FieldTypeMappings;
public static List<FieldType> SupportedFieldTypes;
static Helper()
{
FieldTypeMappings = new Dictionary<FieldType, string>
{
{FieldType.Boolean, "boolean"},
{FieldType.Choice, "string"},
{FieldType.ContentTypeId, "string"},
{FieldType.Counter, "number"},
{FieldType.DateTime, "Date"},
{FieldType.Integer, "number"},
{FieldType.Lookup, "SP.FieldLookupValue"},
{FieldType.Number, "number"},
{FieldType.Text, "string"},
{FieldType.URL, "SP.FieldUrlValue"},
{FieldType.User, "SP.FieldUserValue"},
{FieldType.MultiChoice, "SP.FieldMultiChoice"},
{FieldType.Note, "string"},
{FieldType.Computed, "string"}
};
SupportedFieldTypes = new List<FieldType>();
SupportedFieldTypes.AddRange(FieldTypeMappings.Keys);
}
public static string GetTypeStringByFieldType(Field field)
{
if (field.FieldTypeKind == FieldType.Lookup && ((FieldLookup)field).AllowMultipleValues)
{
return "SP.FieldLookupValue[]";
}
if (field.FieldTypeKind == FieldType.User && ((FieldUser)field).AllowMultipleValues)
{
return "SP.FieldUserValue[]";
}
if (FieldTypeMappings.Keys.Contains(field.FieldTypeKind))
{
return FieldTypeMappings[field.FieldTypeKind];
}
throw new Exception("Can't find mapping for field type " + field.TypeAsString);
}
public static bool IsFieldTypeSupported(FieldType type)
{
return SupportedFieldTypes.Any(f => f == type);
}
public static ClientContext CreateContext(GeneratorConfig config)
{
var ctx = new ClientContext(config.SiteUrl);
var isOnPrem = config.SiteUrl.IndexOf(".sharepoint.com") == -1;
if (isOnPrem && config.Creds != null)
{
ctx.Credentials = new NetworkCredential(config.Creds.UserName, config.Creds.Password);
}
else
{
var passWord = new SecureString();
foreach (char c in config.Creds.Password) passWord.AppendChar(c);
ctx.Credentials = new SharePointOnlineCredentials(config.Creds.UserName, passWord);
}
return ctx;
}
public static bool IsListAcceptable(List list, GeneratorConfig config)
{
if (config.Lists.All)
{
if (config.Lists.ExcludeHidden && list.Hidden)
{
return false;
}
if (config.Lists.Exclude.Count > 0 && config.Lists.Exclude.Any(url => list.RootFolder.ServerRelativeUrl.IndexOf(url, StringComparison.OrdinalIgnoreCase) != -1))
{
return false;
}
return true;
}
if (config.Lists.Include.Count > 0)
{
list.Context.Load(list.ParentWeb, w => w.ServerRelativeUrl);
list.Context.ExecuteQuery();
var siteRelativeUrl = list.ParentWeb.ServerRelativeUrl;
siteRelativeUrl = siteRelativeUrl.EndsWith("/") ? siteRelativeUrl : siteRelativeUrl + "/";
var listWebRelativeUrl = list.RootFolder.ServerRelativeUrl.Replace(siteRelativeUrl, string.Empty);
return config.Lists.Include.Any(url => listWebRelativeUrl.IndexOf(url, StringComparison.OrdinalIgnoreCase) != -1);
}
return false;
}
public static bool IsFieldAcceptableToMapToListItem(Field field, GeneratorConfig config)
{
var baseFieldsToExclude = new[] { "_CopySource", "_CheckinComment", "LinkFilenameNoMenu", "LinkFilename", "DocIcon", "FileSizeDisplay",
"ItemChildCount", "FolderChildCount", "AppAuthor", "AppEditor", "Edit", "_UIVersionString", "ParentVersionString", "ParentLeafName",
"LinkTitleNoMenu", "LinkTitle", "ContentType"};
if (baseFieldsToExclude.ToList().Any(f => f.Equals(field.InternalName)))
{
return false ;
}
return IsFieldAcceptable(field, config);
}
public static bool IsFieldAcceptable(Field field, GeneratorConfig config)
{
var baseFieldsToExclude = new[] { "ID", "Modified", "Created", "Editor", "Author", "FSObjType", "Title", "FileLeafRef", "FileDirRef", "ContentTypeId" };
if (!IsFieldTypeSupported(field.FieldTypeKind))
{
Debug.WriteLine("WARNING: Skipping field {0} because its field type '{1} not supported.'", field.InternalName, field.TypeAsString);
return false;
}
if (baseFieldsToExclude.ToList().Any(f => f.Equals(field.InternalName)))
{
return false;
}
if (config.Fields.All)
{
if (config.Fields.ExcludeHidden && field.Hidden)
{
return false;
}
if (config.Fields.Exclude.Count > 0 && config.Fields.Exclude.Any(f => f.Equals(field.InternalName)))
{
return false;
}
return true;
}
if (config.Fields.Include.Count > 0)
{
return config.Fields.Include.Any(f => f.Equals(field.InternalName));
}
return false;
}
public static void CheckSchemaConsistency(GeneratorConfig config)
{
if (config.Lists == null)
{
throw new Exception("lists attribute is required");
}
if (config.Lists.All && config.Lists.Include.Count > 0)
{
throw new Exception("Lists config: provide only one of the two properties - either 'all' or 'include'");
}
if (config.Lists.Include.Count > 0 && (config.Lists.Exclude.Count > 0 || config.Lists.ExcludeHidden))
{
throw new Exception("Lists config: properties 'exclude' and 'excludeHidden' are redundant when 'include' provided");
}
if (config.Fields == null)
{
throw new Exception("fields attribute is required");
}
if (config.Fields.All && config.Fields.Include.Count > 0)
{
throw new Exception("Fields config: provide only one of the two properties - either 'all' or 'include'");
}
if (config.Fields.Include.Count > 0 && (config.Fields.Exclude.Count > 0 || config.Fields.ExcludeHidden))
{
throw new Exception("Fields config: properties 'exclude' and 'excludeHidden' are redundant when 'include' provided");
}
}
public static string LowerCaseFirstLetter(string input)
{
return Char.ToLowerInvariant(input[0]) + input.Substring(1);
}
}
}