Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.0 kB
1
Indexable
Never
foreach (DataRow ad in advertisingExcel.Rows)
{
    // Assuming these are the correct column names in your SharePoint list
    string databaseColumnName = ad["DatabaseColumn"].ToString();
    string sharepointColumnName = ad["SharePointColumn"].ToString();

    if (advertisingDT.Columns.Contains(databaseColumnName))
    {
        foreach (DataRow dr in advertisingDT.Rows)
        {
            // Check if the column is "OrganizationName" and if its value is null or empty
            if (databaseColumnName == "OrganizationName" &&
                (dr[databaseColumnName] == null || string.IsNullOrEmpty(dr[databaseColumnName].ToString())))
            {
                // Concatenate "OrganixationFirstName" and "OrganizationLastName" to create the final value
                string finalValue = $"{dr["OrganixationFirstName"]}{dr["OrganizationLastName"]}";

                Console.WriteLine($"Database Column: {databaseColumnName}, SharePoint Column: {sharepointColumnName}, Value: {finalValue}");

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem newItem = targetList.AddItem(itemCreateInfo);

                newItem[sharepointColumnName] = finalValue;
                newItem.Update();
            }
            else
            {
                // If the column is not "OrganizationName" or its value is not null or empty, use the original value
                Console.WriteLine($"Database Column: {databaseColumnName}, SharePoint Column: {sharepointColumnName}, Value: {dr[databaseColumnName]}");

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem newItem = targetList.AddItem(itemCreateInfo);

                newItem[sharepointColumnName] = dr[databaseColumnName];
                newItem.Update();
            }
        }
    }
    else
    {
        Console.WriteLine($"Column {databaseColumnName} not found in DataTable.");
    }
}
Leave a Comment