Avatar of MisUszatek
MisUszatek
Flag for United States of America asked on

Error messages when creating site columns from xml file

I am trying to use the solution from this post:
http://social.technet.microsoft.com/Forums/en-US/sharepointadminprevious/thread/f4378893-e807-49cd-a955-eeb15a77aa8e

My script looks like this:

[xml]$s = Get-Content NewColumns.xml
$siteColl = Get-SPSite -Identity "http://server/sites/projects/"
$rootWeb = $siteColl.RootWeb
$fields = $s.SelectNodes("/SiteColumns/Field")
$fields | Foreach-Object {
$fieldXMLString = $_.OuterXml.ToString()
$rootWeb.Fields.AddFieldAsXml($fieldXMLString)
}

Open in new window


When I run the script I get the following errors:

Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "The 'SiteColumns' start tag on line 2 does not match the end tag of 'Fie
ld'. Line 4, position 5."
At line:1 char:8
+ [xml]$s <<<<  = Get-Content NewColumns.xml
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException
 
You cannot call a method on a null-valued expression.
At line:4 char:25
+ $fields = $s.SelectNodes <<<< ("/SiteColumns/Field")
    + CategoryInfo          : InvalidOperation: (SelectNodes:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:6 char:39
+ $fieldXMLString = $_.OuterXml.ToString <<<< ()
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Exception calling "AddFieldAsXml" with "1" argument(s): "Root element is missing."
At line:7 char:30
+ $rootWeb.Fields.AddFieldAsXml <<<< ($fieldXMLString)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window


How do I make this work?
Microsoft SharePointPowershell

Avatar of undefined
Last Comment
MisUszatek

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Joe Klimis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
MisUszatek

ASKER
I got the following error when running the script:

You cannot call a method on a null-valued expression.
At line:51 char:58
+     $fieldXMLString = $s.SiteColumns.Field.OuterXml.ToString <<<< ()
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Exception calling "AddFieldAsXml" with "1" argument(s): "Root element is missing."
At line:52 char:31
+     $rootWeb.Fields.AddFieldAsXml <<<< ($fieldXMLString) 
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Open in new window


Is this a problem with the script or my environment?
Joe Klimis

Hi

did you change line 37 to have the name of your SharePoint server ?

Regards
Joe
MisUszatek

ASKER
I figured out my original problem - I had a typo in my xml file. Thank you very much for your help - very much appreciated!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes