Check if a List Column Exists using SharePoint Cli

2019-04-07 16:19发布

Using the Client Object Model (C#) in SharePoint 2010, how can I determine if a specified column (field) name exists in a given List?

Thanks, MagicAndi.

9条回答
唯我独甜
2楼-- · 2019-04-07 17:15

Server Object Model

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")
            {
                -
                -
            }
        }
    }
}

Client Object Model

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")
    {
        -
        -
    }
}
查看更多
做自己的国王
3楼-- · 2019-04-07 17:17

The following method demonstrates how to determine whether a specified column exists in a List using 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();
    }
}

Usage

using(var ctx = new ClientContext(webUrl))
{
    var list = ctx.Web.Lists.GetByTitle(listTitle);
    if(list.ContainsField("Title")){
       //...
    }
}
查看更多
等我变得足够好
4楼-- · 2019-04-07 17:19

I ended up retrieving the details of the list's fields prior to my operation, and saving them in a generic list of structs (containing details of each field). I then query this (generic) list to see if the current field actually exists in the given (SharePoint) list.

// 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; }
}
查看更多
登录 后发表回答