问题描述
我编写了一个 SP2013 计时器作业,它应该在触发时从 SharePoint 组中删除用户。写在 CustomTimerJob.cs 中的一段代码下面。它在运行计时器时成功将列表项状态更新为活动/过期,但不会从 SharePoint 组中删除用户。可以让我知道我错在哪里吗?
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TimerJobApplication
{
public class CustomTimerJob : SPJobDeFinition
{
public CustomTimerJob() : base() { }
public CustomTimerJob(string jobName,SPService service)
: base(jobName,service,null,SPJobLockType.None)
{
this.Title = "Access Rights Timer";
}
public CustomTimerJob(string jobName,SPWebApplication webapp)
: base(jobName,webapp,SPJobLockType.ContentDatabase)
{
this.Title = "Access Rights Timer";
}
public void RemoveUser(String userLoginName,string groupName,string SiteURL)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SiteURL))
{
using (SPWeb web = site.OpenWeb())
{
SPGroup group = web.SiteGroups[groupName];
SPUser userToRemove = web.EnsureUser(userLoginName);
group.RemoveUser(userToRemove);
group.Update();
}
}
});
}
public override void Execute(Guid targetInstanceId)
{
var currentdate=DateTime.Now.Date;
SPWebApplication webApp = this.Parent as SPWebApplication;
SPList taskList = webApp.Sites[0].AllWebs["dev"].Lists["Limited_Access_To_Security_Groups"];
SPView view = taskList.Views["All Items"]; //custom view name
SPListItemCollection olistitems = taskList.GetItems(view);
foreach (SPListItem item in olistitems)
{
if (Convert.ToDateTime(item["ExpiryDate"]) <= DateTime.Now.Date)
{
RemoveUser(item["UserName"].ToString(),"Dev Visitors","http://devsite/");
item["Status"] = "Expired";
item.Update();
}
else
{
item["Status"] = "Active";
item.Update();
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)