Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Variation of timer job : Explaination needed

$
0
0

I have implemented a timer job to update list items. I got it working based on the info from various blogs, this link being one of them(http://www.sharepointdiary.com/2012/05/creating-custom-timer-job-for-automations.html) . However my problem is this: I have used three variations of the same task in the Execute method(to update list item). Two of them work, but the third runs the timer job but doesn't seem to update list items.

Here is the two variations that works

1)

SPSite oSPSite = new SPSite("http://sitename");
                SPWeb oSPWeb = oSPSite.RootWeb;
                oSPWeb.AllowUnsafeUpdates = true;
                SPList taskList = oSPWeb.Lists["Tasks"];
                SPListItem newTaskItem = taskList.Items.Add();
                newTaskItem["Title"] = DateTime.Now.ToString();
                newTaskItem.Update();
                oSPWeb.AllowUnsafeUpdates = false;

2)

            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
            SPList Listjob = contentDb.Sites[0].RootWeb.Lists["Tasks"];
            SPListItem newItem = Listjob.Items.Add();
            newItem["Title"] = DateTime.Now.ToString();
            //newItem["Place"] = "";
            newItem.Update();

the third below doesn't work:

3)

            using (SPSite site = new SPSite("http://site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["Tasks"];
                    SPListItem item = list.Items.Add();
                    item["title"] = "item added " + list.Items.Count;
                    item.Update();
                    web.AllowUnsafeUpdates = false;

                }
            }

Could someone please tell me the difference between these three code blocks and possibly why the third one doesn't work.

any help much appreciated...

thanks


JK


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>