# UpdateHelpers.ps1













function AddChoiceFieldItems($fields, $choices, $fillInChoice)






{






	foreach ($field in $fields)






	{






		if ($field -eq $null)






		{






			continue






		}






		






		if ($field.Scope.Tolower() -notlike "*lists*" -and $field.Scope -ne $updateSite.ServerRelativeUrl) 






		{






			continue






		}













		$pos1 = $field.SchemaXml.IndexOf("<CHOICES>")






		$pos2 = $field.SchemaXml.IndexOf("</CHOICES>")






		$originalChoices = $field.SchemaXml.Substring($pos1, ($pos2 + 10) - $pos1)






		$newChoices = ""






		foreach ($choice in $choices)






		{






			$newChoices += "<CHOICE>$choice</CHOICE>"






		}













		$newChoices = "<CHOICES>$newChoices</CHOICES>"






		$field.SchemaXml = $field.SchemaXml.Replace($originalChoices, $newChoices)













		if ($fillInChoice -eq $true)






		{






			$pos = $field.SchemaXml.IndexOf("FillInChoice")






			if ($pos -eq -1) 






			{






				$field.SchemaXml = $field.SchemaXml.Replace("AllowDeletion", 'FillInChoice="TRUE" AllowDeletion')






			}






			else 






			{






				$pos = $field.SchemaXml.IndexOf('FillInChoice="FALSE"')






				if ($pos -ne -1) 






				{






					$field.SchemaXml = $field.SchemaXml.Replace('FillInChoice="FALSE"', 'FillInChoice="TRUE"')






				}






			}






		}













		$field.Update()






		Invoke-PnPQuery






	}






}













function EnforceRelationshipBehavior($fields, $behaviour)






{






	$otherBehaviour = "Restrict"






	if ($behaviour -eq $null) 






	{






		$behaviour = "Cascade"






	}






	elseif ($behaviour.ToLower() -eq "restrict") 






	{






		$otherBehaviour = "Cascade"






	}













	foreach ($fieldWithTitle in $fields)






	{






		if ($fieldWithTitle -eq $null)






		{






			continue






		}






		






		$field = $fieldWithTitle[0]






		if ($field -eq $null)






		{






			continue






		}













		$retryCount = 0






		while ($retryCount -lt 2)






		{






			try






			{






				$pos = $field.SchemaXml.IndexOf("Indexed")






				if ($pos -eq -1) 






				{






					$field.SchemaXml = $field.SchemaXml.Replace("AllowDeletion", 'Indexed="TRUE" AllowDeletion')






				}






				else 






				{






					$pos = $field.SchemaXml.IndexOf('Indexed="FALSE"')






					if ($pos -ne -1) 






					{






						$field.SchemaXml = $field.SchemaXml.Replace('Indexed="FALSE"', 'Indexed="TRUE"')






					}






				}













				$pos = $field.SchemaXml.IndexOf("RelationshipDeleteBehavior")






				if ($pos -eq -1) 






				{






					$field.SchemaXml = $field.SchemaXml.Replace("AllowDeletion", "RelationshipDeleteBehavior=""$behaviour"" AllowDeletion")






				}






				else 






				{






					$pos = $field.SchemaXml.IndexOf("RelationshipDeleteBehavior=""$otherBehaviour""")






					if ($pos -ne -1) 






					{






						$field.SchemaXml = $field.SchemaXml.Replace("RelationshipDeleteBehavior=""$otherBehaviour""", "RelationshipDeleteBehavior=""$behaviour""")






					}






					else 






					{






						$field.SchemaXml = $field.SchemaXml.Replace('RelationshipDeleteBehavior="None"', "RelationshipDeleteBehavior=""$behaviour""")






					}






				}













				$field.Update()






				Invoke-PnPQuery






				break






			}






			catch






			{






				$listTitle = $fieldWithTitle[1]






				$retryCount++






				if ($retryCount -eq 1)






				{






					ClearItemsWithUnlinkedLookupField $listTitle $field.InternalName $behaviour






				}






				else 






				{






					$fieldTitle = $field.Title






					Error "$listTitle - $fieldTitle"






					throw






				}






			}






		}






	}






}













function ClearItemsWithUnlinkedLookupField($listTitle, $fieldName, $behaviour)






{






	$listItems = Get-PnPListItem -List $listTitle






	if ($listItems.Count -gt 0) 






	{






		foreach ($listItem in $listItems)






		{






			$lookupId = $listItem[$fieldName].LookupId






			$lookupValue = $listItem[$fieldName].LookupValue






			if (![string]::IsNullOrEmpty($lookupId) -and [string]::IsNullOrEmpty($lookupValue))






			{






				if ($behaviour.ToLower() -eq "cascade" -or $listTitle -eq "SkillsPerAgent")






				{






					Remove-PnPListItem -List $listTitle -Identity $listItem -Force






				}






				else






				{






					SetPnpListItem $listTitle $listItem @{$fieldName = $null}






				}






			}






		}






	}






}













function UpdateSettingValues






