none
How to automatically sync from one web application list item to another web application list using powershell command / SharePoint designer Workflow in SharePoint 2013 RRS feed

  • Question

  • Hello,

                                     How to automatically sync from one web application list item to another web application list using powershell command / SharePoint designer Workflow in SharePoint 2013.

    Thanks

    Mukesh



    Thanks Mukesh

    Saturday, April 21, 2018 2:15 PM

Answers

  • Hi Mukesh,

    Here's full test powershell script for sync the items between 2 webapplications , 

    # Setup the correct modules for SharePoint Manipulation 
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 

       Add-PsSnapin Microsoft.SharePoint.PowerShell 
    }

    $sc1URL="http://spwf:5001" # site collection in the 1st webapplication

    $sc2URL="http://spwf:5004" # site collection in the 2nd webapplication

    $listTitleInSC1="CustomList2";

    $listTitleInSC2="CustomList1";

    $CounterLocation="C:\Sync\Counter.txt";

    $SC1=Get-SPWeb -Site $sc1URL  # get refernce to the first site collection

    $SC2=Get-SPWeb -Site $sc2URL    # get refernce to the second site collection

    $List1= $SC1.Lists.TryGetList("CustomList-2")
    $List2= $SC2.Lists.TryGetList("CustomList-1")
    $Counter=Get-Content $CounterLocation

    #if($Counter -eq 0 -or $Counter -eq $null)
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $camlQuery =  "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>"
    $spQuery.RowLimit=1;
    $spQuery.Query=$camlQuery;
    $splListItems = $List1.GetItems($spQuery) 
    if($splListItems.Count -gt 0)
    {
     $lastItem=$splListItems[0];
     $lastID=$lastItem.ID;

     $ItemDifference=$lastID - $Counter;

     for($i=[int]$Counter+1;$i-le $lastID ; $i++)
     {
      $ItemToBeCopied=$List1.GetItemById($i);
      $NewItemInDestinationWebpApp=$List2.AddItem();
      $NewItemInDestinationWebpApp["Title"]=$ItemToBeCopied["Title"];
      $NewItemInDestinationWebpApp.Update();
     }
     Set-Content -Value $lastID -Path $CounterLocation
     #Add-Content -Value $lastID -Path $CounterLocation -

      Write-Host "ID is:" $ItemDifference 
    }
    else
    {
      Write-Host "No items exists"
    }


    As you see i add the latest item id to the text file at  $CounterLocation="C:\Sync\Counter.txt" to track the last id , then i get the new items added after that record . you create a folder and create a text file and don't forget to add 0 as initial value.

    Here's the result

    if you want to automate the running of this powershell script, then you need to create a schedule in windows task , have a look on below article 

    How to: Run PowerShell Scripts from Task Scheduler
    https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6




    Sunday, April 22, 2018 10:50 PM

