5

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));
}
Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93

1 Answers1

1

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.

Rikard Uppström
  • 1,413
  • 9
  • 16
  • This leads to another question which I think I'll ask seperately, a) what if the log-list doesn't exist?, b) maybe I should add the lists in the FeatureActivation feature receiver method instead of assuming they've been created manually? , c) is programatically adding the lists that much better than manually creating them? – Kelly S. French Aug 15 '12 at 14:34
  • I would recommend, as you suggest, that you create the list in the feature-receiver instead. Automatic creation is (almost) always preferable to manual creation since it reduces work, increases stability and ensures that the settings for the list(s) is exactly the same across all instances of you application (prod, test, dev). – Rikard Uppström Aug 16 '12 at 06:34
  • Other issues remain like a dependence on a hard-coded location for the cofig list. I want to create it in the root of the site collection that activated the feature but when I tried that approach I wasn't able to get it to work. I'll have to post it as another question and then update this one with a link. – Kelly S. French Aug 16 '12 at 14:19
  • Yes, do so and I'll take a look. Also, if you found my answer helpful, please mark as answer.. :) – Rikard Uppström Aug 17 '12 at 06:22