3
votes

Hello and thanks for looking!

Background

I currently have a C# method for looping through a SharePoint List Collection and returning a lists of those SP Lists, including a nested list of their columns/SPFields.

Problem

How do I get a list of ONLY user-created fields in a SharePoint 2007 List via C#?

Code so far. . .

SPSite site = SPContext.Current.Site;
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        SPListCollection lists = web.Lists;

        var PellaListCollection = new List<PellaListModel>();
        
        foreach (SPList l in lists) {
                var PellaList = new PellaListModel();
                var PellaListColumns = new List<PellaListColumn>();
                foreach (SPField c in l.Fields) {
                    if (c.Hidden.Equals(false))
                    {
                        var type = c.FieldTypeDefinition.TypeName.ToString();
                        var col = new PellaListColumn
                        {
                            ColumnId = c.Id,
                            ColumnDataType = type,
                            ColumnTitle = c.Title
                        };
                        PellaListColumns.Add(col);
                    }
                }
                PellaList.ListColumns = PellaListColumns;
                PellaList.ListId = l.ID;
                PellaList.ListTitle = l.Title;
                PellaList.Description = l.Description;
                PellaListCollection.Add(PellaList);
        }
        
        web.AllowUnsafeUpdates = false;

        return PellaListCollection;
    }

As you can see, right now I am filtering by which SPFields are not "Hidden", but this still returns quite a few of the standard SharePoint generated fields with the list. I just need the fields the users have created.

Thanks!

Matt

2
Are you trying to get at Users Info..? or a custom user list that users have each created..?MethodMan

2 Answers

2
votes

You are looking for the FromBaseType property of SPField. This property is true if the field is part of the original schema for the List.

Check out this good SharePoint Exchange article for more.

0
votes

You could potentially test the SourceId in the SPField.

From the MSDN documentation:

"Gets either the namespace that defines a built-in field or, if it a custom field, the GUID that identifies the list or Web site where it was created."