@@ -447,12 +447,12 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
447447 int num1 = 0 ; // Last ID in source list
448448 int num2 = 0 ; // Last ID in destination list
449449
450- string [ , ] actualFields ;
450+ List < ( string Source , string Dest ) > actualFields ;
451451 try
452452 {
453453 // Get the field mappings between the source and destination lists
454454 actualFields = GetActualFields ( sourceList , destList ) ;
455- Logger . Log ( 1 , "DEBUG: Fields obtained" ) ;
455+ Logger . Log ( 1 , "DEBUG: Fields obtained - " + actualFields . Count + " mappings ") ;
456456 }
457457 catch ( Exception ex )
458458 {
@@ -461,13 +461,19 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
461461 return ;
462462 }
463463
464+ if ( actualFields . Count == 0 )
465+ {
466+ Logger . Log ( 2 , "No matching fields found between source and destination lists. Skipping sync." ) ;
467+ return ;
468+ }
469+
464470 try
465471 {
466472 // Determine the last ID in the source list
467- if ( ( uint ) sourceList . ItemCollection . Count > 0U )
473+ if ( sourceList . ItemCollection is { Count : > 0 } )
468474 num1 = ( int ) sourceList . ItemCollection [ sourceList . ItemCollection . Count - 1 ] [ "ID" ] ;
469475 // Determine the last ID in the destination list
470- if ( ( uint ) destList . ItemCollection . Count > 0U )
476+ if ( destList . ItemCollection is { Count : > 0 } )
471477 num2 = ( int ) destList . ItemCollection [ destList . ItemCollection . Count - 1 ] [ "ID" ] ;
472478 }
473479 catch ( Exception ex )
@@ -507,15 +513,15 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
507513 {
508514 int id = ( int ) sourceList . ItemCollection [ index1 ] [ "ID" ] ;
509515 // Copy field values from source to destination based on the field mappings
510- for ( int index2 = 0 ; index2 < actualFields . Length / 2 ; ++ index2 )
516+ foreach ( var ( destField , sourceField ) in actualFields )
511517 {
512518 try
513519 {
514- destList . ItemCollection . GetById ( id ) [ actualFields [ index2 , 0 ] ] = sourceList . ItemCollection [ index1 ] [ actualFields [ index2 , 1 ] ] ;
520+ destList . ItemCollection . GetById ( id ) [ destField ] = sourceList . ItemCollection [ index1 ] [ sourceField ] ;
515521 }
516522 catch ( Exception ex )
517523 {
518- Console . WriteLine ( "ERROR: Failed to copy field '" + actualFields [ index2 , 1 ] + "' to '" + actualFields [ index2 , 0 ] + "' for item ID " + id + "." ) ;
524+ Console . WriteLine ( "ERROR: Failed to copy field '" + sourceField + "' to '" + destField + "' for item ID " + id + "." ) ;
519525 Console . WriteLine ( "Exception: " + ex . Message ) ;
520526 }
521527 }
@@ -557,7 +563,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
557563 }
558564
559565 // Helper method to create a mapping of fields between two lists based on their titles
560- private static string [ , ] GetActualFields ( SPOList listone , SPOList listtwo )
566+ private static List < ( string Source , string Dest ) > GetActualFields ( SPOList listone , SPOList listtwo )
561567 {
562568 Logger . Log ( 1 , "DEBUG: Entering GetActualFields" ) ;
563569 try
@@ -566,33 +572,30 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
566572 List < Field > fields1 = GetFields ( listone ) ;
567573 List < Field > fields2 = GetFields ( listtwo ) ;
568574
569- // Create a 2D array to store the field mappings (internal names)
570- string [ , ] strArray = new string [ fields1 . Count , 2 ] ;
571- int index1 = 0 ;
572- int index2 = 0 ;
575+ // Build a dictionary from fields2 for O(1) lookups by title
576+ var fields2ByTitle = new Dictionary < string , Field > ( StringComparer . OrdinalIgnoreCase ) ;
577+ foreach ( var f in fields2 )
578+ if ( ! string . IsNullOrEmpty ( f . Title ) )
579+ fields2ByTitle [ f . Title ] = f ;
573580
581+ // Match fields from listone to listtwo by title
582+ var mappings = new List < ( string Source , string Dest ) > ( fields1 . Count ) ;
574583 foreach ( Field field1 in fields1 )
575584 {
576- bool found = false ;
577- while ( index2 < fields2 . Count )
585+ if ( string . IsNullOrEmpty ( field1 . Title ) ) continue ;
586+
587+ if ( fields2ByTitle . TryGetValue ( field1 . Title , out var field2 ) )
578588 {
579- Field field2 = fields2 [ index2 ] ;
580- if ( field1 . Title == field2 . Title )
581- {
582- strArray [ index1 , 0 ] = field2 . InternalName ;
583- strArray [ index1 , 1 ] = field1 . InternalName ;
584- found = true ;
585- Logger . Log ( 1 , "DEBUG: Match found - " + field1 . Title ) ;
586- }
587- ++ index2 ;
588- if ( found ) break ;
589+ mappings . Add ( ( field2 . InternalName , field1 . InternalName ) ) ;
590+ Logger . Log ( 1 , "DEBUG: Match found - " + field1 . Title ) ;
591+ }
592+ else
593+ {
594+ Logger . Log ( 1 , "DEBUG: No match for field - " + field1 . Title + " (skipping)" ) ;
589595 }
590-
591- ++ index1 ;
592- index2 = 0 ;
593596 }
594597
595- return strArray ; // Return the field mappings
598+ return mappings ;
596599 }
597600 catch ( Exception ex )
598601 {
@@ -606,7 +609,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
606609 private static List < Field > GetFields ( SPOList list )
607610 {
608611 Logger . Log ( 1 , "DEBUG: Entering GetFields" ) ;
609- List < Field > fieldList = new List < Field > ( ) ;
612+ var fieldList = new List < Field > ( ) ;
610613 try
611614 {
612615 // Iterate through all fields in the list
@@ -627,7 +630,7 @@ private static List<Field> GetFields(SPOList list)
627630 throw ;
628631 }
629632
630- return fieldList ; // Return the list of fields
633+ return fieldList ;
631634 }
632635 }
633636}
0 commit comments