Friday, June 12, 2009

Simple PunchIn PunchOut program using SPList



Scenario :The below code demoss a simple PunchIn / PunchOut program uisng SPList. Also depicts the Inserting data to sharepoinnnnnnt list using webpart. First step, see the below image output.
Steps :Step 1 : Create a custom list aaand add 3 columns to it. Username, InTime, OutTime.
Step 2 : Use the below code
Step 3 : Deploy the webpart and add the webpart to ur page.
Code :
  
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace Punch
{
[Guid("c123a186-3795-4eda-b655-62c4dcedbeff")]
public class PunchTime : System.Web.UI.WebControls.WebParts.WebPart
{
private const string punchListName = "punchlist";
public PunchTime()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Button button = new Button();
button.Text = "Punch In";
button.Click += new EventHandler(button_Click);
this.Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
SaveTimeLogs();
}
void SaveTimeLogs()
{
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;
SPListItemCollection listItemCol = web.Lists[punchListName].Items;
SPListItem item = listItemCol.Add();
item["UserName"] = user.Name.ToString();
item["InTime"] = DateTime.Now.ToShortTimeString();
item.Update();
}
}
}

5 comments:

Sandeep K Nahta said...

kool, did u ever see the Group board discussion template, they have similar thing for timesheet entry or something

Dreamweaver said...

hai sandeep
i done as u have said.i have sucessfully deployed to sharepoint too.but when i click the button i got a error like this "An unexpected error has occurred.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator."

How can i solve this.

Srini Sistla said...

#1. This code is very basic level code... you can add some logs etc on this.
#2. If you are using the same code as is... your custom list name must be 'punchlist' as on the constant 'punchlistname' and also have the columns as mentioned.
#3. I guess you must have already verified the safecontrol entries etc.
#4. Try to debug and see where it is breaking.

Dreamweaver said...

HI Srini.
its working now.

But now i have doubt now.
Now we have 2 buttons punch in and punch out.When i click punch out its got inserted into another coulmn(not in the one where punch in is inserted)

How can i make Puchin and Puch out to a single row.

Srini Sistla said...

You can basically use a simple SPQuery to see for a logged-in user, get the row where his punchin is not null and punchout is null for a given date... and update that rows punchout column.

or, get the ID of the punchin row, update punchout column of the same row.