{






	$listTitle = "SettingValues"






	$listItems = Get-PnPListItem -List $listTitle






	if ($listItems.Count -gt 0) 






	{






		foreach ($deleteItem in $listItems)






		{






			Remove-PnPListItem -List $listTitle -Identity $deleteItem.Id -Force






		}






	}













	$language = $languageInfo[2]






	$importFile = "$RootPath\AnywhereNow\CSV\$language\SettingValues.csv"






	$data = Import-Csv $importFile -Delimiter ";"






	foreach ($row in $data) 






	{






		AddPnPListItem $listTitle @{"Title" = $row.Title; "wsp_ucc_sv_type" = $row.wsp_ucc_sv_type; "wsp_ucc_sv_value" = $row.wsp_ucc_sv_value; 






									"wsp_ucc_sv_default" = $row.wsp_ucc_sv_default; "wsp_ucc_sv_description" = $row.wsp_ucc_sv_description; "wsp_ucc_sv_scope" = $row.wsp_ucc_sv_scope; 






									"wsp_ucc_sv_settinggroup" = $row.wsp_ucc_sv_settinggroup; "wsp_ucc_sv_readonly" = $row.wsp_ucc_sv_readonly; "wsp_ucc_sv_active" = $row.wsp_ucc_sv_active}






	}






}













function SetFieldUniqueValues($fields, $unique, [switch]$removeIndex)






{






	if ($unique -eq $null) 






	{






		$unique = $true






	}






	






	foreach ($field in $fields)






	{






		if ($field -eq $null)






		{






			continue






		}













		try






		{






			$replacePointer = "AllowDeletion"






			if ($field.Title -eq "Title") 






			{






				$replacePointer = "FromBaseType"






			}






			






			# Indexed






			$pos = $field.SchemaXml.IndexOf("Indexed")






			if ($pos -eq -1 -and $unique) 






			{






				$field.SchemaXml = $field.SchemaXml.Replace($replacePointer, "Indexed=""TRUE"" $replacePointer")






			}






			else 






			{






				$changeIndex = $true






				$value1 = "FALSE"






				$value2 = "TRUE"






				if (!$unique)






				{






					$value1 = "TRUE"






					$value2 = "FALSE"






					if (!$removeIndex)






					{






						$changeIndex = $false






					}






				}













				if ($changeIndex)






				{






					$pos = $field.SchemaXml.IndexOf("Indexed=""$value1""")






					if ($pos -ne -1) 






					{






						$field.SchemaXml = $field.SchemaXml.Replace("Indexed=""$value1""", "Indexed=""$value2""")






					}






				}






			}













			# EnforceUniqueValues






			$pos = $field.SchemaXml.IndexOf("EnforceUniqueValues")






			if ($pos -eq -1 -and $unique) 






			{






				$field.SchemaXml = $field.SchemaXml.Replace($replacePointer, "EnforceUniqueValues=""TRUE"" $replacePointer")






			}






			else 






			{






				$value1 = "FALSE"






				$value2 = "TRUE"






				if (!$unique)






				{






					$value1 = "TRUE"






					$value2 = "FALSE"






				}













				$pos = $field.SchemaXml.IndexOf("EnforceUniqueValues=""$value1""")






				if ($pos -ne -1) 






				{






					$field.SchemaXml = $field.SchemaXml.Replace("EnforceUniqueValues=""$value1""", "EnforceUniqueValues=""$value2""")






				}






			}













			$field.Update()






			Invoke-PnPQuery






		}






		catch 






		{






			Error $field.Title






			throw






		}






	}






}













function ConvertSingleLineToMultiLineField($field, $numberOfLines, [switch]$reverse)






{






	if ($field -eq $null)






	{






		return






	}













	if ($numberOfLines -eq $null)






	{






		$numberOfLines = 6






	}













	if (!$reverse)






	{






		$field.SchemaXml = $field.SchemaXml.Replace('Type="Text"', 'Type="Note"')






		$pos = $field.SchemaXml.IndexOf("MaxLength")






		if ($pos -ne -1)






		{






			$field.SchemaXml = $field.SchemaXml.Replace('MaxLength="255"', "NumLines=""$numberOfLines"" RichText=""FALSE""")






		}






		else 






		{






			$pos = $field.SchemaXml.IndexOf("NumLines")






			if ($pos -eq -1)






			{






				$field.SchemaXml = $field.SchemaXml.Replace('Type="Note"', "Type=""Note"" NumLines=""$numberOfLines""")






			}






			






			$pos = $field.SchemaXml.IndexOf("RichText")






			if ($pos -eq -1)






			{






				$field.SchemaXml = $field.SchemaXml.Replace('Type="Note"', "Type=""Note"" RichText=""FALSE""")






			}






		}






	}






	else 






	{






		$field.SchemaXml = $field.SchemaXml.Replace('Type="Note"', 'Type="Text"')






		$field.SchemaXml = $field.SchemaXml.Replace("NumLines=""$numberOfLines"" RichText=""FALSE""", 'MaxLength="255"')






	}













	$field.Update()






	Invoke-PnPQuery






}






