2010-12-09 87 views

回答

8

服务器对象模型

string siteUrl = "http://mysite"; 
using (SPSite site = new SPSite(siteUrl)) 
{ 
    using (SPWeb web = site.OpenWeb()) 
    { 
     SPList list = web.Lists["my forum"]; 
     for (int i = 0; i < list.Fields.Count; i++) 
     { 
      if (list.Fields[i].Title == "xyz") 
      { 
       - 
       - 
      } 
     } 
    } 
} 

客户端对象模型

string siteUrl = "http://MyServer/sites/MySiteCollection"; 
ClientContext clientContext = new ClientContext(siteUrl); 
SP.List List = clientContext.Web.Lists.GetByTitle("my forum"); 
for (int i = 0; i < list.Fields.Count; i++) 
{ 
    if (list.Fields[i].Title == "xyz") 
    { 
     - 
     - 
    } 
} 
+0

这是你的榜样 – axk 2010-12-09 19:16:12

2

我结束了检索之前,我的操作列表中的字段的详细信息,并保存在结构中的泛型列表(含每个领域的细节)。然后,我查询此(通用)列表以查看当前字段是否确实存在于给定(SharePoint)列表中。

// Retrieve detail sof all fields in specified list 
using (ClientContext clientContext = new ClientContext(SharePointSiteUrl)) 
{ 
    List list = clientContext.Web.Lists.GetByTitle(listName); 
    _listFieldDetails = new List<SPFieldDetails>(); 

    // get fields name and their types 
    ClientObjectPrototype allFields = list.Fields.RetrieveItems(); 
    allFields.Retrieve(FieldPropertyNames.Title, 
         FieldPropertyNames.InternalName, 
         FieldPropertyNames.FieldTypeKind, 
         FieldPropertyNames.Id, 
         FieldPropertyNames.ReadOnlyField); 
    clientContext.ExecuteQuery(); 

    foreach (Field field in list.Fields) 
    { 
     SPFieldDetails fieldDetails = new SPFieldDetails(); 
     fieldDetails.Title = field.Title; 
     fieldDetails.InternalName = field.InternalName; 
     fieldDetails.Type = field.FieldTypeKind; 
     fieldDetails.ID = field.Id; 
     fieldDetails.ReadOnly = field.ReadOnlyField; 
     listFieldDetails.Add(fieldDetails); 
    } 
} 

// Check if field name exists 
_listFieldDetails.Exists(field => field.Title == fieldName); 

// Struct to hold details of the field 
public struct SPFieldDetails 
{ 
    public string Title { get; set; } 
    public string InternalName { get; set; } 
    public Guid ID { get; set; } 
    public FieldType Type { get; set; } 
    public bool ReadOnly { get; set; } 
} 
11

刚刚发现这一点的同时寻找同样的事情,但它看起来像SharePoint 2010中有一些建在此,至少在服务器型号:list.Fields.ContainsField("fieldName");

不知道是否存在客户端虽然。想象一下,这将是一个存储这些信息的好地方。

3

下面的方法演示如何确定是否使用List存在指定列CSOM

static class FieldCollectionExtensions 
{ 
    public static bool ContainsField(this List list,string fieldName) 
    { 
     var ctx = list.Context; 
     var result = ctx.LoadQuery(list.Fields.Where(f => f.InternalName == fieldName)); 
     ctx.ExecuteQuery(); 
     return result.Any(); 
    } 
} 

使用

using(var ctx = new ClientContext(webUrl)) 
{ 
    var list = ctx.Web.Lists.GetByTitle(listTitle); 
    if(list.ContainsField("Title")){ 
     //... 
    } 
} 
3

下面是SharePoint列表的扩展码(CSOM)

public static bool DoesFieldExist(this List list, ClientContext clientContext, string internalFieldname) 
    { 
     bool exists = false; 

     clientContext.Load(list.Fields, fCol => fCol.Include(
       f => f.InternalName 
      ).Where(field => field.InternalName == internalFieldname)); 
     clientContext.ExecuteQuery(); 

     if (list.Fields != null && list.Fields.Count > 0) 
     { 
      exists = true; 
     } 

     return exists; 
    } 

使用

List targetList = this.Context.Web.Lists.GetById(<ListID>); 
targetList.DoesFieldExist(<ClientContext>, <Field internal Name>) 

享受:)

2

上面有一些很好的答案。我个人使用过这个:

  List list = ctx.Web.Lists.GetByTitle("Some list"); 
      FieldCollection fields = list.Fields; 
      IEnumerable<Field> fieldsColl = ctx.LoadQuery(fields.Include(f => f.InternalName)); 
      ctx.ExecuteQuery(); 

      bool fieldMissing = fieldsColl.Any(f => f.InternalName != "Internal_Name"); 

你也可以使用'Where'后的Include方法并检查返回的collection/field是否为null。这是关于个人偏好的,因为这两个选项都是在客户端查询的。

-2

到太多的代码使用这种

负载场第一则

bool exists= clientContext2.Site.RootWeb.Fields.Any(o => o.Id.ToString() == a.Id.ToString());