Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -Path (Resolve-Path "$env:CommonProgram FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll")
- Add-Type -Path (Resolve-Path "$env:CommonProgram FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.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()
- 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
Add Comment
Please, Sign In to add comment