All replies

  • Hi Mukesh,

    there are more than an option for sync the list items between web applications, let me display them

    1- You can create a workflow using sharepoint designer for sync list items automatically between 2 web applications,  have a look on the following articles which explain the solution step by step .

    Note : the article have a link to codeplex for downloading custom wf activity which responsible for sync items between web application, i've added the codeplex links below also

    Copy List Items between webapplications

    Useful Sharepoint Designer Custom Workflow Activities
    https://archive.codeplex.com/?p=spdactivities
    https://archive.codeplex.com/?p=spdactivities

    2- Develop custom event receiver and attach it on additing/added list items , here's an example

    public static void CopyListFolderToAnotherSiteCollection(string sourceUrl, string destinationUrl, 
              string listName, string uniqueFolderID, string newFolderUrl) 
          { 
              SPListItem newItem = null;
    
              using (SPSite sourceSite = new SPSite(sourceUrl)) 
              { 
                  using (SPWeb sourceWeb = sourceSite.OpenWeb()) 
                  {
    
                      SPList sourceList = sourceWeb.Lists[listName]; 
                      SPFolder sourceFolder = sourceWeb.GetFolder(new Guid(uniqueFolderID)); 
                      SPQuery q = new SPQuery(); 
                      q.Folder = sourceFolder; 
                      SPListItemCollection sourceItems = sourceList.GetItems(q); 
                      using (SPSite destSite = new SPSite(destinationUrl)) 
                      { 
                          using (SPWeb destWeb = destSite.OpenWeb()) 
                          { 
                              SPList destList = destWeb.Lists[listName];
    
                              SPListItem destItem = destList.Items. 
                                  Add(string.Empty, SPFileSystemObjectType.Folder, newFolderUrl);
    
                              destItem.Update(); 
                              SPFolder destFolder = destWeb.GetFolder(destItem.UniqueId);
    
                              List<SPListItem> listItems = (from l in sourceItems.OfType<SPListItem>() 
                                             where l[SPBuiltInFieldId.ContentType].ToString() != "Folder" select l).ToList();
    
                              List<SPListItem> folderItems = (from l in sourceItems.OfType<SPListItem>() 
                                                            where l[SPBuiltInFieldId.ContentType].ToString() == "Folder" 
                                                            select l).ToList();
    
                              foreach (SPListItem sourceItem in listItems) 
                              { 
                                  newItem = destList.Items.Add(destFolder.ServerRelativeUrl, 
                                                                                      SPFileSystemObjectType.File,null); 
                                  newItem["Title"] = sourceItem["Title"]; 
                                  newItem[SPBuiltInFieldId.ContentType] = sourceItem[SPBuiltInFieldId.ContentType]; 
                                  newItem.Update(); 
                              }
    
                              foreach (SPListItem f in folderItems) 
                              { 
                                  SPFolder folder = f.Folder; 
                                  int index = folder.ServerRelativeUrl.IndexOf(sourceList.RootFolder.Url); 
                                  string folderUrl = folder.ServerRelativeUrl.Substring(index + sourceList.RootFolder.Url.Length + 1); 
                                  CopyListFolderToAnotherSiteCollection(sourceUrl, destinationUrl, 
                                                               listName, folder.UniqueId.ToString(), folderUrl); 
                              }
    
                          } 
                      } 
                  } 
              } 
          }

    here's the related article

    http://sharepointfieldnotes.blogspot.com/2009/11/how-to-copy-list-items-across-site.html

    here's also  a similar thread 

    https://social.msdn.microsoft.com/Forums/sharepoint/en-US/1d93c277-dde5-4198-9365-c916ea66ac13/copy-list-items-to-another-web-applicationautomatically?forum=sharepointdevelopmentlegacy


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6

    Saturday, April 21, 2018 4:35 PM
  • Hello Ahmed,

                       Can't be possible through powershell command to full fill this scenario. Please suggest me . if possible than suggest link to full fill this requirements.

    Thanks

    Mukesh


    Thanks Mukesh

    Sunday, April 22, 2018 9:27 AM
  • Hi Mukesh,

    Here's full test powershell script for sync the items between 2 webapplications , 

    # Setup the correct modules for SharePoint Manipulation 
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 

       Add-PsSnapin Microsoft.SharePoint.PowerShell 
    }

    $sc1URL="http://spwf:5001" # site collection in the 1st webapplication

    $sc2URL="http://spwf:5004" # site collection in the 2nd webapplication

    $listTitleInSC1="CustomList2";

    $listTitleInSC2="CustomList1";

    $CounterLocation="C:\Sync\Counter.txt";

    $SC1=Get-SPWeb -Site $sc1URL  # get refernce to the first site collection

    $SC2=Get-SPWeb -Site $sc2URL    # get refernce to the second site collection

    $List1= $SC1.Lists.TryGetList("CustomList-2")
    $List2= $SC2.Lists.TryGetList("CustomList-1")
    $Counter=Get-Content $CounterLocation

    #if($Counter -eq 0 -or $Counter -eq $null)
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $camlQuery =  "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>"
    $spQuery.RowLimit=1;
    $spQuery.Query=$camlQuery;
    $splListItems = $List1.GetItems($spQuery) 
    if($splListItems.Count -gt 0)
    {
     $lastItem=$splListItems[0];
     $lastID=$lastItem.ID;

     $ItemDifference=$lastID - $Counter;

     for($i=[int]$Counter+1;$i-le $lastID ; $i++)
     {
      $ItemToBeCopied=$List1.GetItemById($i);
      $NewItemInDestinationWebpApp=$List2.AddItem();
      $NewItemInDestinationWebpApp["Title"]=$ItemToBeCopied["Title"];
      $NewItemInDestinationWebpApp.Update();
     }
     Set-Content -Value $lastID -Path $CounterLocation
     #Add-Content -Value $lastID -Path $CounterLocation -

      Write-Host "ID is:" $ItemDifference 
    }
    else
    {
      Write-Host "No items exists"
    }


    As you see i add the latest item id to the text file at  $CounterLocation="C:\Sync\Counter.txt" to track the last id , then i get the new items added after that record . you create a folder and create a text file and don't forget to add 0 as initial value.

    Here's the result

    if you want to automate the running of this powershell script, then you need to create a schedule in windows task , have a look on below article 

    How to: Run PowerShell Scripts from Task Scheduler
    https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6




    Sunday, April 22, 2018 10:50 PM
  • Hello Ahmed,

                      Thanks. can be possible same scenario second webapplication listitem to first webapplication list item using powershell command sync the data automatically.

    Note - one web application list item to second web application list and second web application list item to First Web application list using powershell command sync automatically. Please suggest me. 

    Thanks

    Mukesh


    Thanks Mukesh

    Tuesday, April 24, 2018 4:10 AM
  • Hi Mukesh_SharePoint,

    i just created PowerShell function in  Script, then i passed the 2 web application, text file location , and list url. then in the 2nd call, io swapped the variables

    SyncListItems($firstWebAppURL,$SecondWebAppURL,$listTitleInSC1,$listTitleInSC2,$CounterLocation1)
    
    SyncListItems($SecondWebAppURL,$firstWebAppURL,$listTitleInSC2,$listTitleInSC1,$CounterLocation2)

    Here's full script

    # Setup the correct modules for SharePoint Manipulation 
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
    { 
       Add-PsSnapin Microsoft.SharePoint.PowerShell 
    }
    function SyncListItems($firstWebAppURL,$SecondWebAppURL,$listTitleInSC1,$listTitleInSC2,$CounterLocation)
    {
    
    $SC1=Get-SPWeb -Site $firstWebAppURL  # get refernce to the first site collection
    
    $SC2=Get-SPWeb -Site $SecondWebAppURL    # get refernce to the second site collection
    
    $List1= $SC1.Lists.TryGetList($listTitleInSC1)
    $List2= $SC2.Lists.TryGetList($listTitleInSC2)
    $Counter=Get-Content $CounterLocation
    
    #if($Counter -eq 0 -or $Counter -eq $null)
    $spQuery = New-Object Microsoft.SharePoint.SPQuery
    $camlQuery =  "<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>"
    $spQuery.RowLimit=1;
    $spQuery.Query=$camlQuery;
    $splListItems = $List1.GetItems($spQuery) 
    if($splListItems.Count -gt 0)
    {
     $lastItem=$splListItems[0];
     $lastID=$lastItem.ID;
    
     $ItemDifference=$lastID - $Counter;
    
     for($i=[int]$Counter+1;$i-le $lastID ; $i++)
     {
      $ItemToBeCopied=$List1.GetItemById($i);
      $NewItemInDestinationWebpApp=$List2.AddItem();
      $NewItemInDestinationWebpApp["Title"]=$ItemToBeCopied["Title"];
      $NewItemInDestinationWebpApp.Update();
     }
     Set-Content -Value $lastID -Path $CounterLocation
     #Add-Content -Value $lastID -Path $CounterLocation -
    
      Write-Host "ID is:" $ItemDifference 
    }
    else
    {
      Write-Host "No items exists"
    }
    }
    
    
    $firstWebAppURL="http://spwf:5001";
    
    $SecondWebAppURL="http://spwf:5004";
    
    $listTitleInSC1="CustomList2";
    
    $listTitleInSC2="CustomList1";
    
    $CounterLocation1="C:\Sync\Counter.txt";
    
    $CounterLocation2="C:\Sync\Counter.txt";
    
    SyncListItems($firstWebAppURL,$SecondWebAppURL,$listTitleInSC1,$listTitleInSC2,$CounterLocation1)
    
    SyncListItems($SecondWebAppURL,$firstWebAppURL,$listTitleInSC2,$listTitleInSC1,$CounterLocation2)


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6


    Wednesday, April 25, 2018 7:14 AM
  • Hi Ahmed,

               How to automatically sync from one web application library folder and sub folder item to another web application library folder and sub folder using powershell command in SharePoint 2010. 

    Note:- Folder/SubFolder Content Size more than 1 TB.

    Thanks

    Mukesh


    Thanks Mukesh

    Friday, August 31, 2018 7:15 AM