Help needed with custom timerjob in SharePoint 2007
•22 April, 2009 • 6 CommentsOk this is driving me mad
. I’ve created a timerjob in SharePoint 2007 with the following purpose: once a day, loop through a document library and check if the value in a datefield is in the past. If so send an email to the emailaddress provided in another field of the library.
Now I’m not a experienced developer in SharePoint so this is the main code I’ve written:
public override void Execute(Guid contentDbId) {
// set the reference to the sitecollection and library
SPSite siteCollection = new SPSite(http://xxxxx/nl);
SPWeb portalWeb = siteCollection.RootWeb;
SPList docList = portalWeb.Lists["Sales Contracts"];
// used vars & objects:
String strDocName, strSubject, strBody, strDocTitle, strMailTo;
DateTime docAlertDate;foreach (SPListItem aDoc in docList.Items)
{
if (aDoc["Alert Date"] != null)
{
docAlertDate = Convert.ToDateTime(aDoc["Alert Date"].ToString());
// if the alertdate is in the past then send alert
if (docAlertDate.Date <= DateTime.Today && aDoc["Alert Receiver"] != null)
{
// send mail to address in Field “Alert Receiver” if provided
strMailTo = getAlertReceivers(aDoc["Alert Receiver"].ToString());
// create message
if (aDoc["Title"] != null) strDocTitle = aDoc["Title"].ToString(); else strDocTitle = “No Title”;
strDocName = aDoc["Name"].ToString();
strSubject = “Concerning item: ” + strDocName;
strBody = “The following item is about to expire:<br>”;
// creating link to view properties
strBody += “<a href=’” + siteCollection.Url.ToString() + “/” + docList.Title.ToString() + “/Forms/DispForm.aspx?ID=” + aDoc.ID.ToString() + “‘>” + strDocName + “</a><br>”;
strBody += strDocTitle;
strBody += “<br>Please review/replace this before the effective expiration date.”;
// send mail
SPUtility.SendEmail(portalWeb, true, false, strMailTo, strSubject, strBody);
}
else
{
// handle (or not) Alert Receivers empty -
}
}
}
// cleaning up
docList = null;
portalWeb.Dispose();
siteCollection.Dispose();}
public string getAlertReceivers(string pAlertReceiversField) {
String strAlertReceivers = “”; //emptying receivers list
//splitting the AlertReceiverField to get different recievers if present
//a field based on userlookup, entries look like this: “14;#user@dev.loc; #15; #user1@dev.loc”string[] arEntries;
char[] splitter = { ‘#’, ‘;’ };
int y;
arEntries = pAlertReceiversField.Split(splitter);
for (y = 0; y < arEntries.Length; y++)
{
//if item contains “@”, add the emailaddress
if (arEntries[y].IndexOf(“@”, 0) > 0)
{
strAlertReceivers += arEntries[y] + “;”;
}
}
return strAlertReceivers;
}
}
For testing purposes I deploy the timerjob with the following schedule:
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
Now, in the beginning everything is working fine and I get an email for documents where the datefield is in the past. So for example if there’s 1 document with a datefield value in the past I get 1 email every 5 minutes (schedule of timerjob). But after several runs of the timerjob I start getting mutiple (duplicate) emails for the same document. I’ve already tested with a longer schedule of 15 minutes but the problem is the same: after some runs of the timerjob I start to receive multiple emails.
Maybe it’s some stupid (inexperienced) issue I’m missing here, but does anyone have an idea what’s the problem?
Thanks,
Peter
Adding an administrator to SharePoint 2007
•5 August, 2008 • Leave a CommentEver wanted to add another user as administrator in SharePoint 2007? Giving him all the rights in the Central Administration? Ok, no problem, you say, just add the user by changing “Update farm administrator’s group” and have a nice day. But soon you will see that some links will give you the access denied message, grrrr. Links like “User Profiles and Properties” and “Import Application Definition”. I solved my problems by checking the following:
- add the user to the “Site collection administrators” for the correct site collections
- in the Shared Services Administration check the “Personalization services permissions”
- still in the Shared Services Administration check “Business Data Catalog permissions”
Maybe this is useful for someone else to
Folder Content Types in SharePoint 2007
•1 August, 2008 • Leave a CommentDid you know you also can create content types for folders? Now what’s great about that you wonder? Here are 2 reasons:
- you can create views that are only visible in a folder with a particular content type.
- inside the folder you can control which document content types can be used.
I learned to appreciate this feature through this post with some great info!
Creating a timer job in SharePoint 2007
•1 August, 2008 • 2 CommentsFor the project I’m working for the moment i needed a document library with contracts. The contracts have an end date and an email alert needed to be send when this end date was received. After exploring the possibility to used a work flow created in SharePoint Designer, i abounded the idea and set looked at creating a timer job. This timer job needed to run once a day, check the contracts end date and send alerts when needed.
Handy links where:
But the most useful link I found was by Alexander Brütt (blog by Tobias Hertkorn). On this page you can download a fully functioning Visual Studio Template to create a SharePoint timer job and to get a deployable .wsp file after building the project.There’s even a *.bat file to deploy the solution. Nice!
I’ve created this *.bat file to easily uninstall the feature and solution when testing:
stsadm -o deactivatefeature -name customTimerjob -url http://DEVMOSS/
stsadm -o uninstallfeature -name customTimerjob
stsadm -o retractsolution -name customTimerjob.wsp -immediate
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name customTimerjob.wsp
pause
