Skip to content

Instantly share code, notes, and snippets.

@BasantPandey
Created July 30, 2017 10:51
Show Gist options
  • Save BasantPandey/e2a617b5611a80a72c373c35aadb0b95 to your computer and use it in GitHub Desktop.
Save BasantPandey/e2a617b5611a80a72c373c35aadb0b95 to your computer and use it in GitHub Desktop.
Create Fields with Configuration CSV file https://sharepointfordeveloper.blogspot.com
#Dev Import
$Path = "C:\SharePointPnPPowerShell2013\"
Import-Module '$Path\SharePointPnP.PowerShell.2013.Commands.dll';
#-----Function to create Site Columns----#
function CreateSiteColumns($csvPath, $SiteURL) {
$Cred = $global:GlobalCred
$newUtfFile = "SiteCollectionSiteColumns_utf8.csv";
If (Test-Path $newUtfFile ) {
Remove-Item $newUtfFile
}
Get-Content $csvPath | Out-File $newUtfFile -Encoding utf8
# Get SPWeb Object
# Reads the csv file and gets all records in one collection
$MyFile = import-csv -Delimiter "," -Path $newUtfFile -Encoding Unicode
$Web = $null;
Connect-PnPOnline –Url $siteUrl –Credentials $Cred
$message = "Connect to site $siteUrl"
# Looping the csv file collection
foreach ($myRow in $MyFile) {
# Reading Site Column Attributes values from the csv file collection
$colGroup = $myRow.Group
$desc = $myRow.ColumnDescription
$columnName = $myRow.SiteColumnName
if ($myRow.ColumnDisplayName -ne $null -and $myRow.ColumnDisplayName -ne "") {
$columnDisplayName = $myRow.ColumnDisplayName.Replace("&", "&")
}
$fieldGUID = $myRow.SiteColumnID
$type = $myRow.DataType
$AdditionalFields = $myRow.AdditionalFields
$req = $myRow.IsRequiredField
$space = " "
$required = "";
if ( $req -ne "" -and $req -ne $null) {
$required = ' Required ="' + $req + '" '
}
# Making SPField XML For using while creating Field
$caml = $myRow.CAML + $type + $myRow.CAML2 + $columnName + $myRow.CAML3 + $columnDisplayName + $myRow.CAML4 + $columnName + $myRow.CAML5 + $fieldGUID + $myRow.CAML6
foreach ($isParam in $myRow.Optional.split("|")) {
if ($isParam -eq "required") {
$caml = $caml + $space + $required
}
else {
$caml = $caml + $space + $isParam
}
}
# If Field is a Choice Type and we want to assign default values in that Field
if ($type -eq "Choice" -or $type -eq "MultiChoice") {
$caml = $caml + "<CHOICES>"
foreach ($isChoice in $myRow.Choice.split("|")) {
if ($isChoice -ne $null -and $isChoice -ne "") {
$caml = $caml + "<CHOICE>" + $isChoice.Replace("&", "&amp;") + "</CHOICE>"
}
}
$caml = $caml + "</CHOICES>"
}
if ($type -ne "TaxonomyFieldType") {
$caml = $caml + $myRow.CAML7
}
try {
$IscolExit = Get-PnPField -Identity $columnName -ErrorAction Stop
write-host "Column $columnName already exists."
Write-Host "Column $columnName already exists."
}
catch {
if ($type -ne "TaxonomyFieldType") {
if ($type -eq "Lookup" -or $type -eq "LookupMulti") {
try {
$SourceID = "http://schemas.microsoft.com/sharepoint/v3"
$ListGUID = Get-PnPList -Identity "$LookupList"
Get-PnPProperty -ClientObject $ListGUID -Property Id
$replaceValue ="^Lookup^";
if($LookupListColumn -eq $null -and $LookupListColumn -eq ""){
$LookupListColumn="Title"
}
if($myRow.IsMultiValued -ne $null -and $myRow.IsMultiValued -ne ""){
if([System.Convert]::ToBoolean($myRow.IsMultiValued)){
$ReplceString =' List="{'+$($ListGUID.Id) +'}" ShowField="'+$LookupListColumn+'" Mult="TRUE"'
}
else
{
$ReplceString =' List="{'+$($ListGUID.Id) +'}" ShowField="'+$LookupListColumn+'"'
}
}
$Web = Get-PnPWeb
$caml = $caml.Replace($replaceValue,$ReplceString);
$caml = $caml.Replace($SourceID, $Web.Id);
try{
$message = "Create New Column : $columnName and xml $caml"
Write-Host $caml
Write-Host $message -ForegroundColor Green
$output = Add-PnPFieldFromXml -FieldXml $caml
$output.set_Description($desc)
$output.set_Group($colGroup)
$output.Update();
Execute-PnPQuery
$message = "Create New Column : $columnName "
Write-Host $message
Write-Host $message -ForegroundColor Green
}catch{
write-host "ERROR - Column $columnName NOT created."
Write-Host "ERROR - Column $columnName NOT created."
Write-Host "$($_.Exception.Message)"
}
}
catch {
Write-Host "$($_.Exception.Message)"
}
}
else {
try {
Write-Host $caml
$output = Add-PnPFieldFromXml -FieldXml $caml
$output.set_Description($desc)
$output.set_Group($colGroup)
$output.Update();
Execute-PnPQuery
$message = "Create New Column : $columnName "
Write-Host $message
Write-Host $message -ForegroundColor Green
}
catch {
write-host "ERROR - Column $columnName NOT created."
Write-Host "ERROR - Column $columnName NOT created."
Write-Host "$($_.Exception.Message)"
}
}
}
else {
#For tax Columns----
}
}
}
}
$siteUrl = "http://myintranet.com/sites"
$path = 'C:\Users\basantp\Documents\PNP Blog Section\Code\Fields'
$inputFile = $path + "\SiteColumns.csv"
CreateSiteColumns $csvPath $SiteURL
SiteColumnID SiteColumnName ColumnDisplayName DataType ColumnDescription Group LookupListName LookupListColumn IsRequiredField Choice IsMultiValued CAML CAML2 CAML3 CAML4 CAML5 CAML6 CAML7 Optional AdditionalFields
0c208fc0-65f3-400d-98bc-2935ab067404 My_Status Status Choice This is choice fields My_Fields TRUE New|In progress|Finalized|Declined|Waiting for reply|Offboarding <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|> <Default>New</Default>
0c208fc0-65f3-400d-98bc-2935ab067406 My_FirstName First name Text This is first Name My_Fields TRUE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|>
0c208fc0-65f3-400d-98bc-2935ab067408 My_User AD Account User This is User Fields My_Fields FALSE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required| UserSelectionMode="0" | Indexed = "TRUE" | EnforceUniqueValues = "TRUE" >
0c208fc0-65f3-400d-98bc-2935ab067409 My_StartDate Start Date DateTime This is date Fields My_Fields TRUE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> Format="DateOnly"|required|>
0c208fc0-65f3-400d-98bc-2935ab067422 My_Booean Boolean Boolean This is boolean My_Fields FALSE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|>
0c208fc0-65f3-400d-98bc-2935ab067428 My_Number Number Number This is a number My_Fields FALSE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|>
0c208fc0-65f3-400d-98bc-2935ab067453 My_Note Note Note My_Fields FALSE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|>
0c208fc0-65f3-400d-98bc-2935ab167514 My_Currency Currency Currency My_Fields TRUE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|>
0c208fc0-65f3-400d-98bc-2935ab067517 My_Lookup Lookup LookupMulti My_Fields Lists/LookupList Title FALSE FALSE <Field Type=" " Name=" " DisplayName=" " StaticName=" " ID="{ }" SourceID="http://schemas.microsoft.com/sharepoint/v3" </Field> required|^Lookup^|>
@BasantPandey
Copy link
Author

To learn more about the PNP-PowerShell Ojbect Model. Please visit my blog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment