Office 365 and PnP-PowerShell 2013 : Fields

Now the time to learn about the Fields or so called SharePoint “List” and “Content Type” Columns. 

The below mentioned cmdlets lets you create and remove the Fields to content type and directly to the SharePoint List. 




CmdletDescription
Add‑PnPFieldAdds a field to a list or as a site column
Get‑PnPFieldReturns a field from a list or site
Remove‑PnPFieldRemoves a field from a list or a site
Add‑PnPFieldFromXmlAdds a field to a list or as a site column based upon a CAML/XML field definition
Add‑PnPTaxonomyFieldAdds a taxonomy field to a list or as a site column.
If you want to take a better control over to the list columns better way to design and developed via “Add‑PnPFieldFromXml” method. As this method you only need to work on the XML part for this you need to invest your time to check CAML Documentation.




So I am going use “Add‑PnPFieldFromXml” cmdlets to create the utility to create the columns and configuration done via CSV file.
So before going ahead you need to know about the basic understanding about the CAML  Documentation.




Let’s quickly jump in to the example. This Utility is tried and tested please feel free if you face any issue related to this scripts. 

In this example, I am going to create the CSV files called "SiteColumns.CSV" in which I will provide you all the type of the columns example. 

  1. Choice
  2. Text
  3. User
  4. DateTime
  5. Boolean
  6. Number
  7. Note
  8. Currency
  9. LookupMulti



Also Please check the all the columns closely before going ahead to do any modification mostly please keep check the "optional" columns in the csv which is used to enter the optional data into the Field Xml like make the column to enter only datetime data, or make it required. Also the "optional" field is separated by  Pipe "|" symbol to enter the multiple values inside the tags. Also for example if you set the default value to the choice column.



Here is the Source code. Hope you like it and please let me know your issue and feedback about this. 



#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
view raw Fields.ps1 hosted with ❤ by GitHub
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^|>
view raw SiteColumns.csv hosted with ❤ by GitHub


Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)

STS CryptographicException Error : Key set does not exist