How do I check for existance or clear the contents of a SharePoint List from C# code?
-
How do I check for existance or clear the contents of a SharePoint List from C# code?
I have complete the code that creates a sharepoint List from scratch, creates the columns, and loads the data that I get from an API GET query of an external database.
How do I:
check and see if the SharePoint List already exists clear the contents of the sharepoint list load the list with new data.
Without doing these steps, I am left with creating the list from scratch each time and giving it a unique name each time.
Is there some source of information on how to do this that I can find online? The Microsoft AI chat bot has given me some code that does not work at all.
-
How do I check for existance or clear the contents of a SharePoint List from C# code?
I have complete the code that creates a sharepoint List from scratch, creates the columns, and loads the data that I get from an API GET query of an external database.
How do I:
check and see if the SharePoint List already exists clear the contents of the sharepoint list load the list with new data.
Without doing these steps, I am left with creating the list from scratch each time and giving it a unique name each time.
Is there some source of information on how to do this that I can find online? The Microsoft AI chat bot has given me some code that does not work at all.
Can you show code of how you create/store the List? This link may also be of some help Enable and configure versioning for a list or library - Microsoft Support[^]
-
Can you show code of how you create/store the List? This link may also be of some help Enable and configure versioning for a list or library - Microsoft Support[^]
Hi Charles, I sure can. Editing it or presenting it in such a way where company information is no there is going to take time. The code first assumes it is creating the list for the first time and when an exception is thrown if the list already exists, it just loads the list. Here is the code
string listTitle = "TripLegs" ; string listDescription = "This is a new list created using CSOM"; Microsoft.SharePoint.Client.List? newList = null;
Next, we use the "using" method and start a block like this:
using (var authenticationManager = new AuthenticationManager()) using (var context = authenticationManager.GetContext(site, user, password)) { context.Load(context.Web, p => p.Title); await context.ExecuteQueryAsync(); Console.WriteLine($"Title: {context.Web.Title}"); Web web = context.Web; // Create a new list ListCreationInformation creationInfo = new ListCreationInformation(); creationInfo.Title = listTitle; creationInfo.Description = listDescription; creationInfo.TemplateType = (int)ListTemplateType.GenericList; newList = web.Lists.Add(creationInfo);
THis is when the try catch block is used if an exception is NOT thrown, then we assume that we are dealing with a list that has not been created yet and the use of the ListCreationInformation class is the proper thing to do. The code in the "TRY" block proceeds to create the columns.
try { // Load the list and execute the query context.Load(newList); context.ExecuteQuery(); Console.WriteLine("List created successfully!"); FieldCollection fields = newList.Fields; // set up the columns #region ColumnHeaders FieldCreationInformation fieldInfo\_1 = new FieldCreationInformation(FieldType.Number) { DisplayName = "TripId", InternalName = "TripId", Group = "Custom Columns", AddToDefaultView = true };
-
Hi Charles, I sure can. Editing it or presenting it in such a way where company information is no there is going to take time. The code first assumes it is creating the list for the first time and when an exception is thrown if the list already exists, it just loads the list. Here is the code
string listTitle = "TripLegs" ; string listDescription = "This is a new list created using CSOM"; Microsoft.SharePoint.Client.List? newList = null;
Next, we use the "using" method and start a block like this:
using (var authenticationManager = new AuthenticationManager()) using (var context = authenticationManager.GetContext(site, user, password)) { context.Load(context.Web, p => p.Title); await context.ExecuteQueryAsync(); Console.WriteLine($"Title: {context.Web.Title}"); Web web = context.Web; // Create a new list ListCreationInformation creationInfo = new ListCreationInformation(); creationInfo.Title = listTitle; creationInfo.Description = listDescription; creationInfo.TemplateType = (int)ListTemplateType.GenericList; newList = web.Lists.Add(creationInfo);
THis is when the try catch block is used if an exception is NOT thrown, then we assume that we are dealing with a list that has not been created yet and the use of the ListCreationInformation class is the proper thing to do. The code in the "TRY" block proceeds to create the columns.
try { // Load the list and execute the query context.Load(newList); context.ExecuteQuery(); Console.WriteLine("List created successfully!"); FieldCollection fields = newList.Fields; // set up the columns #region ColumnHeaders FieldCreationInformation fieldInfo\_1 = new FieldCreationInformation(FieldType.Number) { DisplayName = "TripId", InternalName = "TripId", Group = "Custom Columns", AddToDefaultView = true };
Have you read this? Complete basic operations using SharePoint client library code | Microsoft Learn[^]