Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.3 kB
0
Indexable
Never
using System;
using System.Data.SqlClient;
using Microsoft.SharePoint.Client;

class Program
{
    static void Main()
    {
        // Connect to SharePoint
        using (ClientContext context = new ClientContext("Your SharePoint Site URL"))
        {
            // Specify your SharePoint lists
            List list1 = context.Web.Lists.GetByTitle("List1");
            List list2 = context.Web.Lists.GetByTitle("List2");
            List list3 = context.Web.Lists.GetByTitle("List3");
            List list4 = context.Web.Lists.GetByTitle("List4");

            // Connect to the database
            using (SqlConnection connection = new SqlConnection("YourConnectionString"))
            {
                connection.Open();

                // Fetch records from the first table
                using (SqlCommand command1 = new SqlCommand("SELECT * FROM Table1", connection))
                {
                    using (SqlDataReader reader1 = command1.ExecuteReader())
                    {
                        InsertRecordsIntoSharePoint(reader1, list1, context);
                    }
                }

                // Fetch records from the second table
                using (SqlCommand command2 = new SqlCommand("SELECT * FROM Table2", connection))
                {
                    using (SqlDataReader reader2 = command2.ExecuteReader())
                    {
                        InsertRecordsIntoSharePoint(reader2, list2, context);
                    }
                }

                // Repeat for Table3 and Table4
            }
        }

        Console.WriteLine("Data transfer completed.");
    }

    static void InsertRecordsIntoSharePoint(SqlDataReader reader, List list, ClientContext context)
    {
        while (reader.Read())
        {
            // Create a new SharePoint list item
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem newItem = list.AddItem(itemCreateInfo);

            // Set field values for the SharePoint list item
            newItem["Field1"] = reader["Column1"].ToString();
            // Set values for other fields

            // Update the SharePoint list
            newItem.Update();
            context.ExecuteQuery();
        }
    }
}
Leave a Comment