1

I want to read an excel file & create items in main list and lookup list. The main list is creating an item, but it's not returning the ID synchronously. I tried Success callbacks & deferred, but nothing is working.

Method 1:

function CreateRoleRights(rowValue,listDtls,success, error)
    {
    var objRightsRoleList = clientContext.get_web().get_lists().getByTitle(listDtls[0]);
    var itemCreateInfo = new SP.ListItemCreationInformation();
    oRightsRoleListItem = objRightsRoleList.addItem(itemCreateInfo); 

    selectedUser = SP.FieldUserValue.fromUser("dns\\"+rowValue[0]);   

    oRightsRoleListItem.set_item(columnNamesInternal[0], selectedUser);   

    oRightsRoleListItem.set_item(columnNamesInternal[1], rowValue[3]);

    oRightsRoleListItem.set_item(columnNamesInternal[2], rowValue[4]);

    oRightsRoleListItem.set_item(columnNamesInternal[3], rowValue[8]);

    if(manager.length>0)

    {

    selectedUser = SP.FieldUserValue.fromUser(manager);  

    oRightsRoleListItem.set_item(columnNamesInternal[4], selectedUser); 

    }  

    oRightsRoleListItem.update();

    clientContext.load(oRightsRoleListItem);

    clientContext.executeQueryAsync(
        function () {
        itemId=oRightsRoleListItem.get_id();
          success(oRightsRoleListItem);
        },
      error);
};

Method 2:

function CreateRoleRightsListItems(rowValue,listDtls){

var dfd = $.Deferred(function() {
var objRightsRoleList = clientContext.get_web().get_lists().getByTitle(listDtls[0]);
var itemCreateInfo = new SP.ListItemCreationInformation();
var oRightsRoleListItem = objRightsRoleList.addItem(itemCreateInfo); 
selectedUser = SP.FieldUserValue.fromUser("dns\\"+rowValue[0]);   
oRightsRoleListItem.set_item(columnNamesInternal[0], selectedUser);   
oRightsRoleListItem.set_item(columnNamesInternal[1], rowValue[3]);
oRightsRoleListItem.set_item(columnNamesInternal[2], rowValue[4]);
oRightsRoleListItem.set_item(columnNamesInternal[3], rowValue[8]);
if(manager.length>0)
{
    selectedUser = SP.FieldUserValue.fromUser(manager);  
    oRightsRoleListItem.set_item(columnNamesInternal[   4], selectedUser); 
}  
oRightsRoleListItem.update();
clientContext.load(oRightsRoleListItem);
clientContext.executeQueryAsync(function() {
                // Successfull
                dfd.resolve(oRightsRoleListItem);      
            },
            function(sender, args) {
                // Failure
                dfd.reject(args.get_message());
            });
    });
    return dfd.promise();
};

0

Your Answer

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

Browse other questions tagged or ask your own question.