Question

I have a text file with URL & active entry

URL                  Active 
Http://              0  
http://              1

I want to convert the file to a SharePoint list in visual web part

Was it helpful?

Solution

By this way, you can achieve your solution, Please Update the logic as for your context.

using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
        oSPWeb.AllowUnsafeUpdates = true;

        // Fetch the List
        SPList list = oSPWeb.Lists["MyList"];

        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\timesheetfile.txt");

        SPListItem itemToAdd = null; 

        //Display the file contents by using a foreach loop and update logic as You want.           
        foreach (string line in lines)
        {
        string[] arr = line.Split(",");

         //Add a new item in the List
        itemToAdd = list.Items.Add();
        itemToAdd["URL"] = Convert.Tostring(arr[0]);
        itemToAdd["Active"] = Convert.Tostring(arr[1]);
        itemToAdd.Update();

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top