检查列表列是否存在使用SharePoint客户端对象模型?

问题描述 投票:7回答:9

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

谢谢,MagicAndi。

sharepoint sharepoint-2010 client-side csom
9个回答
12
投票

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

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


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

4
投票

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

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

4
投票

这是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
投票

我最终在操作之前检索列表字段的详细信息,并将它们保存在结构的通用列表中(包含每个字段的详细信息)。然后,我查询此(通用)列表,以查看当前字段是否实际存在于给定(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; }
}

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”,并检查返回的collection / field是否为null。这是关于个人偏好,因为这两个选项都是在客户端查询。


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循环以遍历该行,并检查您要查找的列是否存在。


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);
        }
    }

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


-2
投票

很多代码使用这个

首先加载字段

 bool exists= clientContext2.Site.RootWeb.Fields.Any(o => o.Id.ToString() == a.Id.ToString());
© www.soinside.com 2019 - 2024. All rights reserved.