Unable to create a site column named Department using PnP/power-shell script. Error “A duplicate field name ”Department“ was found.”

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/251680

سؤال

I am working on a sharepoint online enterprise wiki site collection. and i want to create a new site column named "Department" of type choice. now i already know that there is a built-in column which has a Display name = Department of type single-line of text, but which has its internal name = ol_Department . so i know that i will not be able to create my site column using the UI, since i will get an error that the Department name already exists. but i use to add the column using power-shell script, since i will define a different internal name. so i try running this power-shell script:-

Add-Type -Path (Resolve-Path "$env:Common\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:Common\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")

#Site collection URL
$siteurl = "https://**********.sharepoint.com/sites/I*******/"

$userName ="admin@******.onmicrosoft.com" 
$password ="**********" 

#client context object and setting the credentials   
[Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)   

# convert password into secure string  
$securedpassword = ConvertTo-SecureString $password -AsPlainText -Force   

$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpassword) 

#get the rootweb
$site = $clientContext.Site  
$web = $site.RootWeb  

#Get fields collection  
$fields = $web.Fields   

$clientContext.Load($web)  
$clientContext.Load($fields)   

$clientContext.ExecuteQuery()   

#your field xml
$fieldXML = "<Field Type='Choice'
Name='Department'
Description=''
DisplayName='Department'
StaticName='Department'
Group='Custom Columns'
Hidden='FALSE'
Required='TRUE'
Sealed='FALSE'
ShowInDisplayForm='TRUE'
ShowInEditForm='TRUE'
ShowInListSettings='TRUE'
ShowInNewForm='TRUE'>
<CHOICES>
<CHOICE>other</CHOICE>
</CHOICES>
</Field>"
$fields.AddFieldAsXml($fieldXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddToDefaultContentType)    
$clientContext.Load($fields)  
$clientContext.ExecuteQuery() 

but this script raised the following error, and my column was not added:-

Exception calling "ExecuteQuery" with "0" argument(s): "A duplicate field name "Department" was found."
At line:20 char:1
+ $clientContext.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

as i mentioned i should be able to create site column which has existing display names as long as the internal name is unique,, so i am not sure why i am getting this error? is there any update on sharepoint online which is preventing us from doing so?

Thanks

هل كانت مفيدة؟

المحلول

You are correct you can have 2 columns with the same display name. But normally at least in case of on prem there are 2 departments columns:

1 DL=Department, SN=Department, IN=Department 2.DL=Department, SN=ol_Departement, IN=ol_Department,

So you cannot create another column with same internal/static name. You need to either reuse this column or create with a slighty diferent name ie. "Dept."

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top