1

I have a document library called Documents in production. I wrote a timer job for sending emails based on the columns in the library.

Everything was fine in the dev environment but the timer job in prod is giving me exceptions. Any ideas?

enter image description here

`

public void SendCorporateReminder(SPWeb web)
    {
        SPList lstComplaint = Helper.GetList(web, "Documents", Helper.ListType.Library);

        if (lstComplaint != null)
        {
            string query = "<Where><Eq><FieldRef Name='DocFormat' /><Value Type='Choice'>Policy</Value></Eq></Where>";
            SPListItemCollection colRecords = Helper.ExecuteQueryRecursive(lstComplaint, query, null, 2000);

            if (colRecords != null && colRecords.Count > 0)
            {
                foreach (SPListItem item in colRecords)
                {

                    DateTime Expires = Helper.GetSPFieldDateTimeValue(item, "Expires");
                    String Title = Helper.GetSPFieldTextValue(item, "Title");
                    SPFieldUserValueCollection userResp = Helper.GetUsers(item, "Contact");
                    TimeSpan totaldays = DateTime.Now.Date - Expires.Date;
                    Int32 days = totaldays.Days;
                    String emailtemplatekeyA = "AEmail" + days.ToString();
                    String emailtemplatekeyB = "BEmail" + days.ToString();
                    String respToEmail = String.Empty;


                    if (userResp != null && userResp.Count > 0)
                    {
                        foreach (SPFieldUserValue user in userResp)
                        {
                            if (!string.IsNullOrEmpty(respToEmail))
                                respToEmail += ",";
                            respToEmail += user.User.Email;
                        }
                    }
                    HandleEmails handleEmail = new HandleEmails();
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.EmailRecipient, PlaceHolder.USERRESP.ToString(), respToEmail);
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.TITLE.ToString(), Title);
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.EXPDT.ToString(), Expires.ToShortDateString());
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.ID.ToString(), item.ID.ToString());
                    handleEmail.SendMail(web, emailtemplatekeyA);
                    handleEmail = new HandleEmails();
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.EmailRecipient, PlaceHolder.USERRESP.ToString(), respToEmail);
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.TITLE.ToString(), Title);
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.EXPDT.ToString(), Expires.ToShortDateString());
                    handleEmail.AddValueToPlaceholder(HandleEmails.EmailPlaceHolders.SendingTypeValue.OTHER, PlaceHolder.ID.ToString(), item.ID.ToString());
                    handleEmail.SendMail(web, emailtemplatekeyB);
                }
            }
        }
    }`
16
  • 1
    Can you share full exception messgae?
    – Aanchal
    Jun 6 '14 at 15:06
  • The stack trace only shows us where the exception occurred. What is the actual exception and error message? Jun 6 '14 at 15:06
  • 1)The Execute method of job definition CorporatePoliciesTimerJob.DailySchedule (ID 6a9e19a2-4141-47a7-b879-3ce10653b614) threw an exception. More information is included below. Object reference not set to an instance of an object.
    – user28455
    Jun 6 '14 at 15:11
  • 3
    Also, just as a suggestion, if you post more information requested by the commenters (like the error message, or more of your code), you should edit your question and add it there in a code block. It would provide better readability than posting code in the comments. Jun 6 '14 at 15:30
  • 1
    What do you mean by "project properties"? In the context of the Execute method on a timer job, this.Properties refers to a property bag stored on the timer job definition. In order for this.Properties["siteurl"] to have a value, you would need to set it in code somewhere. Do you do this in your feature activation code? Jun 6 '14 at 16:53
0

I found the issue and resolved it. Person whose userid is no longer valid is Contact person of a document (he is in contact column for the library I am creating timer job).

So timer job is throwing exception (Object reference not set to an instance of an object) whenever it is trying to read Contact column of the document. I figured it out by modifying my code as below and I removed that person from that column. That user exists in dev server so the timer job is worked in dev but not in prod. Anyhow, issue is resolved in prod now.

Helper.WriteUlsInformation("Item processing:" + i.ToString(), TraceSeverity.Verbose);
DateTime Expires = Helper.GetSPFieldDateTimeValue(item, "Expires");
Helper.WriteUlsInformation("Expires:" + Expires.ToString(), TraceSeverity.Verbose);
String Title = Helper.GetSPFieldTextValue(item, "Title");
Helper.WriteUlsInformation("Title:" + Title, TraceSeverity.Verbose);
SPFieldUserValueCollection userResp = Helper.GetUsers(item, "Contact");
Helper.WriteUlsInformation("UserResp read" , TraceSeverity.Verbose);
TimeSpan totaldays = DateTime.Now.Date - Expires.Date;
Int32 days = totaldays.Days;
Helper.WriteUlsInformation("Days:"+ days.ToString(), TraceSeverity.Verbose);
String emailtemplatekeyA = "AEmail" + days.ToString();
String emailtemplatekeyB = "BEmail" + days.ToString();
String respToEmail = String.Empty;

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.