1

i am working on an online classic team site, and i have a site column named "AutomaticallyCreated" which is set to hidden inside create,edit and display form. now i want to show it inside the Edit and display forms, so i run the following powershell script :-

$User = "admin@****.onmicrosoft.com"
$SiteURL = "https://***.sharepoint.com/sites/**** #enter site collection url"


#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "enterpassword" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

$fieldTitle = "AutomaticallyCreated"
$customfield = $Context.Site.RootWeb.Fields.GetByInternalNameOrTitle($fieldTitle) 
$customfield.ShowInEditForm($true)
$customfield.ShowInDisplayForm($true)
$customfield.UpdateAndPushChanges($true)

$Context.ExecuteQuery()

but i got these exceptions:-

Method invocation failed because [Microsoft.SharePoint.Client.Field] doesn't
contain a method named 'ShowInEditForm'.
At line:1 char:1
+ $customfield.ShowInEditForm($true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [Microsoft.SharePoint.Client.Field] doesn't
contain a method named 'ShowInDisplayForm'.
At line:1 char:1
+ $customfield.ShowInDisplayForm($true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

so can anyone advice on this please? the fields should have methods named ShowInEditForm & ShowInDisplayForm..

2

You need to use SetShowInEditForm and SetShowInDisplayForm when using CSOM.

Modify it as below:

$customfield.SetShowInEditForm($true)
$customfield.SetShowInDisplayForm($true)

Reference - Field.SetShowInEditForm method

Field.SetShowInDisplayForm method

  • thanks for the reply.. now i am getting this error Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (404) Not Found." – SharePoint TestDev Jan 25 at 12:31
  • 1
    are you using pnp powershell ? I see that you have tagged the question using that ? Also, please check if you have connected to the correct site collection having the field that you require. – Gautam Sheth Jan 25 at 12:42
  • yes i am running this code using sharepoint online managment shell and i am connected to the site – SharePoint TestDev Jan 25 at 12:47
  • 1
    yes, i guess that is outdated now. The correct method is indeed SetShowInEditForm. Not sure why you are getting 404 error. What type of field are you trying to change ? – Gautam Sheth Jan 25 at 13:05
  • 1
    single line of text.. i think i have an un-closed " .. now the scritp worked well.. – SharePoint TestDev Jan 25 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.