Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to report status from a SharePoint timer job

I have a SharePoint timer job that requires a configuration list to exist in a specific location in the site collection. If that list does not exist, I want to indicate that to the user so they can create (and populate of course) said list and rerun the job.

I can write to the Event Log with the code below and I know I can throw an exception to indicate a failed job status, but what I want to do is throw an exception with a message that indicates the problem in a way that doesn't require looking through the ULS or having access to the Event log. The posts I've found so far like this one and this one don't have too many details.

So two questions: 1) Is there a way to provide a failure message for a timer job exception? 2) Is there a better choice to throw than Exception()?

Event logging used when the site collection is missing

SPDiagnosticsService.Local.WriteEvent(0,  
    new SPDiagnosticsCategory("MyCategory",   
        TraceSeverity.Unexpected, 
        EventSeverity.ErrorCritical), 
    EventSeverity.ErrorCritical, 
    "Assert failed: if (!spweb.Exists)" + sp.Url, 
    sp.ToString());

What I'd like to do with the missing config list

bool configListExists = ListExists(spweb, ConfigListName);  
if (! configListExists)  
{   
    ReportMissingConfigList();  
    throw new Exception("Configuration list not found");  
}  

public static bool ListExists(SPWeb web, string listName)
{
    return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));
}
like image 624
Kelly S. French Avatar asked Nov 14 '22 00:11

Kelly S. French


1 Answers

I would recommend having a separate log-list next to the config list. In this list you could write the status of the job when it is needed and then the user(s) can have a notification set up on this list so they can take appropriate measures.

like image 171
Rikard Uppström Avatar answered Dec 10 '22 18:12

Rikard Uppström