SharePoint development-a simple example of TimerJob
The TimerJob in SharePoint is similar to a scheduled job in Windows, which allows you to perform specified operations on a scheduled basis.
The example described in this article is to synchronize SharePoint users in SharePoint 2010 Foundation.
1. Open Visual Studio 2010, create an empty SharePoint solution, and select deploy as a field solution.
2. Add a new class MyTimerJob, add using to reference Microsoft. SharePoint. Administration, and inherit from the SPJobDefinition class. This is our own timerjob. Then declare two constructors, as shown below:
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. The next most important step is to write out what timerjob needs to do. Rewrite the Execute method. The Code is as follows:
Public override void Execute (Guid targetInstanceId) {base. execute (targetInstanceId); SqlConnection conn = new SqlConnection (ConfigurationManager. connectionStrings ["SPMIPConn"]. connectionString); conn. open (); // The fetch site is deployed in webConfig. You can directly modify your own webConfig // <add key = "SiteURL" value = "http: // spf02 "/> string siteURL = ConfigurationManager. appSettings ["SiteURL"]; using (SPSite st = new SPSite (siteURL) {SPWeb w Eb = st. rootWeb; SPList userList = web. siteUserInfoList; // web. lists ["User Information List"]; 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. zhi_Gid,. 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 =. zhi_gid left join TD_BuM as c on c. id = B. bu_mid left join TM_ZhiWxx as d on d. zhi_wxxid = B. zhi_wid where. shan_Cqf = 'no' and. userCode = '{0}' ", loginname); SqlDataReader sdr = cmd. executeReader (); if (sdr. read () {// return or set the user's display name user. name = sdr ["Zhi_gxm"]. toString (); user. update (); string queryStr = "<Where> <Eq> <FieldRef Name = 'id'/> <Value Type = 'number'>" + user. ID + "</Value> </Eq> </Where>"; // SPQuery class to query the list: SPQuery query = new SPQuery (); // set the XML query. query = queryStr; SPListItem userItem = userList. getItems (query) [0]; // Title userItem ["JobTitle"] = sdr ["Zhi_wmch"]. toString (); // Department userItem ["Department"] = sdr ["Bu_mmch"]. toString (); userItem. update ();} else {userIDsForDel. add (user. loginName);} sdr. close ();} web. siteUsers. removeCollection (userIDsForDel. toArray ();} conn. close ();}
Here, because my environment is based on the Form authentication of the SQL data user table, stamp it here, so the specific method is to read the data in the SQL user table, then, synchronize the user list to SharePoint.
4. timerjob is finished. Now we need to use feature to control the timerjob, add the timerjob when the feature is activated, and delete it when the activation is canceled. The Code is as follows:
Public override void FeatureActivated (SPFeatureReceiverProperties properties) {const string MY_TASK = "SPMIP User Information Synchronization"; // The Event check timer 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 User Information Synchronization"; // event check timer SPSite site = properties. feature. parent as SPSite; foreach (SPJobDefinition job in site. webApplication. jobDefinitions) {if (job. name = MY_TASK) {job. delete (); break ;}}}
The above is the general process of applying timerjob.