SP2013 TimerJob constructor SPServer parameter - sharepoint

I have a timer job and I am trying to run this timer job on specific server, below is the code I am trying to use to compare server name and creating instance of timer job on FeatureActivated event. I have no idea how to do this. Please help me and correct me if I am doing it totally wrong.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Get an instance of the SharePoint farm.
//SPFarm farm = SPFarm.Local;
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
// Remove job if it exists.
DeleteJobAndSettings(webApp);
var serverName = SPServer.Local.DisplayName;
if (string.Equals("sp2013", serverName, StringComparison.OrdinalIgnoreCase))
{
// Create the job.
MyReportNew job = new MyReportNew(webApp, SPServer.Local);
//Other code
}
}

Firstly, you need to use SPServerJobDefinition class.
Secondly, retrieve SPServer object from SPFarm.Local.Servers collection.
For example:
public class CustomJob : SPServerJobDefinition
{
public CustomJob()
: base()
{
}
public CustomJob(string jobName, SPServer server)
: base(jobName, server)
{
this.Title = jobName;
}
public override void Execute(SPJobState state)
{
// do stuff
}
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Get an instance of the SharePoint farm.
SPFarm farm = SPFarm.Local;
// Remove job if it exists.
DeleteJobAndSettings(webApp);
// Create the job.
MyReportNew job = new MyReportNew("MyJobName", farm.Servers["sp2013"]);
//Other code
}

Related

After creating a timer job i can't see it in the SharePoint administration panel

I've developed a timer job for one of my SharePoint web applications.
I have written my job from SPJobdefinition class:
public class EraseUsersJob : SPJobDefinition
{
#region constants
public struct Constantes
{
public const string JOB_NAME = "EraseUsers";
public const string JOB_TITLE = "Erase Users";
}
#endregion
#region constructors
public EraseUsersJob() : base() { }
public EraseUsersJob(string jobName,
SPService service,
SPServer server,
SPJobLockType targetType)
: base(jobName, service, server, targetType) { }
public EraseUsersJob(SPWebApplication webApplication)
: this(Constantes.JOB_NAME, webApplication)
{
}
public EraseUsersJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.Job)
{
this.Title = Constantes.JOB_TITLE;
}
#endregion
#region override
public override void Execute(Guid targetInstanceId)
{
//my code
}
// my private methods used in execute() method
Then within a console program a create a new instance of this job using the constructor with the SPWebApplication argument.
Then i set a schedule for my job and update it.
My problem is that when i check if my Timer Job has been created in the SharePoint administration i find that it has not been created.
Am i missing something?
If you need more details or further information I will provide it to you.
EDIT:
Here's my Program.cs:
class Program
{
static void Main(string[] args)
{
try
{
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri("http://XXXXX:80"));
//Console.WriteLine("Installing EraseUsers job ...");
JobManager jobsManager = new JobManager();
jobsManager.ApplyJobs(webApplication);
}
catch (Exception e)
{
Console.WriteLine("ERROR: "+e.Message);
}
}
Here's my job manager class:
public class JobManager : DeployJobHelper
{
public void ApplyJobs(SPWebApplication webApplication)
{
try
{
Console.WriteLine(" Installing EraseUsersJob");
EraseUsersJob eraseUsersJob= new EraseUsersJob(webApplication);
this.ApplyJob(webApplication, eraseUsersJob);
}
catch (Exception ex)
{
Console.WriteLine(" Error: " + ex.Message + " // " + ex.StackTrace);
}
Console.WriteLine(" Job installation finished.");
}
}
Here's my DeployHelper.cs:
public class DeployJobHelper
{
protected void ApplyJob(SPWebApplication webApplication, SPJobDefinition jobDefinition)
{
string jobName = jobDefinition.Name;
// delete previous Job definition
webApplication.DeleteJobByName(jobName);
//Install Job
jobDefinition.Schedule = new SPMinuteSchedule() { BeginSecond=0, EndSecond=50,Interval=2 }; //GetScheduleValue(jobName);
jobDefinition.Update();
}
}
Moreover i've seen this error in the ULS:
SharePoint cannot deserialize an object of type XyZ.AbC.EraseUsersJob.EraseUsersJob, XyZ.AbC.EraseUsersJob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null on this machine. This typically occurs because the assembly containing this type is not installed on this machine. In this case, this message can be safely ignored. Otherwise, the assembly needs to be installed on this machine in a location that can be discovered by the .NET Framework.
I've read this error has something to do with either restarting Windows Sharepoint Services Timer or with namespace issues. I've already restarted my Windows SharePoint Services Timer and all my classes are wrapped within XyZ.AbC.EraseUsersJob namespace.

