Check the field/content type/list already exist in SharePoint site using c# [CSOM client]

As i working on creating content type, site columns and lists there is some field name is always conflict with my desired column name so here is a helper function to get over with that nasty exceptions. The model for all the field remain same except the collection like List,Content type, Site Column collection, I made it simple by return back the field count,
1. If count is zero, field doesn’t exist
2. if cont is not zero, field exist
I hope this may helpful for some,

        public dynamic isExist_Helper(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 = & gt; lsts.Include(list = & gt; list.Title).Where(list = & gt; list.Title == fieldToCheck));
        			context.ExecuteQuery();
        			isExist = listCollection.Count;
        			break;
        		case "contenttype":
        			context.Load(cntCollection, cntyp = & gt; cntyp.Include(ct = & gt; ct.Name).Where(ct = & gt; ct.Name == fieldToCheck));
        			context.ExecuteQuery();
        			isExist = cntCollection.Count;
        			break;
        		case "contenttypeName":
        			context.Load(cntCollection, cntyp = & gt; cntyp.Include(ct = & gt; ct.Name, ct = & gt; ct.Id).Where(ct = & gt; ct.Name == fieldToCheck));
        			context.ExecuteQuery();
        			foreach(ContentType ct in cntCollection) {
        				return ct.Id.ToString();
        			}
        			break;
        		case "field":
        			context.Load(fldCollection, fld = & gt; fld.Include(ft = & gt; ft.Title).Where(ft = & gt; 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 = & gt; lstc.Include(lc = & gt; lc.Name).Where(lc = & gt; lc.Name == fieldToCheck));
        			context.ExecuteQuery();
        			isExist = lstcntype.Count;
        			break;
        	}
        	return isExist;
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s