Monday, March 8, 2010

ExpirationTaskJob

-=-=-=-==-==-===-=-ExpirationWorkSpaceTimerJob.cs=============using System;using System.Collections.Generic;using System.Text;using Microsoft.SharePoint;using Microsoft.SharePoint.Administration;using Microsoft.SharePoint.Utilities;namespace ExpirationWorkSpace{ class ExpirationWorkSpaceTimerJob : SPFeatureReceiver { const string _JOB_NAME = "WorkSpace Expiration Timer Job"; //const string _JOB_NAME = "WorkSpace Expiration Date Task"; public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { // SPWeb web = null; // SPSite site = null; // SPWebApplication webApplication = this.Parent as SPWebApplication; SPWebApplication webapp = properties.Feature.Parent as SPWebApplication; if (webapp == null) throw new SPException("Error obtaining sp webapp"); // SPWeb web = properties.Feature.Parent as SPWeb; // string siteUrl = web.Site.Url; // SPSite site = new SPSite(siteUrl); //// SPSite site = properties.Feature.Parent as SPSite; // if (site != null) { // make sure the job isn't already registered //foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) foreach (SPJobDefinition job in webapp.JobDefinitions) { if (job.Name == _JOB_NAME) { job.Delete(); } } // install the job ExpirationTaskJob taskLoggerJob = new ExpirationTaskJob(_JOB_NAME, webapp); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 59; schedule.Interval = 1; taskLoggerJob.Schedule = schedule; //taskLoggerJob.Properties.Add("siteurl", web.Url); ////taskLoggerJob.Properties.Add("JobID", _jobID); //taskLoggerJob.Properties.Add("JobName", _JOB_NAME); taskLoggerJob.Update(); } }); } catch (Exception ex) { SPUtility.TransferToErrorPage(ex.Message); } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { //SPSite site = properties.Feature.Parent as SPSite; //// delete the job //foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) //{ // if (job.Name == wORKSPACE_EXPIRATION_JOB_NAME) // job.Delete(); //} ////throw new Exception("The method or operation is not implemented."); //SPWeb web = null; //SPSite site = null; try { SPSecurity.RunWithElevatedPrivileges(delegate() { //SPWeb web = properties.Feature.Parent as SPWeb; //string siteUrl = web.Site.Url; //SPSite site = new SPSite(siteUrl); //web = properties.Feature.Parent as SPWeb; //site = web.Site as SPSite; //if (site != null) SPWebApplication webapp = properties.Feature.Parent as SPWebApplication; if (webapp == null) throw new SPException("Error obtaining sp webapp"); // SPSite site = properties.Feature.Parent as SPSite; foreach (SPJobDefinition job in webapp.JobDefinitions ) { if (job.Name == _JOB_NAME) { job.Delete(); } } }); } catch (Exception ex) { Microsoft.SharePoint.Utilities.SPUtility.TransferToErrorPage(ex.Message); } } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { // throw new Exception("The method or operation is not implemented."); } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { // throw new Exception("The method or operation is not implemented."); } }}===========================-=-=-===---------ExpirationTaskJob.cs------------==========
using System;using System.Web;using System.Data;using Microsoft.SharePoint;using Microsoft.SharePoint.Administration;using System.Collections.Specialized;using Microsoft.SharePoint.Utilities;using System.Text;namespace ExpirationWorkSpace{ class ExpirationTaskJob : SPJobDefinition{ public ExpirationTaskJob () : base(){ } /// Name of the job. /// The service. /// The server. /// Type of the target. public ExpirationTaskJob (string jobName, SPService service, SPServer server, SPJobLockType targetType) : base (jobName, service, server, targetType) { } /// /// Initializes a new instance of the class. /// /// Name of the job. /// The web application. public ExpirationTaskJob(string jobName, SPWebApplication webApplication) : base (jobName, webApplication, null, SPJobLockType.ContentDatabase) { this.Title = "WorkSpace Expiration Timer Job"; //this.Title = "WorkSpace Expiration Date Task"; } /// /// Executes the specified content db id. /// public override void Execute (Guid contentDbId) { #if (DEBUG) System.Diagnostics.Trace.Assert(false);#endif // get a reference to the current site collection's content database SPWebApplication webApplication = this.Parent as SPWebApplication; // SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database // SPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"]; ////SPList taskList = contentDb.Sites[0].RootWeb.Lists["ExpirationReport"]; SPList taskList = webApplication.Sites[0].RootWeb.Lists["ExpirationReport"]; SPListItemCollection allitems = taskList.Items; DataTable tbl = allitems.GetDataTable(); if (tbl != null) { DataView dv = new DataView(); dv = tbl.DefaultView; // dv.Table.Columns["ExpirationReport"].DataType = Type.GetType("System.DateTime"); dv.RowFilter = "ExpirationDate >='" + DateTime.Now.AddDays(-7) + "'"; DataTable newtbl = new DataTable(); newtbl = dv.ToTable(); int i; if (newtbl.Rows.Count > 0) { for (i = 0; i < sweburl =" newtbl.Rows[i][" _mailbody =" string.Empty;" _mailcontents =" new">"); _mailContents.Append(""); _mailContents.Append(" Following Workspace is about to expire on date Please do needful "); _mailContents.Append(" "); _mailContents.Append("" + sWebUrl.ToString() + ""); _mailContents.Append(" "); _mailContents.Append(""); _mailBody = _mailContents.ToString(); string body = _mailBody; string strToId = ExtractUserName((string)newtbl.Rows[i]["OwnerGrpUser"]) + "@u2ucourse.com"; string strsubject = "Workspace about to Expire"; StringDictionary headers = new StringDictionary(); headers.Add("to", strToId);// "administrator@u2ucourse.com"); headers.Add("from", "administrator@u2ucourse.com"); headers.Add("subject", strsubject); headers.Add("content-type", "text/html"); //This is the default type, so isn't neccessary. bool success = SPUtility.SendEmail(webApplication.Sites[0].RootWeb, headers, body); } } } } string ExtractUserName(string path) { string[] userPath = path.Split(new char[] { '\\' }); return userPath[userPath.Length - 1]; } }}
======================elements.xml
http://schemas.microsoft.com/sharepoint/"> ============================feature.xml=================

No comments:

Post a Comment