博客地址 http://blog.csdn.net/foxdave

SharePoint中的TimerJob类似于Windows系统的计划任务,可以实现定时执行指定操作的功能。

本篇所述的实例为在SharePoint 2010 Foundation版本下实现SharePoint用户的同步功能。

1. 打开Visual Studio 2010,创建一个SharePoint的空解决方案,选择部署为场解决方案

2. 添加一个新类MyTimerJob,添加using引用Microsoft.SharePoint.Administration,继承SPJobDefinition类,这个便是我们自己的timerjob。然后声明两个构造函数,如下所示

using Microsoft.SharePoint.Administration;

namespace TimerJobExample
{
public class MyTimerJob : SPJobDefinition
{
public MyTimerJob() : base() { } public MyTimerJob(string jobName, SPWebApplication webApp) : base(jobName, webApp, null, SPJobLockType.Job) { this.Title = jobName; }
}
}

3. 接下来最重要的一步,就是我们需要写出来这个timerjob需要做什么。重写Execute方法,代码如下所示

public override void Execute(Guid targetInstanceId)
{
base.Execute(targetInstanceId);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SPMIPConn"].ConnectionString);
conn.Open();
//取站点 在webConfig部署,用户直接修改自己webConfig
//<add key="SiteURL" value="http://spf02"/>
string siteURL = ConfigurationManager.AppSettings["SiteURL"];
using (SPSite st = new SPSite(siteURL))
{
SPWeb web = st.RootWeb;
SPList userList = web.SiteUserInfoList; //web.Lists["用户信息列表"];
string loginname = string.Empty;
List<string> userIDsForDel = new List<string>();
foreach (SPUser user in web.SiteUsers)
{
if (!user.LoginName.Contains("spmipmp|"))
{
continue;
}
loginname = user.LoginName.Substring(user.LoginName.LastIndexOf('|') + 1);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = string.Format("select a.Zhi_Gid,a.UserCode,b.zhi_gxm,c.Bu_mmch,d.Zhi_wmch from SYS_User as a left join TM_Zhigxx as b on b.Zhi_gid=a.Zhi_gid left join TD_BuM as c on c.id=b.Bu_mid left join TM_ZhiWxx as d on d.Zhi_wxxid=b.Zhi_wid where a.Shan_Cqf='否' and a.UserCode='{0}'", loginname);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
//返回或设置用户的显示名
user.Name = sdr["Zhi_gxm"].ToString();
user.Update();
string queryStr = "<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>" + user.ID + "</Value></Eq></Where>";
//SPQuery类来完成对列表的查询
SPQuery query = new SPQuery();
//设置XML查询
query.Query = queryStr;
SPListItem userItem = userList.GetItems(query)[0];
//职务
userItem["JobTitle"] = sdr["Zhi_wmch"].ToString();
//部门
userItem["Department"] = sdr["Bu_mmch"].ToString();
userItem.Update();
}
else
{
userIDsForDel.Add(user.LoginName);
}
sdr.Close();
}
web.SiteUsers.RemoveCollection(userIDsForDel.ToArray());
}
conn.Close();
}

这里,由于我手头的环境是基于SQL数据用户表的Form认证,戳这里,所以具体的做法是,读取到SQL用户表中的数据,然后同步到SharePoint的用户列表。

4. timerjob写完了,现在我们需要通过feature去控制这个timerjob,在feature激活的时候添加这个timerjob,并在取消激活的时候删除它,代码如下

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
const string MY_TASK = "SPMIP用户信息同步";//事件检查定时器
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == MY_TASK)
{
job.Delete();
break;
} } Synchronization schtion = new Synchronization(MY_TASK, site.WebApplication);
SPDailySchedule schedule = new SPMonthlySchedule();
schedule.BeginHour = 23;
schedule.BeginMinute = 40;
schedule.BeginSecond = 1;
schedule.EndHour = 23;
schedule.EndMinute = 59;
schedule.EndSecond = 1;
schtion.Schedule = schedule;
schtion.Update();
} public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
const string MY_TASK = "SPMIP用户信息同步";//事件检查定时器
SPSite site = properties.Feature.Parent as SPSite;
foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
if (job.Name == MY_TASK)
{
job.Delete();
break;
}
}
}

