in my project, there is a picture library with a look field. I delete and create this lookup field like that :
function MajBibliPhotoGare() { var clientContext =SP.ClientContext.get_current(); var field = clientContext.get_web().get_lists().getByTitle('BibliPhotoGare').get_fields().getByInternalNameOrTitle('IDListeGare'); clientContext.load(field); clientContext.executeQueryAsync( Function.createDelegate(this,function() { // effacer IDListeGare field.deleteObject(); clientContext.get_web().update(); clientContext.executeQueryAsync( Function.createDelegate(this,function() { // Create lookup Field IDListeGare var oList = clientContext.get_web().get_lists().getByTitle('BibliPhotoGare'); var fieldColl = oList.get_fields(); var fieldSchema=''; //Add Look up field to the Field Collection var lookupField = fieldColl.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType) ; fieldColl.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType) ; lookupField.update(); clientContext.executeQueryAsync( // Field OK Function.createDelegate(this,function() { // Field OK alert('ok'); }), Function.createDelegate(this, function() { // Field not OK alert(' Not ok'); }) ); }), Function.createDelegate(this, function() { // error deleting lookup field alert('error deleting lookup field IDListeGare') }) ); }), Function.createDelegate(this,function() { // error loading lookup field alert('error loading lookup field IDListeGare') }) ); }
The code is running well, and when i look at the picture library, there is two lookup field named IDListeGare BibliPhotoGare picture Library result
Thank you fo your help.