web api 2 - Passing data from action filter to action as an argument

In order to avoid getting the user data on every action I've create an custom action filter that gets the user by its ID and then passes to the action.
public class UserDataAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
...
// getting the user and storing it in the request properties
object user = userBLL.GetUserById(userId);
actionContext.Request.Properties.Add("User", user);
}
}
And the I can get the user object in the action method like this:
[Authorize]
[UserData]
[HttpGet]
[Route("dosomething")]
public IHttpActionResult DoSomething()
{
// retrieve the user
object user;
Request.Properties.TryGetValue("User", out user);
User u = (User)user;
return Ok();
}
However, in MVC it's possible to use ActionParameters in the filter to store something that will be used by the action method, like so:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
...
// Create object parameter.
filterContext.ActionParameters["User"] = userBLL.GetUserById(userId);
}
And then use the User object as if it were part of the original request:
[AddActionParameter]
public ActionResult Index(User user)
{
// Here I can access the user setted on the filter
...
return View();
}
So, my question is: There is a way in Web API 2 to pass the User object from the action filter to the action as an argument, just like in MVC?
With ASP.NET Web API, you can create a parameter binding to receive an object, User in your case. You don't have to create a filter for this. So, you will create a binding like this.
public class UserParameterBinding : HttpParameterBinding
{
public UserParameterBinding(HttpParameterDescriptor descriptor) :
base(descriptor) { }
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,
HttpActionContext context,
CancellationToken cancellationToken)
{
SetValue(context, new User() { // set properties here });
return Task.FromResult<object>(null);
}
}
Then, to use the binding, you will configure it, like this.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// snip
config.ParameterBindingRules.Insert(0, d =>
d.ParameterType == typeof(User) ? new UserParameterBinding(d) : null);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
With that, wherever you have User as action method parameter, it will automatically bind the instance you are creating inside UserParameterBinding to that parameter.

How to access a list in a website from a timer job

I'm creating a timer Job. And i have to access some lists which are in my solution stored in the site : "http://server:9090/sites/thesite"
For the moment, in my Timer Job i use this :
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
SPList ParametresTech = contentDb.Sites["sites/thesite"].RootWeb.Lists[Constantes.Listes.PARAMETRES_TECHNIQUES.Name];
The problem i'm facing here is that i'm in my development environnement, and i don't know what will be the url of the site they will use to deploy the solution in production.
So is there a way to get to the list without knowing the name of the site ?
Thanks
EDIT :
That's how the timer job is activated :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string ListJobName = "SAPToSQL";
SPSite site = properties.Feature.Parent as SPSite;
// make sure the job isn't already registered
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == ListJobName)
job.Delete();
}
// install the job
TimerJobSAPToSP listLoggerJob = new TimerJobSAPToSP(ListJobName, site.WebApplication);
SPHourlySchedule schedule = new SPHourlySchedule();
schedule.BeginMinute = 0;
schedule.EndMinute = 59;
listLoggerJob.Schedule = schedule;
listLoggerJob.Update();
}
I would definitely identify the site collection using the feature ID that creates the timer job rather than by URL. Not only does this give you flexibility in naming sites, it also allows you to process multiple site collections that have each subscribed to the job.
I wrote the following utility method to collect the site collections for a timer job:
public static List<Guid> GetSiteIDs(SPWebApplication webApplication, Guid featureId)
{
List<Guid> ids = new List<Guid>();
foreach (SPSite site in webApplication.Sites)
{
try
{
if (SPSite.Exists(new Uri(site.Url))
&& null != site.Features[featureId])
{
try
{
ids.Add(site.ID);
}
catch (Exception ex)
{
// Handle Exception
}
}
}
finally
{
site.Dispose();
}
}
return ids;
}
In the featureId parameter, I pass a constant that I declare in my job definition class.
For more information see: Scope of a feature activated Custom Sharepoint-Timer Job

Render an MVC3 action to a string from a WCF REST service method

