7

使用 SharePoint 2010 中的客户端对象模型 (C#),如何确定给定列表中是否存在指定的列(字段)名称?

谢谢,魔术师安迪。

4

9 回答 9

12

刚刚在搜索相同的东西时发现了这个,但看起来 Sharepoint 2010 内置了一些东西,至少对于服务器模型:list.Fields.ContainsField("fieldName");

不确定它是否存在于客户端。然而,认为这将是存储这些信息的好地方。

于 2012-03-19T15:47:35.023 回答
7

服务器对象模型

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")
    {
        -
        -
    }
}
于 2010-12-09T18:32:10.320 回答
5

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")){
       //...
    }
}
于 2014-10-02T21:01:54.187 回答
4

这是共享点列表的扩展代码 (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>)

请享用 :)

于 2015-12-29T10:10:30.430 回答
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; }
}
于 2010-12-10T16:33:16.473 回答
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");

您还可以在 Include 方法之后使用“Where”并检查返回的集合/字段是否为空。这是关于个人喜好的,因为这两个选项都是在客户端查询。

于 2016-07-22T12:35:58.317 回答
0

Mitya 扩展方法的简化版:

 public static bool FieldExists(this List list, string internalFieldname)
    {
        using (ClientContext clientContext = list.Context as ClientContext)
        {
            clientContext.Load(list.Fields, fCol => fCol.Include(
                    f => f.InternalName
                ).Where(field => field.InternalName == internalFieldname));
            clientContext.ExecuteQuery();

            return (list.Fields != null) && (list.Fields.Count > 0);
        }
    }

当您已经可以使用列表附带的上下文时,无需传入单独的客户端上下文参数。

于 2019-03-10T00:08:23.130 回答
0

我更喜欢 SharePoint Plus 库,因为它非常干净: http ://aymkdn.github.io/SharepointPlus/symbols/%24SP%28%29.list.html

$SP().list("My List").get({
  fields:"Title",
  where:"Author = '[Me]'"
},function getData(row) {
  console.log(row[0].getAttribute("Title"));
});

您可以设置一个 for 循环来循环遍历该行并检查您要查找的列是否存在。

于 2015-11-19T05:04:45.107 回答
-2

很多代码使用这个

先加载字段然后

 bool exists= clientContext2.Site.RootWeb.Fields.Any(o => o.Id.ToString() == a.Id.ToString());
于 2017-10-12T15:01:25.347 回答