-1

I was wondering if anyone could explain me the following code what it does.

SPWebApplication webApp = this.Parent as SPWebApplication;

SPList taskList = webApp.Sites[0].RootWeb.Lists["MyList"];

SPListItemCollection taskListItems = taskList.Items;

and the following 2 constructor what do they do?

public CustomTimerJob() : base() { }
        public CustomTimerJob(string jobName, SPService service) :
            base(jobName, service, null, SPJobLockType.None)
        {
            // |Emri i job timer
            this.Title = "Task Complete Timer";
        }
        public CustomTimerJob(string jobName, SPWebApplication webapp) :
            base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Task Complete Timer";
        }
1
SPWebApplication webApp = this.Parent as SPWebApplication;

SPList taskList = webApp.Sites[0].RootWeb.Lists["MyList"];

SPListItemCollection taskListItems = taskList.Items;

Above code means you are accessing MyList from where you are running your timer job. Assuming that the URL of your web application host and root site collection is same. If you wish to access MyList from another site collection, then use following code.

using (SPSite site = new SPSite("Your Site URL"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList taskList = web.Lists["MyList"];
                        SPListItemCollection taskListItems = taskList.Items;
                    }
                }

Rest of the code explanation

public CustomTimerJob() : base() { }
        public CustomTimerJob(string jobName, SPService service) :
            base(jobName, service, null, SPJobLockType.None)
        {
            // |Emri i job timer
            this.Title = "Task Complete Timer";
        }
        public CustomTimerJob(string jobName, SPWebApplication webapp) :
            base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Task Complete Timer";
        }

Learn more about timer job's constructors from here

The first step in creating a custom timer job is to create a class that inherits from SPJobDefinition. The SPJobDefinition class contains three overridden constructors. According to the Windows SharePoint Services 3.0 SDK, the constructor that has no parameters is reserved for internal use. The other two constructors set the timer job's name, the parent Web application or service (such as the search indexer), the server, and the job lock type. The lock type helps prevent multiple jobs from running at the same time. There are three different types of locking, as defined by the SPJobLockType enumeration:

  • SPJobLockType.ContentDatabase Locks the content database. A timer job runs one time for each content database that is associated with the Web application.

  • SPJobLockType.Job Locks the timer job so that it runs on only one machine in the farm.

  • SPJobLockType.None No locks. The timer job runs on every machine on which the parent service is provisioned.

  • SPWebApplication webApp = this.Parent as SPWebApplication; SPList taskList = webApp.Sites[0].RootWeb.Lists["MyList"]; SPListItemCollection taskListItems = taskList.Items; on this stage I am referring to spwebapplication. but can i do jobtimer to work also in spsitecollection so referring to spsitecollection not spswebapplication? – Nderon Hyseni Oct 7 '15 at 8:33
  • Yes. you can refer any site over here. In that case, approach is different. – Atish Dipongkor - MVP Oct 7 '15 at 8:39
  • how it is handle in this case all i need to change are these 3 lines of code, but im not sure SPWebApplication webApp = this.Parent as SPWebApplication; SPList taskList = webApp.Sites[0].RootWeb.Lists["MyList"]; SPListItemCollection taskListItems = taskList.Items; can you tell me what i have to do briefly – Nderon Hyseni Oct 7 '15 at 8:46
  • can you tell me briefly how to do it? – Nderon Hyseni Oct 7 '15 at 9:31
  • You want to access list from another site, right? – Atish Dipongkor - MVP Oct 7 '15 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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