Custom Fields Only From Sharepoint List

霸气de小男生 提交于 2019-12-23 12:18:24

问题


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


回答1:


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.




回答2:


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
}



回答3:


I don't know if its really what you're looking for, of if LINQ is still "smiled upon" by .net digirati, but using LINQ to Sharepoint might work for you.



来源:https://stackoverflow.com/questions/5340746/custom-fields-only-from-sharepoint-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!