0

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.

  • Is there any update? Don't forget to accept the helpful answer as the best answer if it resolves your problem. – JoannaW_MSFT Oct 12 '18 at 6:24
0

You are creating duplicate lookup fields in your code:

var lookupField = fieldColl.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType) ; 
fieldColl.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType) ;   

Remove the second row, only add one field:

var lookupField = fieldColl.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType) ; 

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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