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'm having a problem to reach a specific SPlist in my sitecollection. The timerjob is supposed to read all items from a SPlist I've choseed and send them to a user through mail. I'm not sure if it's the right location of my site I'm trying to reach. When I'm checking my job history the status of my timerjob says failed and gives me the following errormessage: nativehr 0x80070003 /nativehr nativestack /nativestack. The site im trying to reach has the url: winsharepoint/sites/MLA/AdminSite and the lists name is "Accounting - MLA".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;
using System.IO;

namespace MLATimerjob3
{
public class SimpleJobDefinition : SPJobDefinition
{
    public const string JobName = "MLA - Timer job";

    public SimpleJobDefinition() : base() { }

    public SimpleJobDefinition(SPWebApplication webApp) :
        base(JobName, webApp, null, SPJobLockType.Job)
    {
        Title = "MLA - Timer job";
    }

    public override void Execute(Guid targetInstanceId)
    {
        string mailBody = "";

        using (SPWeb webSite = new SPSite("http://winsharepoint/sites/MLA/AdminSite").OpenWeb())
        {
            SPList list = webSite.GetList("Accounting - MLA");
            SPSite site = new SPSite("http://winsharepoint/");

            SPWeb web = site.OpenWeb("MLA/AdminSite");

            foreach (SPListItem item in list.Items)
            {
                if (DateTime.Parse(item["Serving Date"].ToString()).ToShortDateString() 
                    == DateTime.Now.ToShortDateString())
                {
                    mailBody = item["Menu Item"].ToString() + '\t'
                         + item["Specified Meal"].ToString() + '\t'
                         + item["Company Name"].ToString() + '\t'
                        + item["Price"].ToString() + '\t'
                        + item["Guest"].ToString() + '\t'
                         + item["Name"].ToString() + '\t'
                     + item["Serving Date"].ToString() + '\t'
                      + item["Ordered Date"].ToString() + '\n';
                }
            }

            StringDictionary headers = new StringDictionary();
            headers.Add("to", "daniel@example.com");
            headers.Add("cc", "xyz@abc.com");
            headers.Add("bcc", "");
            headers.Add("from", "email@add.com");
            headers.Add("subject", "Email Subject");
            headers.Add("content-type", "text/html");

            SPUtility.SendEmail(web, headers, mailBody);

        }

    }
}
}
share|improve this question
add comment

2 Answers

The SPWeb.GetList method takes a server relative URL, not just the name.

E.g. from the MS Docs:

string listUrl = "/sites/sitecollection/subsite/Lists/Announcements";
SPList list = web.GetList(listUrl);

And yeah, there shouldn't be a need to create the 2nd site and web objects (and if there is some reason for it, you need to dispose them too).

share|improve this answer
add comment

Try to debug the timer job and see if you are getting any null reference for SPSite\SpWeb\SPList.To debug, set break-point and attach to Owstimer process (and not w3wp). You may need to reset Owstimer as well before you start debugging the latest code.

By the way, you can have below line of code instead of creating new spweb again :

SPUtility.SendEmail(webSite, headers, mailBody);
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.