Check List, Content Type, Field Exist In SharePoint site using CSOM

Check List, Content Type, Field Exist using CSOM SharePoint

Check List, Content Type, Field Exist In SharePoint site using CSOM. In this post I describe you Check List, Content Type, Field Exist using CSOM SharePoint 2013. Basically, when we are going to create SharePoint List, Content Type, Field exist in SharePoint Site Collection and Content Types added on list or library. If the count is zero, the field doesn’t exist and if the count is not zero, the field exists.

If you don’t know to add references in your solutions then follow the post below

Add  references in Solution

[AdSense-A]

isExist1 = isExist(clientContext, ContentTypeName, "contenttype");
public static int isExist(ClientContext context, string fieldToCheck, string type)
        {
            var isExist = 0;
            ListCollection listCollection = context.Web.Lists;
            ContentTypeCollection cntCollection = context.Web.ContentTypes;
            FieldCollection fldCollection = context.Web.Fields;
            switch (type)
            {
                case "list":
                    context.Load(listCollection, lsts => lsts.Include(list => list.Title).Where(list => list.Title == fieldToCheck));
                    context.ExecuteQuery();
                    isExist = listCollection.Count;
                    break;
                case "contenttype":
                    context.Load(cntCollection, cntyp => cntyp.Include(ct => ct.Name).Where(ct => ct.Name == fieldToCheck));
                    context.ExecuteQuery();
                    isExist = cntCollection.Count;
                    break;
                case "contenttypeName":
                    context.Load(cntCollection, cntyp => cntyp.Include(ct => ct.Name, ct => ct.Id).Where(ct => ct.Name == fieldToCheck));
                    context.ExecuteQuery();
                    foreach (ContentType ct in cntCollection)
                    {

                        //  isExist= ct.Id;
                    }
                    break;
                case "field":
                    context.Load(fldCollection, fld => fld.Include(ft => ft.Title).Where(ft => ft.Title == fieldToCheck));
                    try
                    {
                        context.ExecuteQuery();
                        isExist = fldCollection.Count;
                    }
                    catch (Exception e)
                    {
                        if (e.Message == "Unknown Error")
                        {
                            isExist = fldCollection.Count;
                        }
                    }
                    break;
                case "listcntype":
                    List lst = context.Web.Lists.GetByTitle(fieldToCheck);
                    ContentTypeCollection lstcntype = lst.ContentTypes;
                    context.Load(lstcntype, lstc => lstc.Include(lc => lc.Name).Where(lc => lc.Name == fieldToCheck));
                    context.ExecuteQuery();
                    isExist = lstcntype.Count;
                    break;
            }
            return isExist;
        }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!