Question

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));
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top