Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
0
Indexable
Never
static void ProcessDataForSharePoint(ClientContext context, List targetList, string listName, DataTable databaseData, DataTable excelData)
{
    // Explicitly load the Title field
    context.Load(targetList, l => l.Fields.Include(f => f.InternalName, f => f.Title));
    context.ExecuteQuery();

    foreach (DataRow row in databaseData.Rows)
    {
        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem newItem = targetList.AddItem(itemCreateInfo);

        foreach (DataRow mappingRow in excelData.Rows)
        {
            string databaseColumn = mappingRow["DatabaseColumn"].ToString();
            string sharePointColumn = mappingRow["SharePointColumn"].ToString();

            if (databaseData.Columns.Contains(databaseColumn))
            {
                // Check if the column exists in databaseData
                object columnValue = row[databaseColumn];

                // Handle concatenation for the Title field
                if (sharePointColumn.ToLower() == "title")
                {
                    string userFirstName = row["UserFirstName"] as string;
                    string userLastName = row["UserLastName"] as string;

                    if (!string.IsNullOrEmpty(userFirstName) || !string.IsNullOrEmpty(userLastName))
                    {
                        newItem[sharePointColumn] = $"{userFirstName} {userLastName}".Trim();
                    }
                    else
                    {
                        newItem[sharePointColumn] = "Default Title"; // Provide a default value if both are null
                    }
                }
                else
                {
                    newItem[sharePointColumn] = columnValue;
                }

                Console.WriteLine(columnValue);
            }
            else
            {
                Console.WriteLine($"Column '{databaseColumn}' not found in {listName} databaseData.");
            }
        }

        newItem.Update();
    }

    context.ExecuteQuery();
}
Leave a Comment