Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all

i want to ask if i could read text file data and import it to sharepoint list items
in fact i want to read employees time attendance card reader text file and import its data to a list contain employees data sch as :employee name , NO,time in ,time out in order to create a time attendance tracking software using sharepoint 2007

i appreciate any suggestion ,please to send me your opinions to achieve my goal

Thanks
Posted

1 solution

Friend,

Make sure your txt data is uniform in future so its easy to write logic.

I am happy it helps you and I am suggesting you a logic for reading a file.

Before begin my assumptions on txt file are :
1] data written in file is such that each entry is on newline. {or some separator }
2] file is accessible and you have permissions to read it.

Now follow below links and implement it in your code.
links
http://msdn.microsoft.com/en-us/library/vstudio/ezwyzy7b(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.90).aspx

This is basic code for file access.
You can read all data in array or else, as below :

C#
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.           
            foreach (string line in lines)
            {
            string[] arr = line.Split(",");

             //Add a new item in the List
            itemToAdd = list.Items.Add();
            itemToAdd["EmpName"] = Convert.Tostring(arr[0]);
            itemToAdd["EmpNumber"] = Convert.Tostring(arr[1]);
            itemToAdd["TimeIn"] = Convert.Tostring(arr[2]);
            itemToAdd["TimeOut"] = Convert.Tostring(arr[3]);
            itemToAdd.Update();

            }
             
             list.Update(); 
            oSPWeb.AllowUnsafeUpdates = false;
       }
}


For scheduling :

I can suggest many way's. Prefer what you want.

Way 1] Create a Timer job : This will read .txt file and use server object model to perform CRUD operation. Use this solution in case you do not need instant entry. You can schedule it for hourly so it will read entry on hourly basis.

follow link to write and understand timer :
http://lamahashim.blogspot.in/2010/02/creating-timer-job-in-moss.html[^]

Way 2] Create you own Windows utility {.exe} and schedule it in windows OS scheduler. .exe can be console application which contains server side code to perform CRUD operations. Here you need to add SharePoint dll.
Personally i do not prefer it because put .exe on production is not good idea.

Way 3] You can use default service provided by SharePoint like List.asmx
In this case you can create empty SharePoint project, add application page the add reference of list.asmx in it. Then read data from .txt file and upload into list.

Refer this blog :
http://blogs.msdn.com/b/pranab/archive/2008/11/24/sharepoint-2007-moss-wss-using-lists-asmx-getlistitems.aspx[^]

Refer my answer as well :
C# code to upload document to Sharepoint without using Microsoft.Sharepoint DLL[^]

Way 4] Create your own WCF service to perform CRUD operations and read txt file.
Host it in IIS or in share point to perform operations.
You can integrate it with Timer job or with custom utility {.exe}

Oh that was huge solution.
Hope you are good to go :)
 
Share this answer
 
v2
Comments
Sarah MQ 31-Oct-13 1:52am    
thanks a lot PrasadShastri for your response ,you said that "I hope you do not need code link for "How to read data from file"" but i really need :(
and i understood that 4 ways you suggested above is about the scheduling process to the reading from file in order to make my tracking up to date and its very useful
but a gain i don't know how to read the file and add its data in the list
Bajirao_ 1-Nov-13 1:31am    
I have updated solution, check it out.
Sarah MQ 2-Nov-13 3:49am    
thanks again PrasadShastri i will apply your solution and i will send feedback
but what do you think about retrieving text file data and save it in array using javascript and sharepoint designer to import data to list ?
is it a weak solution?
Bajirao_ 3-Nov-13 11:09am    
This is good solution to read file, but you need to think about its scheduling.
I think here challenge is trigger client side code on txt file update Or hit client side code on regular interval of time to read file and create list item.

If you able do that, it would be great.
I suggest you to write an article on it, no matter what ever way you choose.

Good Luck !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900