knowledgecenter-breadcrum

Knowledge Center

31 May, 2023

Create SharePoint list items with attachments in bulk using the Console app in C#.

Posted on 31 May, 2023 by Admin, Posted in SharePoint , C#

Create SharePoint list items with attachments in bulk using the Console app in C#. Blogs

Introduction :
In this blog we will understand Bulk Creation of SharePoint List Items with Attachments using C# Console App

Steps:

Step 1: Create console app in VS Studio as shown below.

Step 2: Declare variables that takes input from the user and store inputs received in declared variables.

Step3: Use SecureString class to create a secure password.

Step4: Right click on the project and open Manage NuGet Packages installer.

Search for Microsoft.SharePointOnline.CSOM as shown below.

Use below code to connect to the SharePoint.

Step5: After connecting SharePoint inside client context get list by name.
Create the list item creation information to store the created items as shown below

Step6: Creating the document to attach in the attachment of the list item.

Step7: Now, using a foreach loop, we are creating the desired number of list items and updating them in the SharePoint list. Additionally, we are also updating the attachments in the list items.

Step8: Once all the list items are added in the list creation information, we execute the below command to create all the list items in SharePoint in bulk.

Step9: Here is the output of the code

Code:

using Microsoft.SharePoint.Client;
using System;
using System.IO;
using System.Security;
using System.Text;
namespace NISL.BLOG.CONSOLEAPP
{
class Program
{
private static string spAdminEmail = string.Empty;
private static string spPassword = string.Empty;
private static string SharePOintURL = string.Empty;
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("Please Enter Dynamics SharePoint details:");
Console.WriteLine();
Console.WriteLine("1. Enter SharePoint URL: (eg. https://nisltest2.sharepoint.com/sites/ddatest1)");
SharePOintURL = Console.ReadLine();
        Console.WriteLine("2. Enter SharePoint Admin Email: (eg. admin@yourdomain.com)");
        spAdminEmail = Console.ReadLine();

        Console.WriteLine("3. Enter SharePoint Admin Password:");
        spPassword = Console.ReadLine();

        string password = spPassword;
        SecureString securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }
        using (ClientContext clientContext = new ClientContext(SharePOintURL))
        {

            // Set the credentials
            clientContext.Credentials = new SharePointOnlineCredentials(spAdminEmail, securePassword);

            // Specify the target list
            List targetList = clientContext.Web.Lists.GetByTitle("Demo_1");

            // To store the Create ListItems
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

            string strMessage = "this is a demo";
            byte[] filename = Encoding.ASCII.GetBytes(strMessage);

            for (int i = 1; i < 100>             {
                //Create new List Item to Add the Item In SharePoint List
                ListItem newItem = targetList.AddItem(itemCreateInfo);
                newItem["Title"] = "Demo_" + i;
                newItem.Update();
                // Add the Attchmnets
                for (int j = 1; j <= 2; j++)
                {
                    AttachmentCreationInformation attachmentInfo = new AttachmentCreationInformation();
                    attachmentInfo.FileName = "Demo_"+j+".txt";
                    attachmentInfo.ContentStream = new MemoryStream(filename);
                    // Add the attachment to the list item
                    newItem.AttachmentFiles.Add(attachmentInfo);
                    newItem.Update();
                }
            }
            // Craete All list in builk in share Point
            clientContext.ExecuteQuery();
        }

        Console.WriteLine("Process End.");
        Console.ReadLine();
    }
}
}

Comment

Loading

Recent Updates

Thumbnail D365 for Marketing
Blogs
16 Dec, 2024

10 Reasons Why You Need Dynamics 365 for Marketing

This blog discusses the top 10 reasons to choose Dynamics 365 for Marketing to improve your business's marketing efforts.  …

READ MORE
Thumbnail_top 10 benefits of
Blogs
02 Dec, 2024

Top 10 Benefits of Microsoft Dynamics 365

This blog lists the advantages of Microsoft Dynamics 365, focusing on how it can improve your business operations.    …

READ MORE
Thumbnail_Microsoft365 Vs D365
Blogs
12 Nov, 2024

Microsoft 365 vs Dynamics 365

This blog will help you understand the key difference between Microsoft 365 and Dynamics 365, so you can make an…

READ MORE