1

Is there a way to delete a site column (and remove it from all according content types) via Javascript? I found some CSOM scripts and tried to approximate the JSOM method but it didn't work out. The methods seem to be slightly different.

-thanks a lot

deleteSiteColumn = function(columnSTR) {
    myList.init();      
    var fieldOBJ = myList.webContextOBJ.get_fields().getByInternalNameOrTitle(columnSTR);
    myList.clientContextOBJ.load(fieldOBJ);
    myList.clientContextOBJ.executeQueryAsync(function() {
        fieldOBJ.deleteObject();
        myList.webContextOBJ.update();
        myList.clientContextOBJ.executeQueryAsync(function() {
            console.log("Site Column '" + columnSTR + "' deleted");
        }, function(senderOBJ, argsOBJ) {
            console.log('Failed: ' + argsOBJ.get_message());
        });     
    }, function(senderOBJ, argsOBJ) {
        console.log('Failed: ' + argsOBJ.get_message());
    });     
}
2
  • Can you post your code that you have tried already Commented Oct 27, 2016 at 8:47
  • Sorry, I actually hadn't much code...
    – Dave
    Commented Oct 27, 2016 at 9:37

1 Answer 1

3

You have not posted your existing code!! It is always welcome if you post what you have tried so far. Anyway find my code for deleting site column

var clientContext = new SP.ClientContext();
var web = clientContext.get_web();
var field = web.get_fields().getByTitle('{Display Name of Site Column}');
clientContext.load(field);

clientContext.executeQueryAsync(function() {
    field.deleteObject();
    web.update();
    clientContext.executeQueryAsync(function() {
        console.log("Site Column deleted");
    }, function() {
        // handle error
    });


}, function() {
    // handle error
});

PS: If your site column is being used in any content types then you will get an error.

7
  • It's not working... He executes the queries and logs the "deleted" message, but the columns are still there (they are not in use, neither in a list nor in a content type). (I posted my method, based on your code)
    – Dave
    Commented Oct 27, 2016 at 9:36
  • Just tested my code again! It works. Can you test my code in console? Commented Oct 27, 2016 at 9:39
  • Hmmm mine to if I execute the funtion seperatly... But if I use it in a loop, to delete several columns at once it won't.
    – Dave
    Commented Oct 27, 2016 at 9:46
  • in loop approach is little different but it must work. Show your code how you are trying Commented Oct 27, 2016 at 9:51
  • oh got the error... I used a global ClientContext. They proberbly overwrote each other...
    – Dave
    Commented Oct 27, 2016 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.