-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-StrictPropertyUpdaters.ps1
93 lines (77 loc) · 2.46 KB
/
Set-StrictPropertyUpdaters.ps1
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
function Set-ItemProperties(
[hashtable]$properties,
[object]$element
) {
$ignoreProperties = 'elements', 'name', 'fieldType', 'validations', 'settings', 'submitActions'
$element.PSObject.Properties | ForEach-Object {
if (!($_.Name -in $ignoreProperties)) {
$sitecorePropertyName = Get-SitecorePropertyName $_.Name
$propertyValue = $_.Value
$propertyType = $propertyValue.GetType().Name
switch ($propertyType) {
"Boolean" {
Set-BoolProperty $properties $sitecorePropertyName $propertyValue
break;
}
"Int32" { }
"String" {
Set-StringProperty $properties $sitecorePropertyName $propertyValue
}
default {
Write-Host "Property Type Not Supported: $propertyType"
}
}
}
}
}
function Get-SitecorePropertyName (
[string]$propertyName
) {
$sitecorePropertyName = $propertyName -creplace '.(?=[^a-z])', '$& '
return $sitecorePropertyName
}
function Set-StringProperty (
[hashtable]$properties,
[string]$itemPropertyName,
[string]$value
) {
# test string as a date
$result = Get-Date
$success = [datetime]::TryParseExact(
$value,
"mm/dd/yyyy",
[system.Globalization.DateTimeFormatInfo]::InvariantInfo,
[system.Globalization.DateTimeStyles]::None,
[ref]$result
)
if ($success) {
Set-DateProperty $properties $itemPropertyName $result
return
}
$properties.Add($itemPropertyName, $value)
}
function Set-BoolProperty (
[hashtable]$properties,
[string]$itemPropertyName,
[bool]$value
) {
$dataValue = [System.Convert]::ToBoolean($value)
$dataSitecoreValue = if ($dataValue) { "1" } else { "0" }
$properties.Add($itemPropertyName, $dataSitecoreValue)
}
function Set-DateProperty (
[hashtable]$properties,
[string]$itemPropertyName,
[DateTime]$date
) {
$resultString = $date.ToString("yyyymmdd")
$properties.Add($itemPropertyName, $resultString)
}
function Update-NewItem (
[Sitecore.Data.Items.Item]$item,
[hashtable]$properties
) {
$item.Editing.BeginEdit()
$properties.Keys | % { $item[$_] = $properties[$_] }
$item.Editing.EndEdit() | Out-Null
}