Saturday, 26 June 2010

Create a new SharePoint site column using PowerShell

The script below can be used to create a new site column. It assumes you are creating the column in the top-level site of a site collection. It can be used to create columns in a Content Type Hub site, which can then be propagated to other site collections in the farm using the Managed Metadata Service Application (a story for another blog article maybe).

The main advantages of using PowerShell to create the site column are that you can document the changes, easily repeat them across development, staging and production environments, and you can set some properties that are not available to select in the UI (e.g., the “ShowIn” properties at the end of the XML string).

#Assumes that you are creating the site column in the top-level site of a site collection
#Script can also be used to create a site column in the Content Type Hub

#Get site and web object
$site = Get-SPSite -Identity "http://portal"
$web = $site.RootWeb

#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="DateTime"
Name="TestDate"
Description="This is a test date column."
DisplayName="Test Date"
StaticName="TestDate"
Group="Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'

#Output XML to console
write-host $fieldXML

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

#Example of changing the site column settings after creation
#Configure Test Date column to be Date Only instead of Date & Time
$dtField = $web.Fields["Test Date"]
$dtField.DisplayFormat = "DateOnly"
$dtField.Update()

#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()

Note at the end of the script that I call the new column back so that I can change a setting. You could add column settings to the XML during creation, but changing it after creation can be useful if you have already created the column and wanted to make further changes.

