2010-12-09 46 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()); 
相關問題