Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to check if a site column exist using Javascript

$
0
0

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


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>