Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. 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?

Scheduled Pinned Locked Moved C#
databasequestioncsharpsharepointjson
4 Posts 2 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xarzu
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • X Xarzu

      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.

      C Offline
      C Offline
      charles henington
      wrote on last edited by
      #2

      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[^]

      X 1 Reply Last reply
      0
      • C charles henington

        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[^]

        X Offline
        X Offline
        Xarzu
        wrote on last edited by
        #3

        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
                            };
        
        C 1 Reply Last reply
        0
        • X Xarzu

          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
                              };
          
          C Offline
          C Offline
          charles henington
          wrote on last edited by
          #4

          Have you read this? Complete basic operations using SharePoint client library code | Microsoft Learn[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups