SOLVED

Datetime Format for site columns - PNP

Brass Contributor

Hi, I need to add a new site column, with DateTime Type, using PnP.

I can do that using next command:

Add-PnPField -DisplayName "Birthday" -InternalName "dateBirthday" -Group "TEST" -Type DateTime


My problem is that I need create the column with Date only format... When I use previous command by default columns site are created with Date&Time format.


date only.JPG


Thank you!

 

6 Replies

Try this:

 

Add-PnPFieldFromXml '<Field Type="DateTime" DisplayName="My Test Column" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="Custom Columns" FriendlyDisplayFormat="Disabled" ID="{10ce4fed-921a-4d51-a870-534605bf89be}" SourceID="{6cf53ae4-314b-435e-9685-19b7f7b8df07}" StaticName="MyTestColumn2" Name="MyTestColumn2"></Field>'
best response confirmed by María José Pedreira (Brass Contributor)
Solution

Hi @María José Pedreira

 

Try this:

 

Add-PnPFieldFromXml '<Field Type="DateTime" DisplayName="My Test Column" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="Custom Columns" FriendlyDisplayFormat="Disabled" ID="{10ce4fed-921a-4d51-a870-534605bf89be}" SourceID="{6cf53ae4-314b-435e-9685-19b7f7b8df07}" StaticName="MyTestColumn2" Name="MyTestColumn2"></Field>'

 

Hi @Pieter Veenstra,

 

your command works fine!
Could you give me explanation about  ID and SourceID??... Is there any way to avoid guids? 

Add-PnPFieldFromXml '<Field Type="DateTime" 
				DisplayName="My Test Column" 
				Required="FALSE" 
				EnforceUniqueValues="FALSE" 
				Indexed="FALSE" Format="DateOnly" 
				Group="Custom Columns" 
				FriendlyDisplayFormat="Disabled" 
				ID="{10ce4fed-921a-4d51-a870-534605bf89be}" 
				SourceID="{6cf53ae4-314b-435e-9685-19b7f7b8df07}" 
				StaticName="MyTestColumn2" 
				Name="MyTestColumn2">
		</Field>'

  

Hi @María José Pedreira,

 

The sourceID doesn't matter too much. you can even remove that. It's there as I exported the xml from an example field that I created.

 

The ID just needs to be unique in your site collection. 

Ok, I´ll remove SourceId property and I´ll set a random ID with EnforceUniqueValues="TRUE"

 

Your response is perfect for me, thank you again!

I had issue when source ID is not specified on SharePoint 2013 => the search would not crawl the site column..

 

I'm sharing my helper

 

$fieldTitle = "Target Date"
$fieldInternalName= "targetDate"
$guid = [guid]::NewGuid()
cm_Add-PnpField-DateOnly $fieldTitle $fieldInternalName $guid $customGroup

 

function cm_Add-PnpField-DateOnly($fieldTitle,$fieldInternalName,$guid,$fieldGroup){
$targetDate = '<Field Type="DateTime" DisplayName="'+$fieldTitle+'" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="' + $fieldGroup +'" FriendlyDisplayFormat="Disabled" ID="{'+$guid+'}" SourceID="{'+$guid+'}" StaticName="'+$fieldInternalName+'" Name="'+$fieldInternalName+'"></Field>'
Add-PnPFieldFromXml $targetDate
}

1 best response

Accepted Solutions
best response confirmed by María José Pedreira (Brass Contributor)
Solution

Hi @María José Pedreira

 

Try this:

 

Add-PnPFieldFromXml '<Field Type="DateTime" DisplayName="My Test Column" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="Custom Columns" FriendlyDisplayFormat="Disabled" ID="{10ce4fed-921a-4d51-a870-534605bf89be}" SourceID="{6cf53ae4-314b-435e-9685-19b7f7b8df07}" StaticName="MyTestColumn2" Name="MyTestColumn2"></Field>'

 

View solution in original post