Hi all,
How can I check if a site column exists before creating a new one when using JavaScript and the object model in SP2013
I have the following code, but it always return something. The MSDN documentation doesn't provide any examples.
this.web = appContextSite.get_web(); webFields = this.web.get_fields(); context.load(webFields); context.executeQueryAsync( Function.createDelegate(this, fieldsLoaded), Function.createDelegate(this, errorHandler) ); function fieldsLoaded() { var oTestField = webFields.getByTitle("NewField"); //The below always return an object if (oTestField == null) { var fieldSchema = '<Field Type="Text" DisplayName="NewField" Name="NewField" />'; oTestField = webFields.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType); } $('#message').html('Fields found:<ul>' + listInfo + '</ul>'); } function errorHandler(sender, args) { $('#message').text("Could not complete cross-domain call: " + args.get_message()); }
Cheers