in C# how do I use the FieldCollection class Add method for adding columns to shaprepoint lists?

in C# how do I use the FieldCollection class Add method for adding columns to shaprepoint lists?

I did a search online for the answer to this and the responses were sometimes close to the specifics but often not close at all. I saved my first attempt at this in file: https://www.likablelogic.org/NicholasAir/in%20C%23%20how%20do%20I%20use%20the%20FieldCollection%20class%20Add%20Method.htm

I am not talking about a C# List class. I am talking about a SharePoint List. And I am not System Collection class wither.

The question, again, is this. in C# how do I use the FieldCollection class Add method for adding columns to shaprepoint lists?

I asked AI. First I asked Microsoft’s Bing Copilot AI and the response was:

using Microsoft.SharePoint.Client;

ClientContext clientContext = new ClientContext("https://yoursharepointsiteurl");
List targetList = clientContext.Web.Lists.GetByTitle("Your List Title");

// Define the new field
FieldCreationInformation newFieldInfo = new FieldCreationInformation(FieldType.Text)
{
    DisplayName = "New Column",
    InternalName = "NewColumn",
    Group = "Custom Columns",
    AddToDefaultView = true
};

// Add the new field to the list
Field newField = targetList.Fields.Add(newFieldInfo);
clientContext.ExecuteQuery();

Console.WriteLine("New column added successfully.");

This does not help. I have some string types for columns, datetime tyhpes for columns, and some double types for columns. This bit of code does not explain how I manage to add those column types.

So I asked chatgpt.com

“in C# how do I use the FieldCollection class Add method for adding columns to shaprepoint lists?”

It answered:

using Microsoft.SharePoint;
using System;

namespace SharePointAddColumnExample
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://yoursitecollection"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Get the list where you want to add the column
                    SPList list = web.Lists["Your List Name"];

                    // Check if the field (column) already exists to avoid duplication
                    if (!list.Fields.ContainsField("New Column"))
                    {
                        // Add a new text column (single line of text)
                        list.Fields.Add("New Column", SPFieldType.Text, false);

                        // Update the list to save changes
                        list.Update();

                        Console.WriteLine("Column added successfully.");
                    }
                    else
                    {
                        Console.WriteLine("Column already exists.");
                    }
                }
            }
        }
    }
}

But this does nto work because this is an error:
// Add a new text column (single line of text)
list.Fields.Add(“New Column”, SPFieldType.Text, false);

The Add method for Fields does not take three arguments.

I am going to be editing and updating this post as I find an answer.

I did a search in Chinese Baidu and I think the code their AI gave me actually works. So in addition to surpassing us in GDP, it looks like China has surpassed us in other ways as well. And The West deserves its downfall, to be frank.

using Microsoft.SharePoint.Client;

using System;

namespace SharePointListExample
{
class Program
{
static void Main(string[] args)
{
// 填入SharePoint站点URL、用户名和密码
string siteUrl = “https://yoursharepointsite”;
string username = “yourusername”;
string password = “yourpassword”;

        SecureString securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }

        using (ClientContext context = new ClientContext(siteUrl))
        {
            context.Credentials = new SharePointOnlineCredentials(username, securePassword);

            // 获取指定的Web
            Web web = context.Web;

            // 获取Lists集合
            ListCollection lists = web.Lists;

            // 确定List名称和列名称
            string listTitle = "YourListTitle";
            string columnName = "NewColumn";

            // 获取指定的List
            List list = lists.GetByTitle(listTitle);

            // 确保列不存在
            if (list.Fields.GetByTitle(columnName) == null)
            {
                // 创建一个新的Field定义
                FieldText textField = new FieldText(context, list, columnName, FieldTextType.SingleLineOfText);

                // 设置Field的属性
                textField.Title = "New Column";
                textField.Required = true;

                // 添加Field到List中
                textField.Add();
                context.ExecuteQuery();

                Console.WriteLine($"Column '{columnName}' has been created successfully.");
            }
            else
            {
                Console.WriteLine($"Column '{columnName}' already exists.");
            }
        }
    }
}

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật