Search code examples
c#powershellsharepoint-2010

get custom fields only from a sharepoint list


Can anyone tell me how can I get the custom fields from a sharepoint list? I don't want to get all the fields like hidden, just the custom ones or the ones in the default view. The end result will be to save the schemaxml for each field in an xml file.

I am trying to do it like this:

SPfieldCollection fields = list.DefaultView.ViewFields
foreach(SPField field in fields)
{
....
}

Powershell or C# will be very helpful. Thank you


Solution

  • Properties you want to check are Hidden, ReadOnlyField. If either of these is TRUE, it means that this field is a 'system field'.

    And then you can check (and filter) remaining fields - Attachments, Title, ... - by it's internal name.

    Here is the script that will filter fields:

    $w = get-spweb http://localhost
    $l = $w.Lists["Custom list"]
    $l.Fields | ? { $_.Hidden -eq $false -and $_.ReadOnlyField -eq $false -and $_.Title -ne "Attachments" } | select title