I have a WCF REST service that takes some parameters and sends an email. The template for the email is an MVC3 action. Essentially I want to render that action to a string.
If it were an ASP.NET WebForm, I could simply use Server.Execute(path, stringWriter, false). However when I plug in the path to my action, I get Error executing child request.
I have full access to HttpContext from my service (AspNetCompatibilityRequirementsMode.Allowed).
I know there are other answers out there for rendering actions to strings from within the context of a controller. How do I do this when I'm outside that world, but still on the same server (and, for that matter, in the same app)?
I cobbled together an answer based on several different google searches. It works, but I'm not 100% sure it's as lean as it could be. I'll paste the code for others to try.
string GetEmailText(TemplateParameters parameters) {
// Get the HttpContext
HttpContextBase httpContextBase =
new HttpContextWrapper(HttpContext.Current);
// Build the route data
var routeData = new RouteData();
routeData.Values.Add("controller", "EmailTemplate");
routeData.Values.Add("action", "Create");
// Create the controller context
var controllerContext = new ControllerContext(
new RequestContext(httpContextBase, routeData),
new EmailTemplateController());
var body = ((EmailTemplateController)controllerContext.Controller)
.Create(parameters).Capture(controllerContext);
return body;
}
// Using code from here:
// http://blog.approache.com/2010/11/render-any-aspnet-mvc-actionresult-to.html
public class ResponseCapture : IDisposable
{
private readonly HttpResponseBase response;
private readonly TextWriter originalWriter;
private StringWriter localWriter;
public ResponseCapture(HttpResponseBase response)
{
this.response = response;
originalWriter = response.Output;
localWriter = new StringWriter();
response.Output = localWriter;
}
public override string ToString()
{
localWriter.Flush();
return localWriter.ToString();
}
public void Dispose()
{
if (localWriter != null)
{
localWriter.Dispose();
localWriter = null;
response.Output = originalWriter;
}
}
}
public static class ActionResultExtensions
{
public static string Capture(this ActionResult result, ControllerContext controllerContext)
{
using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response))
{
result.ExecuteResult(controllerContext);
return it.ToString();
}
}
}

Problem Activating Sharepoint Timer Job

I have created a very simple sharepoint timer job. All i want it to do is iterate through a list and update each list item so that it triggers an existing workflow that works fine. In other words all i am trying to do is work around the limitation that workflows cannot be triggered on a scheduled basis. I have written a class that inherits from SPJobDefinition that does the work and i have a class that inherits from SPFeatureReceiver to install and activate it. I have created the feature using SPVisualdev that my coleagues have used in the past for other SP development.
My Job class is below:
public class DriverSafetyCheckTrigger : SPJobDefinition
{
private string pi_SiteUrl;
public DriverSafetyCheckTrigger(string SiteURL, SPWebApplication WebApp):base("DriverSafetyCheckTrigger",WebApp,null, SPJobLockType.Job)
{
this.Title = "DriverSafetyCheckTrigger";
pi_SiteUrl = SiteURL;
}
public override void Execute(Guid targetInstanceId)
{
using (SPSite siteCollection = new SPSite(pi_SiteUrl))
{
using (SPWeb site = siteCollection.RootWeb)
{
SPList taskList = site.Lists["Driver Safety Check"];
foreach(SPListItem item in taskList.Items)
{
item.Update();
}
}
}
}
}
And the only thing in the feature reciever class is that i have overridden the FeatureActivated method below:
public override void FeatureActivated(SPFeatureReceiverProperties Properties)
{
SPSite site = Properties.Feature.Parent as SPSite;
// Make sure the job isn't already registered.
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == "DriverSafetyCheckTrigger")
job.Delete();
}
// Install the job.
DriverSafetyCheckTrigger oDriverSafetyCheckTrigger = new DriverSafetyCheckTrigger(site.Url, site.WebApplication);
SPDailySchedule oSchedule = new SPDailySchedule();
oSchedule.BeginHour = 1;
oDriverSafetyCheckTrigger.Schedule = oSchedule;
oDriverSafetyCheckTrigger.Update();
}
The problem i have is that when i try to activate the feature it throws a NullReferenceException on the line oDriverSafetyCheckTrigger.Update(). I am not sure what is null in this case, the example i have followed for this is this tutorial. I am not sure what I am doing wrong.
I ran your code in a console application and got the following exception when calling .Update()
"DriverSafetyCheckTrigger cannot be deserialized because it does not have a public default constructor"
Maybe that will help?
I have similar code that is working in one of my Feature Receivers:
string jobName = "MyJobDefinition";
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == jobName)
{
job.Delete();
}
}
SPDailySchedule schedule = new SPDailySchedule();
schedule.EndHour = 2;
schedule.EndMinute = 59;
schedule.EndSecond = 59;
SPJobDefinition jobDefinition = new MyJobDefinition(jobName, site.WebApplication);
jobDefinition.Schedule = schedule;
jobDefinition.Update();
I wonder if your problem is with the schedule. Try setting BeginMinute and maybe also BeginSecond. You could also try setting the End values in conjunction with or instead of the Begin values.

Resources