Powershell create site columns from XML
Hi,
I need to create around 50 custom site columns via powershell.
My XML file has this content
<SiteColumns>
<Field Type="DateTime"
Name="DOcumentationReview"
Description="Shows the next review date of the document"
DisplayName="Development Review Team"
StaticName="DevReviewReview"
Group="SP2010 Applications"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>
</SiteColumns>
I decided to read the xml file in the usual way as
[xml]$s = get-content sitecolumns.xml
Now I want to get the complete node <Field as a string so that I can use in this fashion.
$siteColl = Get-SPSite -Identity "http://mydevsite/"
$rootWeb = $siteColl.RootWeb
$fieldXMLString = '<Field Type="DateTime"
Name="DOcumentationReview"
Description="Shows the next review date of the document"
DisplayName="Development Review Team"
StaticName="DevReviewReview"
Group="SP2010 Applications"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
$rootWeb.Fields.AddFieldAsXml($fieldXMLString)
How can I loop through the 'Field' node and get the entire node content as a string?
November 28th, 2011 1:30am
Hi Cutloo,
If I understand correctly, you have multiple fields under <SiteColumns>, you want to loop through each field, and then add them to SharePoint.
If so, you can use the following PowerShell code:
[xml]$s = get-content sitecolumns.xml Now I want to get the complete node <Field as a string so that I can use in this fashion. $siteColl = Get-SPSite -Identity "http://mydevsite/" $rootWeb = $siteColl.RootWeb $fields = $s.SelectNodes("/SiteColumns/Field") $fields | Foreach-Object { $fieldXMLString = $_.OuterXml.ToString() $rootWeb.Fields.AddFieldAsXml($fieldXMLString) }
If you have any more questions, please feel free to ask.
Thanks,
Jinchun Chen
Free Windows Admin Tool Kit Click here and download it now
November 29th, 2011 7:44am
Is there a way to get this to work for Taxonomy columns?
Tried the above with an XML file I have for a Managed Metadata field and I can't get the fields to create (Managed metadata column consists of two columns a TaxonomyFieldType and Text field).
July 23rd, 2015 10:23pm