SharePoint开发 - TimerJob简单实例讲解

来源:转载

SharePoint中的TimerJob类似于Windows系统的计划任务,可以实现定时执行指定操作的功能。

本篇所述的实例为在SharePoint 2010 Foundation版本下实现SharePoint用户的同步功能。

1. 打开Visual Studio 2010,创建一个SharePoint的空解决方案,选择部署为场解决方案

2. 添加一个新类MyTimerJob,添加using引用Microsoft.SharePoint.Administration,继承SPJobDefinition类,这个便是我们自己的timerjob。然后声明两个构造函数,如下所示

using Microsoft.SharePoint.Administration;namespace TimerJobExample{public class MyTimerJob : SPJobDefinition{public MyTimerJob() : base() { }public MyTimerJob(string jobName, SPWebApplication webApp) : base(jobName, webApp, null, SPJobLockType.Job) { this.Title = jobName; }}}

3. 接下来最重要的一步,就是我们需要写出来这个timerjob需要做什么。重写Execute方法,代码如下所示

public override void Execute(Guid targetInstanceId) { base.Execute(targetInstanceId); SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SPMIPConn"].ConnectionString); conn.Open(); //取站点 在webConfig部署,用户直接修改自己webConfig //<add key="SiteURL" value="http://spf02"/> string siteURL = ConfigurationManager.AppSettings["SiteURL"]; using (SPSite st = new SPSite(siteURL)) { SPWeb web = st.RootWeb; SPList userList = web.SiteUserInfoList; //web.Lists["用户信息列表"]; string loginname = string.Empty; List<string> userIDsForDel = new List<string>(); foreach (SPUser user in web.SiteUsers) { if (!user.LoginName.Contains("spmipmp|")) { continue; } loginname = user.LoginName.Substring(user.LoginName.LastIndexOf('|') + 1); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = string.Format("select a.Zhi_Gid,a.UserCode,b.zhi_gxm,c.Bu_mmch,d.Zhi_wmch from SYS_User as a left join TM_Zhigxx as b on b.Zhi_gid=a.Zhi_gid left join TD_BuMas c on c.id=b.Bu_mid left join TM_ZhiWxx as d on d.Zhi_wxxid=b.Zhi_wid where a.Shan_Cqf='否' and a.UserCode='{0}'", loginname); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read()) { //返回或设置用户的显示名 user.Name = sdr["Zhi_gxm"].ToString(); user.Update(); string queryStr = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + user.ID + "</Value></Eq></Where>"; //SPQuery类来完成对列表的查询 SPQuery query = new SPQuery(); //设置XML查询 query.Query = queryStr; SPListItem userItem = userList.GetItems(query)[0]; //职务 userItem["JobTitle"] = sdr["Zhi_wmch"].ToString(); //部门 userItem["Department"] = sdr["Bu_mmch"].ToString(); userItem.Update(); } else { userIDsForDel.Add(user.LoginName); } sdr.Close(); } web.SiteUsers.RemoveCollection(userIDsForDel.ToArray()); } conn.Close(); }

这里,由于我手头的环境是基于SQL数据用户表的Form认证,戳这里,所以具体的做法是,读取到SQL用户表中的数据,然后同步到SharePoint的用户列表。

4. timerjob写完了,现在我们需要通过feature去控制这个timerjob,在feature激活的时候添加这个timerjob,并在取消激活的时候删除它,代码如下

public override void FeatureActivated(SPFeatureReceiverProperties properties) { const string MY_TASK = "SPMIP用户信息同步";//事件检查定时器 SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == MY_TASK) { job.Delete(); break; } } Synchronization schtion = new Synchronization(MY_TASK, site.WebApplication); SPDailySchedule schedule = new SPMonthlySchedule(); schedule.BeginHour = 23; schedule.BeginMinute = 40; schedule.BeginSecond = 1; schedule.EndHour = 23; schedule.EndMinute = 59; schedule.EndSecond = 1; schtion.Schedule = schedule; schtion.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { const string MY_TASK = "SPMIP用户信息同步";//事件检查定时器 SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == MY_TASK) { job.Delete(); break; } } }
以上就是应用timerjob的大致过程

分享给朋友:
您可能感兴趣的文章:
随机阅读: