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.");
}
}
}
}
}