Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom Timer Job that runs on every Monday. However, I want to be able to run the timer job manually outside of Central admin. but I dont want to use PowerShall or stsadm. Can I put a link or a button on my site collection/SharePoint site and when someone clicks that link the timer job should run?

Thanks, Vikram

share|improve this question
    
Just to be sure - SharePoint 2010/2013 or SharePoint 2007? –  SPArchaeologist Mar 8 '13 at 10:41
add comment

2 Answers

You can write similar code on your button click :

      SPSite site = new SPSite([your-site-url]);

        foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
        {
            if (job.Name == "YouTimerjobname")
                job.RunNow();
        }
share|improve this answer
1  
using(SPSite site = new SPSite([your-site-url])) should be used as you are creating a new instance of SPSite that needs to be disposed. –  Muhammad Raja Mar 8 '13 at 10:27
    
I used the code you gave and created an webpart with a button. But when I click the button i get a error "The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. "?? –  Vikram Mar 9 '13 at 5:09
1  
try the code using SPsecurity.RunWithElevatedPrivileges –  Amit Kumawat Mar 9 '13 at 5:24
    
this is the code i am using now, can plz tell me wht iam doing wrong? –  Vikram Mar 9 '13 at 6:31
    
protected void btn_sendEmail_Click(object sender, EventArgs e) {SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite("xxxxxxxsiteURLxxxxx")) {foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) {if (job.Name.Equals("Email")) job.RunNow(); }} }); } –  Vikram Mar 9 '13 at 6:49
show 2 more comments

You can start a timer job by code. So you can create a web part with a button. When a user clicks the button the 'onclick' code of the button starts the job.

Add the web part to a page and/or set security on the page so that only you can access it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.