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条回答
【Aperson】
2楼-- · 2019-04-07 16:56

Some good answers above. I personally used this one:

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

You can also use 'Where' after Include method and check if returned collection/field is null. It's about personal preference, because both options are querying on client side.

查看更多
三岁会撩人
3楼-- · 2019-04-07 17:01

Here's an extension code (CSOM) for sharepoint list

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

usage

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

enjoy :)

查看更多
淡お忘
4楼-- · 2019-04-07 17:01

I prefer the SharePoint Plus Library as it is really clean: 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"));
});

You could setup a for loop to loop through the row and check if the column you're looking for exists.

查看更多
Explosion°爆炸
5楼-- · 2019-04-07 17:07

Just found this while searching for the same thing, but it looks like Sharepoint 2010 has something built in for this, at least for the Server model: list.Fields.ContainsField("fieldName");

Not sure if it exists for Client side though. Figured it would be a good place to store this information however.

查看更多
Summer. ? 凉城
6楼-- · 2019-04-07 17:07

A cut down and simplified version of Mitya's extension method:

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

There's no need to pass in a separate client context parameter when you can already use the context that comes in with the list.

查看更多
手持菜刀,她持情操
7楼-- · 2019-04-07 17:10

to much code use this

load Fields first then

 bool exists= clientContext2.Site.RootWeb.Fields.Any(o => o.Id.ToString() == a.Id.ToString());
查看更多
登录 后发表回答