激活SharePoint 2010 Timer Job 的Feature时,发生了错误:
The EXECUTE permission was denied on the object 'proc_putObject', database 'WSS_Config', schema 'dbo'
错误发生在激活过程中Timer Job 实例提交的时候
AutoMailJob autoMailJob = new AutoMailJob("SHFMOS_ReportMail_Job", webApp);
SPDailySchedule dailySchedule = new SPDailySchedule();
dailySchedule.BeginHour = 19;
dailySchedule.EndHour = 19;
dailySchedule.EndMinute = 5;
autoMailJob.Schedule = dailySchedule;
autoMailJob.Update();
因为:autoMailJob.Update();
有人说在执行这个代码的时候加上权限提升的语句
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Your Code
});
加了,但是还是一样的错误。
最终的解决方案是将feature部署的范围改成场或应用程序,利用场管理员激活。
参考文章:
http://rnowik.com/SharePoint-EXECUTE-permission-was-denied-on-the-object-proc_putObject.aspx
SharePoint 的Timer job是这么写
新增一个类,集成Timer Job的基类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Cbw.SHFMOS.AutoMailReport.Handlers;
namespace Cbw.SHFMOS.AutoMailReport
{
class AutoMailJob: SPJobDefinition
{
public AutoMailJob() : base() { }
public AutoMailJob(string jobName, SPWebApplication webApp)
: base(jobName, webApp, null, SPJobLockType.None)
{
this.Title = jobName;
Log.Log2File("New AutoMailJob", this.Title);
}
protected override bool HasAdditionalUpdateAccess()
{
Log.Log2File("HasAdditionalUpdateAccess", this.Title);
return true;
}
public override void Execute(Guid contentDbId) {
try
{
// Put your job's code here.
using (SPSite site = new SPSite("http://solution-ap.mycom.net/web"))
using (SPWeb web = site.OpenWeb())
{
//执行我的处理的逻辑,这里是发送邮件
FMOSReportHandler hdl = new FMOSReportHandler();
hdl.SendReportByZeRenZhuGuan(web);
hdl.SendReportByCuoShiFzr(web);
}
}
catch (Exception ex)
{
Log.Log2File("Exception Of Execute", ex.Message);
}
finally
{
}
}
}
}
新增一个feature用于控制Timer Job的启动和关闭
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace Cbw.SHFMOS.AutoMailReport.Features.Feature1
{
/// <summary>
/// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
/// </summary>
/// <remarks>
/// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。
/// </remarks>
[Guid("5c439d15-5aad-4b8d-9a72-ca2e8c118c76")]
public class Feature1EventReceiver : SPFeatureReceiver
{
// 取消对以下方法的注释,以便处理激活某个功能后引发的事件。
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
Log.Log2File("webApp", webApp.Name);
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == "SHFMOS_ReportMail_Job")
{
job.Delete();
}
}
try
{
AutoMailJob autoMailJob = new AutoMailJob("SHFMOS_ReportMail_Job", webApp);
SPDailySchedule dailySchedule = new SPDailySchedule();
dailySchedule.BeginHour = 19;
dailySchedule.EndHour = 19;
dailySchedule.EndMinute = 5;
autoMailJob.Schedule = dailySchedule;
autoMailJob.Update();
}
catch (Exception err)
{
Log.Log2File("Update Exception", err.Message);
}
//});
}
// 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//SPWeb web = properties.Feature.Parent as SPWeb;
// delete the job
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == "SHFMOS_ReportMail_Job")
job.Delete();
}
//});
}
}
}
而且注意,在feature的代码中:
properties.Feature.Parent会因feature所激活的位置而改变
如果feature是web级的,那么SPWeb web = properties.Feature.Parent as SPWeb;
如果feature是site级的,那么SPSite site = properties.Feature.Parent as SPSite;
类推:
应用程序级的:SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
场级的:SPFarm farm = properties.Feature.Parent as SPFarm;
2013/8/10 补充:
部署的时候,使用的PS命令按如下步骤执行:
using 'SharePoint 2010 Management Shell' Tool
1.add solution
PS> add-spsolution -LiteralPath c:\test.wsp
2.deploy solution
PS> install-spsolution test.wsp -webappliction http://targetapp.net -force
3.restart sp timer service
PS> net stop sptimerv4
PS> net start sptimerv4
4.restart feature in the webpplication features list
调试程序的时候注意两个问题:
1、重新部署后,需要重新启动sptimerv4服务(定时器服务)
2、调试的进程是:OWSTIMER.exe(定时器的进程)
3、重新激活feature