自定义计时器作业无法在生产中使用

时间:2014-06-06 15:51:01

标签: sharepoint-2010 sharepoint-timer-job

我在生产中有一个名为Documents的文档库。我写了一个计时器作业,用于根据库中的列发送电子邮件。

在开发环境中一切都很好但是prod中的计时器工作给了我例外。有什么想法吗?

        Entering monitored scope (Timer Job Corporate Policies - Reminder Emails)

作业定义的执行方法CorporatePolicyReminders.DailySchedule(ID 3586f4d2-1bc1-4770-b5b8-4c2c578ee8d5)引发了异常。更多信息包括在下面。对象引用未设置为对象的实例。

异常堆栈跟踪:
 在CorporatePolicyReminders.CorporatePolicyReminders.SendCorporateReminder(SPWeb web)
 在CorporatePolicyReminders.DailySchedule.Execute(Guid contentDbId)
 在Microsoft.SharePoint.Administration.SPTimerJobInvokeInternal.Invoke(SPJobDefinition jd,Guid targetInstanceId,Boolean isTimerService,Int32& result)

离开监控范围(计时器工作公司政策 - 提醒电子邮件)。执行时间= 364.288198639771

SendCorporateReminers Method

 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);
            }
        }
    }
}`

1 个答案:

答案 0 :(得分:0)

我找到了问题并解决了它。其用户标识不再有效的人是联系文档的人(他在我创建计时器作业的库的联系人列中)。因此,只要计划读取文档的“联系人”列,计时器作业就会抛出异常(对象引用未设置为对象的实例。)我通过修改我的代码如下所示,并从该列中删除了该人。

    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;