简体   繁体   中英

Custom Fields Only From Sharepoint List

Is it possible to loop through field collection of a sharepoint list and retrieve only our custom fields and not the sharepoint built-in fields.

using (SPSite site = new SPSite("http://localhost/"))
{
   using (SPWeb web = site.OpenWeb())
   {
       SPList list = web.Lists["My List"];
       foreach (SPField field in list.Fields)
       {
           //We also get sharepoint built-in column here. And we don't want that, just our
           //custom created fields.
       }
   }
}

Any help would be appreciated.

Thanks

You have two options:

  1. Check if the field is a built-in field: SPBuiltInFieldId.Contains(field.Id)
  2. Check on the SPField.SourceId (from MSDN):

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.

Here's a contrived (and currently untested) way:

string fieldTypeClass = field.FieldTypeDefinition.FieldTypeClass;

if (!(string.IsNullOrEmpty(fieldTypeClass) || fieldTypeClass.StartsWith("Microsoft.SharePoint"))) {
//Only custom fields here
}

我不知道它是否真的是你正在寻找的,如果LINQ仍然被.net digirati“微笑”, 但使用LINQ to Sharepoint可能对你有用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM