SaguiItay

My blog has moved!

You should be automatically redirected in 4 seconds. If not, visit:
http://itaysagui.wordpress.com
and update your bookmarks.

Thursday, September 15, 2011

SharePoint Client Object Model: Adding a Site Column only if it does not already exist

Before creating a new Site Column, you should make sure that the column doesn't already exist. At the very least, make sure that the ID you are using isn't already taken. The sample below uses the FindField method from a previous post:

public static Field EnsureField(this ClientContext clientContext, Web web, Guid id, string name, string schema)
{
	var field = FindField(clientContext, web, f => f.Id == id);
	if (field != null)
		return field;

	var newField = web.Fields.AddFieldAsXml(schema, false,
		AddFieldOptions.AddToNoContentType | AddFieldOptions.AddFieldInternalNameHint);
	clientContext.ExecuteQuery();
	return newField;
}

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home