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

  • 2
    According to my understanding do you want to read the text file (URL, Active) and need to save it in the custom list? – Debugger Sep 18 '17 at 7:24
  • Yes Exactly !!!. – Aashirya Sep 18 '17 at 9:32

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]);

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.