-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathListItemHelper.cs
More file actions
664 lines (630 loc) · 35.3 KB
/
ListItemHelper.cs
File metadata and controls
664 lines (630 loc) · 35.3 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using PnP.Core.QueryModel;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
namespace PnP.PowerShell.Commands.Utilities
{
public static class ListItemHelper
{
private class FieldUpdateValue
{
public string Key { get; set; }
public object Value { get; set; }
public string FieldTypeString { get; set; }
public FieldUpdateValue(string key, object value)
{
Key = key;
Value = value;
}
public FieldUpdateValue(string key, object value, string fieldTypeString)
{
Key = key;
Value = value;
FieldTypeString = fieldTypeString;
}
}
public static void SetFieldValues(this ListItem item, Hashtable valuesToSet, BasePSCmdlet cmdlet)
{
var itemValues = new List<FieldUpdateValue>();
var context = item.Context as ClientContext;
var list = item.ParentList;
context.Web.EnsureProperty(w => w.Url);
var clonedContext = context.Clone(context.Web.Url);
var web = clonedContext.Web;
var fields =
context.LoadQuery(list.Fields.Include(f => f.InternalName, f => f.Title,
f => f.TypeAsString));
context.ExecuteQueryRetry();
Hashtable values = valuesToSet ?? new Hashtable();
foreach (var key in values.Keys)
{
var field = fields.FirstOrDefault(f => f.InternalName == key as string || f.Title == key as string);
if (field != null)
{
switch (field.TypeAsString)
{
case "User":
case "UserMulti":
{
List<FieldUserValue> userValues = new List<FieldUserValue>();
var value = values[key];
if (value == null) goto default;
if (value is string && string.IsNullOrWhiteSpace(value + "")) goto default;
if (value.GetType().IsArray)
{
foreach (var arrayItem in value as IEnumerable)
{
int userId;
if (!int.TryParse(arrayItem.ToString(), out userId))
{
var user = web.EnsureUser(arrayItem as string);
clonedContext.Load(user);
clonedContext.ExecuteQueryRetry();
userValues.Add(new FieldUserValue() { LookupId = user.Id });
}
else
{
userValues.Add(new FieldUserValue() { LookupId = userId });
}
}
itemValues.Add(new FieldUpdateValue(key as string, userValues.ToArray(), null));
}
else
{
int userId;
if (!int.TryParse(value as string, out userId))
{
var user = web.EnsureUser(value as string);
clonedContext.Load(user);
clonedContext.ExecuteQueryRetry();
itemValues.Add(new FieldUpdateValue(key as string, new FieldUserValue() { LookupId = user.Id }));
}
else
{
itemValues.Add(new FieldUpdateValue(key as string, new FieldUserValue() { LookupId = userId }));
}
}
break;
}
case "TaxonomyFieldType":
case "TaxonomyFieldTypeMulti":
{
var value = values[key];
if (value != null && value.GetType().IsArray)
{
var taxSession = clonedContext.Site.GetTaxonomySession();
var terms = new List<KeyValuePair<Guid, string>>();
foreach (var arrayItem in value as object[])
{
TaxonomyItem taxonomyItem;
Guid termGuid;
if (!Guid.TryParse(arrayItem?.ToString(), out termGuid))
{
// Assume it's a TermPath
taxonomyItem = clonedContext.Site.GetTaxonomyItemByPath(arrayItem?.ToString());
}
else
{
taxonomyItem = taxSession.GetTerm(termGuid);
clonedContext.Load(taxonomyItem);
clonedContext.ExecuteQueryRetry();
}
if (taxonomyItem != null)
{
terms.Add(new KeyValuePair<Guid, string>(taxonomyItem.Id, taxonomyItem.Name));
}
else
{
cmdlet.LogWarning("Unable to find the specified term. Skipping values for field '" + field.InternalName + "'.");
}
}
TaxonomyField taxField = context.CastTo<TaxonomyField>(field);
taxField.EnsureProperty(tf => tf.AllowMultipleValues);
if (taxField.AllowMultipleValues)
{
var termValuesString = String.Empty;
foreach (var term in terms)
{
termValuesString += "-1;#" + term.Value + "|" + term.Key.ToString("D") + ";#";
}
if (!string.IsNullOrEmpty(termValuesString))
{
termValuesString = termValuesString.Substring(0, termValuesString.Length - 2);
var newTaxFieldValue = new TaxonomyFieldValueCollection(context, termValuesString, taxField);
itemValues.Add(new FieldUpdateValue(key as string, newTaxFieldValue, field.TypeAsString));
}
}
else
{
cmdlet.LogWarning("You are trying to set multiple values in a single value field. Skipping values for field '" + field.InternalName + "'.");
}
}
else
{
Guid termGuid = Guid.Empty;
var taxSession = clonedContext.Site.GetTaxonomySession();
TaxonomyItem taxonomyItem = null;
bool updateTaxItemValue = true;
if (value != null && !Guid.TryParse(value?.ToString(), out termGuid))
{
// Assume it's a TermPath
taxonomyItem = clonedContext.Site.GetTaxonomyItemByPath(value as string);
if (taxonomyItem == null)
{
updateTaxItemValue = false;
cmdlet.LogWarning("Unable to find the specified term. Skipping values for field '" + field.InternalName + "'.");
}
}
else
{
if (value != null)
{
taxonomyItem = taxSession.GetTerm(termGuid);
clonedContext.Load(taxonomyItem);
clonedContext.ExecuteQueryRetry();
}
}
TaxonomyField taxField = context.CastTo<TaxonomyField>(field);
TaxonomyFieldValue taxValue = new TaxonomyFieldValue();
if (taxonomyItem != null)
{
taxValue.TermGuid = taxonomyItem.Id.ToString();
taxValue.Label = taxonomyItem.Name;
itemValues.Add(new FieldUpdateValue(key as string, taxValue, field.TypeAsString));
}
else
{
if (updateTaxItemValue)
{
taxField.ValidateSetValue(item, null);
}
}
}
break;
}
case "Lookup":
case "LookupMulti":
{
var value = values[key];
if (value == null) goto default;
int[] multiValue;
if (value is Array)
{
var arr = (object[])values[key];
multiValue = new int[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
multiValue[i] = int.Parse(arr[i].ToString());
}
}
else
{
string valStr = values[key].ToString();
multiValue = valStr.Split(',', ';').Select(int.Parse).ToArray();
}
var newVals = multiValue.Select(id => new FieldLookupValue { LookupId = id }).ToArray();
FieldLookup lookupField = context.CastTo<FieldLookup>(field);
lookupField.EnsureProperty(lf => lf.AllowMultipleValues);
if (!lookupField.AllowMultipleValues && newVals.Length > 1)
{
throw new Exception("Field " + field.InternalName + " does not support multiple values");
}
itemValues.Add(new FieldUpdateValue(key as string, newVals));
break;
}
default:
{
itemValues.Add(new FieldUpdateValue(key as string, values[key]));
break;
}
}
}
else
{
throw new PSInvalidOperationException($"Field {key} not present in list.");
}
}
if (item != null && !item.ServerObjectIsNull.Value)
{
var specialFields = new[] { "Author", "Editor", "Created", "Modified" };
// check if we are setting editor or author fields
if (itemValues.Any(i => specialFields.Contains(i.Key)))
{
foreach (var field in specialFields)
{
if (itemValues.FirstOrDefault(i => i.Key == field) == null)
{
if (item.FieldValues.TryGetValue(field, out object fieldValue))
{
itemValues.Add(new FieldUpdateValue(field, fieldValue));
}
}
}
}
}
foreach (var itemValue in itemValues)
{
if (string.IsNullOrEmpty(itemValue.FieldTypeString))
{
item[itemValue.Key] = itemValue.Value;
}
else
{
switch (itemValue.FieldTypeString)
{
case "TaxonomyFieldTypeMulti":
{
var field = fields.FirstOrDefault(f => f.InternalName == itemValue.Key as string || f.Title == itemValue.Key as string);
var taxField = context.CastTo<TaxonomyField>(field);
if (itemValue.Value is TaxonomyFieldValueCollection)
{
taxField.SetFieldValueByValueCollection(item, itemValue.Value as TaxonomyFieldValueCollection);
}
else
{
taxField.SetFieldValueByValue(item, itemValue.Value as TaxonomyFieldValue);
}
break;
}
case "TaxonomyFieldType":
{
var field = fields.FirstOrDefault(f => f.InternalName == itemValue.Key as string || f.Title == itemValue.Key as string);
var taxField = context.CastTo<TaxonomyField>(field);
taxField.SetFieldValueByValue(item, itemValue.Value as TaxonomyFieldValue);
break;
}
}
}
}
}
public static Dictionary<string, object> GetFieldValues(PnP.Core.Model.SharePoint.IList list, PnP.Core.Model.SharePoint.IListItem existingItem, Hashtable valuesToSet, ClientContext clientContext, PnPBatch batch)
{
TermStore store = null;
TaxonomySession taxSession = null;
int defaultLanguage = CultureInfo.CurrentCulture.LCID;
var item = new Dictionary<string, object>();
// xxx: return early if hashtable is empty to save getting fields?
var fields = list.Fields;
Hashtable values = valuesToSet ?? new Hashtable();
foreach (var key in values.Keys)
{
var field = fields.AsRequested().FirstOrDefault(f => f.InternalName == key as string || f.Title == key as string);
if (field != null)
{
switch (field.TypeAsString)
{
case "User":
case "UserMulti":
{
var userValueCollection = field.NewFieldValueCollection();
var value = values[key];
if (value == null) goto default;
if (value is string && string.IsNullOrWhiteSpace(value + "")) goto default;
if (value.GetType().IsArray)
{
foreach (var arrayItem in (value as IEnumerable))
{
int userId;
if (!int.TryParse(arrayItem.ToString(), out userId))
{
var user = list.PnPContext.Web.EnsureUser(arrayItem as string);
userValueCollection.Values.Add(field.NewFieldUserValue(user));
}
else
{
try
{
var fieldUserValue = list.PnPContext.Web.GetUserById(userId);
userValueCollection.Values.Add(field.NewFieldUserValue(fieldUserValue));
}
catch
{
// It is SharePoint Group
list.PnPContext.Web.LoadAsync(p => p.SiteGroups).GetAwaiter().GetResult();
var groupItem = list.PnPContext.Web.SiteGroups.AsRequested().Where(g => g.Id == userId).FirstOrDefault();
if (groupItem != null)
{
userValueCollection.Values.Add(field.NewFieldUserValue(groupItem));
}
}
}
}
item[key as string] = userValueCollection;
}
else
{
int userId;
if (!int.TryParse(value as string, out userId))
{
var user = list.PnPContext.Web.EnsureUser(value as string);
item[key as string] = field.NewFieldUserValue(user);
}
else
{
try
{
var fieldUserValue = list.PnPContext.Web.GetUserById(userId);
item[key as string] = field.NewFieldUserValue(fieldUserValue);
}
catch
{
// It is SharePoint Group
list.PnPContext.Web.LoadAsync(p => p.SiteGroups).GetAwaiter().GetResult();
var groupItem = list.PnPContext.Web.SiteGroups.AsRequested().Where(g => g.Id == userId).FirstOrDefault();
if (groupItem != null)
{
item[key as string] = field.NewFieldUserValue(groupItem);
}
}
}
}
break;
}
case "TaxonomyFieldType":
case "TaxonomyFieldTypeMulti":
{
var value = values[key];
if (batch.TermStore == null)
{
taxSession = clientContext.Site.GetTaxonomySession();
store = taxSession.GetDefaultSiteCollectionTermStore();
clientContext.Load(store, s => s.DefaultLanguage);
clientContext.ExecuteQueryRetry();
defaultLanguage = store.DefaultLanguage;
batch.TermStore = store;
batch.TaxonomySession = taxSession;
batch.DefaultTermStoreLanguage = defaultLanguage;
}
else
{
taxSession = batch.TaxonomySession;
store = batch.TermStore;
defaultLanguage = batch.DefaultTermStoreLanguage.Value;
}
if (value != null && value.GetType().IsArray)
{
var fieldValueCollection = field.NewFieldValueCollection();
foreach (var arrayItem in value as object[])
{
Term taxonomyItem;
Guid termGuid;
var label = string.Empty;
var itemId = Guid.Empty;
if (!Guid.TryParse(arrayItem?.ToString(), out termGuid))
{
var batchedTerm = batch.GetCachedTerm(arrayItem?.ToString());
if (batchedTerm.key == null)
{
taxonomyItem = clientContext.Site.GetTaxonomyItemByPath(arrayItem?.ToString()) as Term;
if (taxonomyItem == null)
{
throw new PSInvalidOperationException($"Cannot find term '{arrayItem}'");
}
var labelResult = taxonomyItem.GetDefaultLabel(defaultLanguage);
clientContext.ExecuteQueryRetry();
label = labelResult.Value;
itemId = taxonomyItem.Id;
batch.CacheTerm(arrayItem?.ToString(), itemId, label);
batch.CacheTerm(itemId.ToString(), itemId, label);
}
else
{
itemId = batchedTerm.id;
label = batchedTerm.label;
}
}
else
{
var batchedTerm = batch.GetCachedTerm(termGuid.ToString());
if (batchedTerm.key == null)
{
taxonomyItem = taxSession.GetTerm(termGuid);
if (taxonomyItem == null)
{
throw new PSInvalidOperationException($"Cannot find term {arrayItem}");
}
var labelResult = taxonomyItem.GetDefaultLabel(defaultLanguage);
clientContext.Load(taxonomyItem);
clientContext.ExecuteQueryRetry();
itemId = taxonomyItem.Id;
label = labelResult.Value;
batch.CacheTerm(termGuid.ToString(), termGuid, label);
}
else
{
itemId = batchedTerm.id;
label = batchedTerm.label;
}
}
fieldValueCollection.Values.Add(field.NewFieldTaxonomyValue(itemId, label));
}
item[key as string] = fieldValueCollection;
}
else
{
Guid termGuid = Guid.Empty;
Term taxonomyItem = null;
var label = string.Empty;
var itemId = Guid.Empty;
if (value != null && !Guid.TryParse(value as string, out termGuid))
{
var batchedTerm = batch.GetCachedTerm(termGuid.ToString());
if (batchedTerm.key == null)
{
// Assume it's a TermPath
taxonomyItem = clientContext.Site.GetTaxonomyItemByPath(value as string) as Term;
var labelResult = taxonomyItem.GetDefaultLabel(defaultLanguage);
clientContext.ExecuteQueryRetry();
itemId = taxonomyItem.Id;
label = labelResult.Value;
}
else
{
itemId = batchedTerm.id;
label = batchedTerm.label;
}
}
else
{
if (value != null)
{
var batchedTerm = batch.GetCachedTerm(termGuid.ToString());
if (batchedTerm.key == null)
{
taxonomyItem = taxSession.GetTerm(termGuid);
var labelResult = taxonomyItem.GetDefaultLabel(defaultLanguage);
clientContext.Load(taxonomyItem);
clientContext.ExecuteQueryRetry();
label = labelResult.Value;
}
else
{
itemId = batchedTerm.id;
label = batchedTerm.label;
}
}
}
item[key as string] = field.NewFieldTaxonomyValue(taxonomyItem.Id, label);
}
break;
}
case "Lookup":
case "LookupMulti":
{
var value = values[key];
if (value == null) goto default;
int[] multiValue;
if (value is Array)
{
var fieldValueCollection = field.NewFieldValueCollection();
var arr = (object[])values[key];
for (int i = 0; i < arr.Length; i++)
{
var arrayValue = arr[i].ToString();
fieldValueCollection.Values.Add(field.NewFieldLookupValue(int.Parse(arrayValue)));
}
item[key as string] = fieldValueCollection;
}
else
{
var fieldValueCollection = field.NewFieldValueCollection();
string valStr = values[key].ToString();
multiValue = valStr.Split(',', ';').Select(int.Parse).ToArray();
if (multiValue.Length > 1)
{
for (int i = 0; i < multiValue.Length; i++)
{
fieldValueCollection.Values.Add(field.NewFieldLookupValue(multiValue[i]));
}
item[key as string] = fieldValueCollection;
}
else
{
item[key as string] = field.NewFieldLookupValue(multiValue[0]);
}
}
break;
}
case "MultiChoice":
{
string itemValue = string.Empty;
var choices = values[key];
if (choices is string)
{
// Handle comma or semicolon separated string
itemValue = string.Join(";#", ((string)choices).Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(c => c.Trim()));
}
else if (choices is Array)
{
// Handle array of values (string[], object[], etc.)
foreach (var choice in (Array)choices)
{
itemValue += choice?.ToString() + ";#";
}
if (!string.IsNullOrEmpty(itemValue))
{
itemValue = itemValue.Substring(0, itemValue.Length - 2);
}
}
else if (choices is IEnumerable)
{
// Handle other enumerable types
foreach (var choice in (IEnumerable)choices)
{
itemValue += choice?.ToString() + ";#";
}
if (!string.IsNullOrEmpty(itemValue))
{
itemValue = itemValue.Substring(0, itemValue.Length - 2);
}
}
else
{
// Handle a single value
itemValue = choices?.ToString();
}
item[key as string] = itemValue;
break;
}
default:
{
object itemValue = values[key] is PSObject ? ((PSObject)values[key]).BaseObject : values[key];
item[key as string] = itemValue;
break;
}
}
}
else
{
throw new PSInvalidOperationException($"Field {key} not present in list.");
}
}
if (existingItem != null && existingItem.Requested)
{
var specialFields = new[] { "Author", "Editor", "Created", "Modified" };
// check if we are setting editor or author fields
if (item.Any(i => specialFields.Contains(i.Key)))
{
foreach (var field in specialFields)
{
if (!item.ContainsKey(field) && existingItem.Values.ContainsKey(field))
{
item[field] = existingItem[field];
}
}
}
}
return item;
}
public static void UpdateListItem(this ListItem item, ListItemUpdateType updateType)
{
switch (updateType)
{
default:
case ListItemUpdateType.Update:
{
item.Update();
break;
}
case ListItemUpdateType.SystemUpdate:
{
item.SystemUpdate();
break;
}
case ListItemUpdateType.UpdateOverwriteVersion:
{
item.UpdateOverwriteVersion();
break;
}
}
}
}
}