Eric's Blog

有需求才有进步

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

背景: 
       需要一个list用来记录我们上班/下班时间,需求并不是复杂。

分析:
       1. 其实该webpart仅需要两个按钮就可以了, 一个按钮是上班, 另一个则是下班的按钮;为了防止员工更改上班时间干脆就直接用了一个按钮,如果当天该员工有了一条记录在list里面,那么该按钮变成下班的按钮。
       2. 为了防止员工手动在list里面自己new,于是干脆把所有的add/edit/delete功能全部禁止了,只剩下了一个view item。 于是在代码里面只能用虚拟身份登录,还好2007有了SPSecurity.RunWithElevatedPrivileges,方便了许多。
       3. 功能比较简单,所以并没有写一个完整的webpart,而是写了一个UserControl,然后使用了Kanboy的QuickPart,发布非常的方便。

CODE:
  1.  查询该员工在当天是否有记录。


// 查询
// strToday 今天的日期
// GetEmployee() 员工名字
public string QueryString
    
{
        
get {
            System.Text.StringBuilder strQueryBuffer 
= new System.Text.StringBuilder();
            strQueryBuffer.Append(
"<Where><And>");
            strQueryBuffer.AppendFormat(
"<Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq>", strToday);
            strQueryBuffer.AppendFormat(
"<Eq><FieldRef Name='Employee'/><Value Type='Text'>{0}</Value></Eq>", GetEmployee());
            strQueryBuffer.Append(
"</And></Where>");
            strQuery 
= strQueryBuffer.ToString();
            
return strQuery;
        }

    }



    
private Boolean CheckExistWorkTime(string strToday, string strUserName)
    
{
        myQuery 
= new SPQuery();
        myQuery.Query 
= QueryString;
        ListItems 
= myList.GetItems(myQuery);
        
if (ListItems.Count >= 1)
        
{
            
return true;
        }

        
else
        
{
            
return false;
        }

    }

  2.  响应按钮事件, 插入、更新记录:



        
//Run as system administrator
        SPSecurity.RunWithElevatedPrivileges(delegate()
        
{
            
using (SPSite currentSite = new SPSite(mySite.ID))
            
{
                
using (SPWeb currentWeb = currentSite.OpenWeb(myWeb.ID))
                
{
                        currentWeb.AllowUnsafeUpdates 
= true;
                        SPList TimeList 
= currentWeb.Lists[strList];                        
                        
if (CheckExistWorkTime(strToday, strEmployeeName))
                        
{
                            
// Update当天的记录,更新下班的时间
                            SPListItemCollection TimeListItems;
                            myQuery 
= new SPQuery();
                            myQuery.Query 
= QueryString;
                            TimeListItems 
= TimeList.GetItems(myQuery);
                            
foreach (SPListItem myItem in TimeListItems)
                            
{
                                myItem[strEndTimeField] 
= strTime;
                                myItem.Update();
                            }

                        }

                        
else
                        
{
                            
//Insert当天的记录, 记录上班的时间
                            SPListItem myItem = TimeList.Items.Add();
                            myItem[strDateField] 
= strToday;                //Single line of text = 当天的日期
                            myItem[strEmpolyeeField] = myCurrentUser;       //Person or Group = 未虚拟身份之前的用户
                            myItem[strStartTimeField] = strTime;            //Single line of text = 时间
                            myItem.Update();
                            ButtonSubmit.Text 
= "Check Out";

                        }

                        currentWeb.AllowUnsafeUpdates 
= false;                       

                                    
                }

            }

        }
);


效果:
没有记录的效果(上班)               有记录的效果(下班)
 
List 的内容:

posted on 2008-02-01 14:44  Eric.Chai  阅读(201)  评论(0编辑  收藏  举报