Guest User

Untitled

a guest
Oct 29th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. Add-Type -Path (Resolve-Path "$env:CommonProgram FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll")
  2. Add-Type -Path (Resolve-Path "$env:CommonProgram FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll")
  3.  
  4. #Site collection URL
  5. $siteurl = "https://**********.sharepoint.com/sites/I*******/"
  6.  
  7. $userName ="admin@******.onmicrosoft.com"
  8. $password ="**********"
  9.  
  10. #client context object and setting the credentials
  11. [Microsoft.SharePoint.Client.ClientContext]$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
  12.  
  13. # convert password into secure string
  14. $securedpassword = ConvertTo-SecureString $password -AsPlainText -Force
  15.  
  16. $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpassword)
  17.  
  18. #get the rootweb
  19. $site = $clientContext.Site
  20. $web = $site.RootWeb
  21.  
  22. #Get fields collection
  23. $fields = $web.Fields
  24.  
  25. $clientContext.Load($web)
  26. $clientContext.Load($fields)
  27.  
  28. $clientContext.ExecuteQuery()
  29.  
  30. #your field xml
  31. $fieldXML = "<Field Type='Choice'
  32. Name='Department'
  33. Description=''
  34. DisplayName='Department'
  35. StaticName='Department'
  36. Group='Custom Columns'
  37. Hidden='FALSE'
  38. Required='TRUE'
  39. Sealed='FALSE'
  40. ShowInDisplayForm='TRUE'
  41. ShowInEditForm='TRUE'
  42. ShowInListSettings='TRUE'
  43. ShowInNewForm='TRUE'>
  44. <CHOICES>
  45. <CHOICE>other</CHOICE>
  46. </CHOICES>
  47. </Field>"
  48. $fields.AddFieldAsXml($fieldXML, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddToDefaultContentType)
  49. $clientContext.Load($fields)
  50. $clientContext.ExecuteQuery()
  51.  
  52. Exception calling "ExecuteQuery" with "0" argument(s): "A duplicate field name "Department" was found."
  53. At line:20 char:1
  54. + $clientContext.ExecuteQuery()
  55. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  57. + FullyQualifiedErrorId : ServerException
Add Comment
Please, Sign In to add comment