以上就是应用timerjob的大致过程

SharePoint开发 - TimerJob简单实例讲解的更多相关文章

  1. SharePoint 开发TimerJob 介绍

    项目需要写TimerJob,以前也大概知道原理,不过,开发过程中,还是遇到一些问题,网上看了好多博客,也有写的灰常好的,不过,自己还是想再写一下,也算是给自己一个总结,也算给大家多一个参考吧. Tim ...

  2. (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)

    hibernate是非常典型的持久层框架,持久化的思想是非常值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和 ...

  3. 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)

    Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...

  4. Android Studio1.4.x JNI开发基础 - 简单实例

    接上一篇,搭建好基于Android Studio的环境之后,编写native代码相对来说也比较简单了.在Android上编写Native代码和在Linux编写C/C++代码还是有区别,Native代码 ...

  5. 简单实例讲解linux的module模块编译步骤

    注:原博文地址http://blog.sina.com.cn/s/blog_4ba5b45e0102v25h.html ---------------------------------------- ...

  6. VS2008中C#开发webservice简单实例

    1.创建工程 文件-> 新建->网站 如下图. 工程建好后,会自动添加如下代码: using System; using System.Linq; using System.Web; us ...

  7. MVVM开发模式简单实例MVVM Demo

    本文主要是翻译Rachel Lim的一篇有关MVVM模式介绍的博文 A Simple MVVM Example 并具体给出了一个简单的Demo(原文是以WPF开发的,对于我自己添加或修改的一部分会用红 ...

  8. SharePoint 2010 BCS - 简单实例(二)外部列表创建

    博客地址 http://blog.csdn.net/foxdave 接上篇 由于图片稍多篇幅过长影响阅读,所以分段来写. 添加完数据源之后,我们需要为我们要放到SharePoint上的数据表定义操作, ...

  9. SharePoint 2010 BCS - 简单实例(一)数据源添加

    博客地址 http://blog.csdn.net/foxdave 本篇基于SharePoint 2010 Foundation. 我的数据库中有一个病人信息表Patient,现在我就想把这个表中的数 ...

随机推荐

  1. [华为]查找两个字符串a,b中的最长公共子

    链接:https://www.nowcoder.com/questionTerminal/181a1a71c7574266ad07f9739f791506来源:牛客网 查找两个字符串a,b中的最长公共 ...

  2. ReactNative Ios报出 'React/RCTBundleURLProvider.h' file not found错误

    我在创建react-native项目时  npm了一个第三方库  结果一打开 xcode 竟然报错 React/RCTBundleURLProvider.h' file not found: 然后 我 ...

  3. Smarty 模板布局继承

    Smarty 模板继承 在覆盖父模板的{block}块以外的地方, 子模板不能定义任何内容.任何在{block}以外的 内容都会被自动忽略. 在子模板和父模板中的{block}内容,可以通过 appe ...

  4. EditPlus 4.3.2475 中文版已经发布(10月28日更新)

    新的修订版修复了上移多个插入点时会造成程序崩溃的问题.

  5. Python 安装pytz

    1.    https://pypi.org/project/pytz/#files 2.    下载上图标黄的文件, 3.    pip install 4.    from pytz import ...

  6. tomcat jdbc pool

    文中内容主要转自:http://www.open-open.com/lib/view/open1327478028639.html http://www.open-open.com/lib/view/ ...

  7. PowerDesigner教程系列

    文章转载至:http://www.cnblogs.com/yxonline/archive/2007/04/09/705479.html PowerDesigner教程系列(一)概念数据模型 目标:本 ...

  8. Harbor 企业级 Docker Registry

    HarBor项目:https://github.com/vmware/harbor 下载:https://github.com/vmware/harbor/releases 安装文档:https:// ...

  9. BurpSuite工具应用

    BurpSuite工具应用 BurpSuite是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP ...

  10. [PyTorch]论文pytorch复现中遇到的BUG

    目录 1. zip argument #1 must support iteration 2. torch.nn.DataParallel 3. model.state_dict() 1. zip a ...