Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working on a Sharepoint 2010 site. I am trying to run this script from a website in order to create an xml containing info on my site columns.

$sourceWeb = Get-SPWeb http://farmstuff:1284
$xmlFilePath = "C:\Users\User\Scripts\Script-SiteColumns.xml"

#Create Export Files
New-Item $xmlFilePath -type file -force

#Export Site Columns to XML file
Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
Add-Content $xmlFilePath "`n<Fields>"
$sourceWeb.Fields | ForEach-Object {
   if ($_.Group -eq "Custom Columns") {
        Add-Content $xmlFilePath $_.SchemaXml
    }
}
Add-Content $xmlFilePath "</Fields>"

$sourceWeb.Dispose()

And I get the following error:

An error occurred while enumerating through a collection: Exception has been thrown by the target of an invocation..
At C:\Users\User\Scripts\get-sitecolumns.ps1:10 char:5
+     <<<< %sourceWeb.Fields | ForEach-Object {
    + CategoryInfo           : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], Runtime Exception
    + FullyQualifiedErrorId: BadEnumeration

Any help would be appreciated! I am not too familiar with Sharepoint or Powershell nuances yet. Thanks.

Edit:

<Field ID="{3C0E9E00-8FCC-479f-9D8D-3447CDA34C5B}" Name="OtherAddressCountry" StaticName="OtherAddressCountry" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Other Address Country" Group="Core Contact and Calendar Columns" Type="Text" Sealed="TRUE" AllowDeletion="TRUE" />
<Field ID="{9EBCD900-9D05-46c8-8F4D-E46E87328844}" Name="Categories" StaticName="Categories" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Categories" Group="Base Columns" Type="Text" Sealed="TRUE" AllowDeletion="TRUE" />
share|improve this question
    
Does $web.Fields | % { $_.SchemaXml } alone works? In fact, I'm suspecting you have a unknown file type. This line should also fail in this case. –  Steve B Jul 19 '12 at 14:43
1  
If your code is copied from some site then it may have hidden characters or the apostrope near "`n<Fields>" may need to be deleted and reintroduced. I had similar situation where I had to go through line by line and fix hidden or messed up characters. I used Notepad++. –  Bill Baer Jul 19 '12 at 14:46
    
@SteveB, I'm editing my main post with what's output when using your line (and replacing $web with $sourceWeb). It looks like it works twice and then chokes on something? I checked the original XML file and it has the same data in it as well. –  John Smith Jul 19 '12 at 15:10
    
@BillBaer, I'll check. Thanks. –  John Smith Jul 19 '12 at 15:12
    
I hope this will works, try this : $sourceWeb.Fields[2] or $sourceWeb.Fields[2].ID if the former fail. If you are lucky, you will find the ID of the Field that is causing the issue. Starting with this ID, you will be able to search the faulty field. Probably a custom feature. I suggest you to use Notepad++ and do a search within all files of the 14 hives of kind xml, with the found ID as pattern. If I'm right, you'll find a <Field ID="xxx" />. Please post its declaration then. –  Steve B Jul 19 '12 at 15:16
show 1 more comment

1 Answer

Are you flexible and try the following method

foreach ($field in $web.Fields)
{
  if($field.Group -eq "Custom Column Group")
  {
     DO YOUR THING
  }
}
share|improve this answer
    
I get the following. C:\Users\User\Scripts\get-sitecolumns.ps1 : Exception has been thrown by the target of an invocation. At line:1 char2: + & <<<< .\get-sitecolumns.ps1 + CategoryInfo : NotSpecified: (:) [get-sitecolumns.ps1], TargetInvocationException + FullyQualifiedErrorId: System.Reflection.TargetInvocationException,get-sitecolumns.ps1 By the way I'm running this from the console with & –  John Smith Jul 19 '12 at 15:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.