29 comments:

  1. This came in handy in my situation

    In the change settings:

    $dtField.PushChangesToLists = "TRUE"

    ReplyDelete
  2. Hi. I used your script to create my site column. I mistyped my field type as 'choice' instead of 'Choice' and that corrupted my site columns page under site settings. Is there any way I can delete my site column again using a powershell script? I can't delete via UI or SharePoint Designer. Thanks.

    ReplyDelete
  3. Try this:

    http://get-spscripts.com/2010/10/cant-remove-site-column-from-content.html

    Phil

    ReplyDelete
  4. Or this if it is a site column:

    http://get-spscripts.com/2011/01/delete-unused-workflow-status-columns.html

    Phil

    ReplyDelete
  5. Hi Phil

    I have the same question as Anonymous above.
    A typo caused my site settings column page to error. I created a custom column but haven't used it in any content type, workflow or list yet. How can I delete it from my site columns?

    Thanks in advance!

    ReplyDelete
  6. Hi - Use the UI to delete the column or following the links I posted above

    ReplyDelete
  7. I can't access the UI pages, so can't delete them over there.
    The links show an example of deleting a column from a specific content type or list, which I don't have.

    Could I do something like this:

    $web = Get-SPWeb http://portal
    $spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($web.Fields["MyCustomField"])
    $web.FieldLinks.Delete($spFieldLink.Id)
    $web.Update()
    $web.Dispose()

    Thanks

    ReplyDelete
  8. Try this:

    $web = Get-SPWeb http://portal
    $column = $web.Fields["Bad Column"]
    $column.Delete()
    $web.Dispose()

    ReplyDelete
  9. I'm getting a null value.
    Investigating why, I'll let you know if I find the problem.

    Thanks for your feedback.

    ReplyDelete
  10. how this site column created can be referenced to a list using powershell

    ReplyDelete
  11. Use SharePoint Manager 2010?

    ReplyDelete
  12. Thanks! I am working on a script to read parameters from a file and build a set of site columns. Your script is very helpful. - Doug

    ReplyDelete
  13. hi , I have a custom site column called 'reviewed' and this field type is 'checkbox'. we are referring this field in 30 contenttypes. these cotent types associated to 10 document libraries. now i want to change this field type to
    choice - dropdown , with required field option.

    how can i change 'type' of existing site column using powershell ?

    i tried to use below powershell script...no luck

    $field =$RootWeb.fields.getfield("test1")
    $field.Type= "Choice"
    #$field.Update($true)
    $field.Choices.Add("Yes")
    $field.Choices.Add("No")
    $field.Required=$true
    $field.Update($true)

    ReplyDelete
  14. With havin so much written content do you ever run into any problems of plagorism or copyright violation?
    My site has a lot of unique content I’ve either authored myself or outsourced but it seems a lot of it is….
    popping it up all over the internet without my agreement.
    Do you know any solutions to help prevent content from being ripped off?

    ReplyDelete
  15. I’ve been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before.

    ReplyDelete
  16. Thanks for sharing superb informations. Your web-site is very cool. I am impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched everywhere and simply could not come across. What a great web-site.

    ReplyDelete
  17. Nice stuff, but don't you need an ID field ?

    ReplyDelete
  18. After I originally commented I seem to have clicked on the -Notify me when new comments are added- checkbox and now every time a comment
    is added I recieve 4 emails with the same comment. There has to be an easy method you can remove me from that service?
    Thank you!

    ReplyDelete
  19. Hi! Do you know if they make any plugins to assist with
    Search Engine Optimization? I’m trying to get my blog to rank for some
    targeted keywords but I’m not seeing very good results.
    If you know of any please share. Thanks!

    ReplyDelete
  20. Divorcemind.com is a online divorce resource for divorce attorney, attornies and lawyers. You will find the best divorce information online. Divorce Mind will make your divorce or separation a quick, convenient and private....

    ReplyDelete
  21. Saya pribadi selalu mencari nafkah lewat taruhan bola on-line. Jujur saja dalam sehari saya dapat memperoleh minimum 200 ribu rupiah. Ini merasa begitu gampang sekali. 200k rupiah di kalikan dengan 30 hari yaitu 6 juta rupiah. Benar-benar cukup untuk saya untuk hidup melalui hari. agen sbobet.Sudah pasti untuk mengais rejeki di bagian taruhan bola on-line tidaklah hal gampang untuk pemula.

    Stay selalu di situs (blog) saya seandainya anda menginginkan menuntut pengetahuan perjudian on-line yang hasilkan kemenangan. agen sbobet. Beberapa hal perlu yang perlu diperhatikan diantaranya yaitu mencari agen bola sbobet yang legit atau terpercaya. Artikel saya kesempatan ini yaitu pengalaman saya selamA 1 th. 2 bulan menempatkan taruhan bola dengan website agen sbobet terpercaya agen sbobet Saya ringkaskan dalam judul Review Agen Sbobet Terpercaya. agen sbobet. Hal apa sajakah yang buat saya kerasan bermain dengan agen sbobet ini? Simak berikut ini.

    agen sbobet Melayani Mulai sejak 2013

    Sudah pasti dalam pilih agen bola yang dapat diakui yaitu lamanya berdiri website itu. agen sbobet adalah satu diantaranya. agen sbobet. Pertama sekali saya daftarkan diri, sesungguhnya terbesit kesangsian apakah agen yang satu ini legit atau tidak. Saya check lewat internet mengenai berapakah lama usia domain itu. Nyatanya telah mulai sejak 2012 domain agen sbobet terbeli. Ini sedikit menghapus kesangsian saya yang semula muncul. Simak video pendaftaran anggota berikut ini.


    Service Customer Dukungan Yang Memuaskan

    Saya lalu buka chat dengan menyebut Customer Service mereka lewat Live Chat. Kesan pertama yaitu kesopanan. Cs itu lalu dengan friendly menerangkan profile website agen mereka. Lalu beliau menerangkan mengenai langkah mengolah transaksi, langkah lakukan penarikan bila saya menang, serta pastinya grade tiap-tiap product. Dalam hal semacam ini,
    agen sbobet. saya pilih Sbobet untuk taruhan bola on-line. Selesai pendaftaran, saya lalu coba membulatkan tekad kirim dana sebesar minimum deposit sesuai sama ketentuan website agen sbobet. Sesudah lakukan penelusuran grade apa yang saya dapatkan dalam user id Sbobet , nyatanya benar sekali.

    User Sbobet saya peroleh grade A. Ini sesuai sama apa yang tercantum di dalam website mereka. Kejujuran yaitu hal paling penting dalam melakukan bisnis taruhan on-line.
    agen sbobet. Website ini memberi kesan itu pertama kalinya mengunjunginya. Cs yaitu orang yang paling dekat dengan player.
    agen sbobet. Customer Supportlah yang melindungi data diri pemain aman dari segalanya jelek. bola tangkas Hargai mereka.

    ReplyDelete