Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.7 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"
            if (databaseColumnName == "OrganizationName")
            {
                // Check if its value is null or DBNull.Value
                if (dr[databaseColumnName] == null || dr[databaseColumnName] == DBNull.Value)
                {
                    // Concatenate "OrganixationFirstName" and "OrganizationLastName" to create the final value
                    string finalValue = $"{dr["OrganixationFirstName"]}{dr["OrganizationLastName"]}";

                    Console.WriteLine($"Database Column: {databaseColumnName}, SharePoint Column: {sharepointColumnName}, Original Value: {dr[databaseColumnName]}, Final Value: {finalValue}");

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

                    newItem[sharepointColumnName] = finalValue;
                    newItem.Update();
                }
                else
                {
                    // If the value is not null, 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
            {
                // If the column is not "OrganizationName", 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