title: How to get data from Sharepoint list(s) and then insert it into SQL table using C# console app
body: <p>Background:</p>
  
  <p>I am doing all this with SSOM (Sever side object model) in C# console app.
  And I have a connection to and empty database which will be initialy populated and than updated as needed.</p>
  
  <p>On SharePoint server there are a couple lists among which there is a "List Of Headers" that contains the name of all other lists. Other lists are catalogs of articles.</p>
  
  <p>Headers List contains around 40 fields or so (Title, TypeOfCatalog, CatalogName, CatalogID,... so on)</p>
  
  <p>Other Catalog Lists contain around 300 fields (CatalogId, ArticlePrice, ArticleName,... and so on)</p>
  
  <p>Here's the code from the class i use:</p>
  
  <pre><code> public class SharePointProduction
   {
      private static XYZ_Entity _context = new XYZ_Entity();
  
      public static void Main()
      {           
  
          string siteURL = "http://somesite.local/sites/promo_tool";          
  
          try
          {
              using (SPSite site = new SPSite(siteURL))
              {
                  using (SPWeb web = site.OpenWeb())
                  {          
  
                      GetSharePointCatalogHeaders(web);
  
                      Console.WriteLine("Press any key to continue...");
  
                  }
              }
          }
          catch (Exception)
          {
              PrintErrorSharepointConnection();
              PrintPressKeyToExit();
          }
          finally
          {
              Console.ReadKey();
          }
      }
  </code></pre>
  
  <p>GetSharePointCatalogHeaders METHOD -----------------</p>
  
  <pre><code>private static SPListItemCollection GetSharePointCatalogHeaders(SPWeb web)
      {
  
          var catalogHeaderList = web.Lists.TryGetList("Headers List");
  
          if (catalogHeaderList == null) throw new Exception("Unable to get catalog headers used for checking status of catalogs");
  
          var catalogHeaderItems = catalogHeaderList.GetItems("Id", "_x0160_ifra_x0020_artikla", "Naziv_x0020_artikla", "Katalog_x002f_ID", "Status_x0020_kataloga", "Datum_x0020_po_x010d_etka_x0020_");
  
          return catalogHeaderItems;
      }
  </code></pre>
  
  <p>So far, that's what I got.</p>
  
  <p>I need to update Headers List in one table in database called "Headers", and other lists (Article Catalogs) to a "Catalogs" table.</p>
  
  <p>To be clearer (I do apologize, English is my second language):</p>