You need to write a custom SharePoint timer job to regularly check the image expiration date in the list of site collection images. If it is about to expire, you need to send an email to the relevant personnel.
Google found very few materials. Fortunately, I found Andrew Connell's blog with a very detailed sample code. Http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx
The basic idea of the Code is: our custom job class needs to be inherited from spjobdefinition, and several constructors and an execute method need to be rewritten. The default constructor without parameters is required.
So how can we get this spwebapplication object in the execute method? AC uses the following code:
Spwebapplication webapplication = This. Parent as spwebapplication;
Using the parent attribute and cast as a webapplication object is quite confusing, because according to the instructions in the SDK, there is a webapplication attribute that can be used. Why is it so troublesome. I tried the following code:
Spwebapplication webapplication = This. webapplication;
Test results show that they are completely equivalent.
Well, after writing this class, we need to write another spfeaturereceiver class. Because we need a feature to deploy our timer to the server and trigger/close this timerjob through feature activated/deactivated.
Public override void featureactivated (spfeaturereceiverproperties properties)
{
Spsite site = properties. feature. Parent as spsite;
Spweb web = site. rootweb;
// Make sure the job isn' t already registered
Foreach (spjobdefinition job in site. webapplication. jobdefinitions)
{
If (job. Name = "imagevalidationcheckerjob ")
{
Job. Delete ();
}
}
// Install the job
Imagevalidationcheckerjob = new imagevalidationcheckerjob ("imagevalidationcheckerjob", site. webapplication );
Spdailyschedule schedule = new spmonthlyschedule ();
Schedule. beginhour = 23;
Schedule. beginminute = 40;
Schedule. beginsecond = 1;
Schedule. endhour = 23;
Schedule. endminute = 59;
Schedule. endsecond = 1;
Imagevalidationcheckerjob. Schedule = schedule;
Imagevalidationcheckerjob. Update ();
}
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 = "imagevalidationcheckerjob ")
{
Job. Delete ();
}
}
}
This spdailyschedule object bothers me a little. I now understand that you need to set the start time and end time, and then the system will randomly calculate a time in the middle of the two to start the job. For example, in the above example, the daily running time should be between and. I don't know if this is the case, but I guess it should be the case.
After the code is written, you need to write a feature. This feature is relatively simple, just an XML file.
Feature. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Feature xmlns = "http://schemas.microsoft.com/sharepoint"
Id = "1f481c17-4f00004919-a64a-eae5c1301b4b"
Title = "image validation checker"
Description = "If any images in the top level site colleciton images are expiring soon, email relative person ."
Scope = "Site"
Hidden = "true"
Version = "1.0.0.0"
Receiverassembly = "timerjobcontrol, version = 1.0.0.0, culture = neutral, publickeytoken = f2aef6a9088f714f"
Receiverclass = "timerjobcontrol. imagevalidationcheckerjobinstaller">
</Feature>
The rest is to install the feature, if there is no error in the code.
As for how to debug this program, take the following steps: (some steps may not be used sometimes, but they can be used to ensure that there is no problem. This is the blood lesson I learned in one day)
(For debugging, set schedule to spminuteschedule and set it to run every 2 minutes)
Place Assembly DLL in GAC
Command Line: iisreset
Deactivate feature first, and then activate feature.
Command Line: net stop sptimerv3
Command Line: Net start sptimerv3
Visual Studio: attach to process: owstimer. exe
.
Note that if you want to use the web in the timerjob class. I think it is impossible to get some worth words from the config file. At least I haven't found out how to do it. So, if the code needs to obtain some information from the outside,
Solution: create a file in the c: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ bin directory named owstimer.exe. config,
<Configuration>
<Deleetask>
<Add key = "yourkey" value = "yourvalue"/>
</Appsettings>
</Configuration>
Use configurationmanager. deleetask. get ("yourkey"); to obtain this value.
Remember to take the steps mentioned above before debugging the code after each modification, especially steps 4 and 5 are easy to forget. Otherwise, you may encounter inexplicable problems.
Finally, happy vertex pointing.