3

I have the same list in two sites, one in Site1 and other in Site2.

The list items are copied form site1 to site2 list and during this copy one column (RelationShipCol) has the same value as Site1 list to identify that row exists in both lists.

Has anyone written a JSOM for comparing two lists and find out if an item exists in Site1 List but is not available in Site2 list?

This has to run for all items in List from Site1 and find it any missing items in Site2?

Any help on JSOM as this cant be C# code as planning for SharePoint online.

1 Answer 1

1

The following example demonstrates how to compare list items between two lists

function getListItems(url,listTitle,success,error)
{
   var ctx = new SP.ClientContext(url);
   var list = ctx.get_web().get_lists().getByTitle(listTitle);
   var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
   ctx.load(items);
   ctx.executeQueryAsync(function() {
       success(items);
   },error);
}


function containsAny(items,propertyName,value)
{
    var result = items.filter(function(item){ return item.get_item(propertyName) == value });   
    return (result.length > 0);  
}    


function compareListItems(settings,success,error)
{
    getListItems(settings.Source.Url,settings.Source.Title,
      function(sourceItems)
      {
          getListItems(settings.Target.Url,settings.Target.Title,
            function(targetItems)
            {
                 var result = sourceItems.get_data().filter(function(sourceItem){
                       if(!containsAny(targetItems.get_data(),settings.CompareKey,sourceItem.get_item(settings.CompareKey)))
                             return sourceItem;
                 });         
                success(result);
            },
            error);     
      },
      error);
}   

Usage

Compare list items between two Tasks lists by Title

In your case you could specify RelationShipCol column

var settings = {
      'Source': {
         'Url': 'https://contoso.sharepoint.com/sites/news',
         'Title': 'Tasks'
      },
      'Target': {
            'Url': 'https://contoso.sharepoint.com/sites/openpositions',
            'Title': 'Tasks'
      },
      'CompareKey': 'Title'       
    };

compareListItems(settings,
   function(data)
   {
       if(data.length == 0)
           console.log('Source and target lists are identical');
       else {
           console.log('The target lists does not contain the following items:');
           data.forEach(function(item){
              console.log(item.get_item('Title'));   
           });
       }     
   }, 
   function(sender,args){
      console.log(args.get_message());
   });

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.