SOLVED

Datetime Format for site columns - PNP

%3CLINGO-SUB%20id%3D%22lingo-sub-83555%22%20slang%3D%22en-US%22%3EDatetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83555%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20need%20to%20add%20a%20new%20site%20column%2C%20with%20DateTime%20Type%2C%20using%20PnP.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20can%20do%20that%20using%20next%20command%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CPRE%3EAdd-PnPField%20-DisplayName%20%22Birthday%22%20-InternalName%20%22dateBirthday%22%20-Group%20%22TEST%22%20-Type%20DateTime%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EMy%20problem%20is%20that%20I%20need%20create%20the%20column%20with%20Date%20only%20format...%20When%20I%20use%20previous%20command%20by%20default%20columns%20site%20are%20created%20with%20Date%26amp%3BTime%20format.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20536px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F16629i123E20A6756C2D54%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22date%20only.JPG%22%20title%3D%22date%20only.JPG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThank%20you!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-343658%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-343658%22%20slang%3D%22en-US%22%3E%3CP%3EI%20had%20issue%20when%20source%20ID%20is%20not%20specified%20on%20SharePoint%202013%20%3D%26gt%3B%20the%20search%20would%20not%20crawl%20the%20site%20column..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20sharing%20my%20helper%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24fieldTitle%20%3D%20%22Target%20Date%22%3CBR%20%2F%3E%24fieldInternalName%3D%20%22targetDate%22%3CBR%20%2F%3E%24guid%20%3D%20%5Bguid%5D%3A%3ANewGuid()%3CBR%20%2F%3Ecm_Add-PnpField-DateOnly%20%24fieldTitle%20%24fieldInternalName%20%24guid%20%24customGroup%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efunction%20cm_Add-PnpField-DateOnly(%24fieldTitle%2C%24fieldInternalName%2C%24guid%2C%24fieldGroup)%7B%3CBR%20%2F%3E%24targetDate%20%3D%20'%3CFIELD%20type%3D%22%26quot%3BDateTime%26quot%3B%22%20displayname%3D%22%26quot%3B'%2B%24fieldTitle%2B'%26quot%3B%22%20required%3D%22%26quot%3BFALSE%26quot%3B%22%20enforceuniquevalues%3D%22%26quot%3BFALSE%26quot%3B%22%20indexed%3D%22%26quot%3BFALSE%26quot%3B%22%20format%3D%22%26quot%3BDateOnly%26quot%3B%22%20group%3D%22%26quot%3B'%22%3E%3C%2FFIELD%3E'%3CBR%20%2F%3EAdd-PnPFieldFromXml%20%24targetDate%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-83577%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83577%22%20slang%3D%22en-US%22%3E%3CP%3EOk%2C%20I%C2%B4ll%20remove%20SourceId%20property%20and%20I%C2%B4ll%20set%20a%20random%20ID%20with%26nbsp%3BEnforceUniqueValues%3D%22TRUE%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYour%20response%20is%20perfect%20for%20me%2C%20thank%20you%20again!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-83574%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83574%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F47968%22%20target%3D%22_blank%22%3E%40Mar%C3%ADa%20Jos%C3%A9%20Pedreira%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20sourceID%20doesn't%20matter%20too%20much.%20you%20can%20even%20remove%20that.%20It's%20there%20as%20I%20exported%20the%20xml%20from%20an%20example%20field%20that%20I%20created.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20ID%20just%20needs%20to%20be%20unique%20in%20your%20site%20collection.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-83570%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83570%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2387%22%20target%3D%22_blank%22%3E%40Pieter%20Veenstra%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eyour%20command%20works%20fine!%3CBR%20%2F%3ECould%20you%20give%20me%20explanation%20about%20%26nbsp%3BID%20and%20SourceID%3F%3F...%20Is%20there%20any%20way%20to%20avoid%20guids%3F%26nbsp%3B%3C%2FP%3E%3CPRE%3EAdd-PnPFieldFromXml%20'%26lt%3BField%20Type%3D%22DateTime%22%20%0A%20%20%20%20DisplayName%3D%22My%20Test%20Column%22%20%0A%20%20%20%20Required%3D%22FALSE%22%20%0A%20%20%20%20EnforceUniqueValues%3D%22FALSE%22%20%0A%20%20%20%20Indexed%3D%22FALSE%22%20Format%3D%22DateOnly%22%20%0A%20%20%20%20Group%3D%22Custom%20Columns%22%20%0A%20%20%20%20FriendlyDisplayFormat%3D%22Disabled%22%20%0A%20%20%20%20ID%3D%22%7B10ce4fed-921a-4d51-a870-534605bf89be%7D%22%20%0A%20%20%20%20SourceID%3D%22%7B6cf53ae4-314b-435e-9685-19b7f7b8df07%7D%22%20%0A%20%20%20%20StaticName%3D%22MyTestColumn2%22%20%0A%20%20%20%20Name%3D%22MyTestColumn2%22%26gt%3B%0A%20%20%26lt%3B%2FField%26gt%3B'%3C%2FPRE%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-83563%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83563%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F47968%22%20target%3D%22_blank%22%3E%40Mar%C3%ADa%20Jos%C3%A9%20Pedreira%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EAdd-PnPFieldFromXml%20'%26lt%3BField%20Type%3D%22DateTime%22%20DisplayName%3D%22My%20Test%20Column%22%20Required%3D%22FALSE%22%20EnforceUniqueValues%3D%22FALSE%22%20Indexed%3D%22FALSE%22%20Format%3D%22DateOnly%22%20Group%3D%22Custom%20Columns%22%20FriendlyDisplayFormat%3D%22Disabled%22%20ID%3D%22%7B10ce4fed-921a-4d51-a870-534605bf89be%7D%22%20SourceID%3D%22%7B6cf53ae4-314b-435e-9685-19b7f7b8df07%7D%22%20StaticName%3D%22MyTestColumn2%22%20Name%3D%22MyTestColumn2%22%26gt%3B%26lt%3B%2FField%26gt%3B'%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-83562%22%20slang%3D%22en-US%22%3ERe%3A%20Datetime%20Format%20for%20site%20columns%20-%20PNP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-83562%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EAdd-PnPFieldFromXml%20'%26lt%3BField%20Type%3D%22DateTime%22%20DisplayName%3D%22My%20Test%20Column%22%20Required%3D%22FALSE%22%20EnforceUniqueValues%3D%22FALSE%22%20Indexed%3D%22FALSE%22%20Format%3D%22DateOnly%22%20Group%3D%22Custom%20Columns%22%20FriendlyDisplayFormat%3D%22Disabled%22%20ID%3D%22%7B10ce4fed-921a-4d51-a870-534605bf89be%7D%22%20SourceID%3D%22%7B6cf53ae4-314b-435e-9685-19b7f7b8df07%7D%22%20StaticName%3D%22MyTestColumn2%22%20Name%3D%22MyTestColumn2%22%26gt%3B%26lt%3B%2FField%26gt%3B'%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
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 (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
}