定时器作业--将sap数据更新至列表库

前期工作:SAP数据通过Biztalk连接,发布成webservices。将列表库结构设计好,并存为模板
为了能实现快速更新,采取直接删除整个列表库再新建录入数据的方式更新。

新建类

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections;
//web引用
using SAPTimer.Biztalk;
namespace SAPTimer
{
    public class SAPDataTimer : SPJobDefinition
    {
        public SAPDataTimer() : base() { }

        /// <summary>
        /// 初始化EventCheckTimer
        /// 更多的构造函数请参看SDK
        /// </summary>
        /// <param name="_timername">计时器的名称</param>
        /// <param name="_wp">Web应用程序名称</param>
        public SAPDataTimer(string _timername, SPWebApplication _wp)
            : base(_timername, _wp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "SAPTimer";
        }

        /// <summary>
        /// 此方法由系统调用,contentDbId也由系统传递
        /// </summary>
        /// <param name="_contentdbid">内容数据库的id</param>
        public override void Execute(Guid targetInstanceId)
        {

            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
            //门户站点
            SPSite spsite = new SPSite("。。。");
            //子网站
            SPWeb spweb = spsite.OpenWeb("/");
           
            SPList list1 = spweb.Lists["WBSInfo"];
            SPList list2 = spweb.Lists["StaffInfo"];
            SPList list3 = spweb.Lists["DeptInfo"];

            WebService_BizForOA wsBFOA = new WebService_BizForOA();

            WBSInfo wbs = new WBSInfo();
            WBSCollection[] wbsCollections = wsBFOA.GetWBSInfo(wbs);

            StaffInfo si = new StaffInfo();
            StaffCollection[] staffCollections = wsBFOA.GetStaffInfo(si);

            DeptInfo de = new DeptInfo();
            DeptCollection[] deptCollections = wsBFOA.GetDeptList(de);

            //删除权限
            spweb.AllowUnsafeUpdates = true;

            #region 更新WBSInfo
            //清空文档库list1
            spweb.Lists.Delete(list1.ID);
            SPListTemplateCollection listTemplateCollection = spsite.GetCustomListTemplates(spweb);
            //列表库模板:可在Moss中将特定列表库另存为模板文件,位于站点中。
            SPListTemplate template = listTemplateCollection["WBS"];
            Guid ListLibGuid = spweb.Lists.Add("WBSInfo", "", template);
            list1 = spweb.Lists[ListLibGuid];

            //add
            foreach (WBSCollection wwbs in wbsCollections)
            {
                SPListItem item1 = list1.Items.Add();
                item1["WBS元素编号"] = wwbs.WBS元素编号;
                item1["合同名称"] = wwbs.合同名称;
                item1["简明标识"] = wwbs.简明标识;
                item1["合同签署地"] = wwbs.合同签署地;
                item1["合同签署地代码"] = wwbs.合同签署地代码;
                item1["利润中心"] = wwbs.利润中心;
                item1["利润中心代码"] = wwbs.利润中心代码;
                item1["项目负责人"] = wwbs.项目负责人;
                item1["项目负责人代码"] = wwbs.项目负责人代码;
                item1.Update();
            }
            #endregion

            #region 更新StaffInfo
            //清空list2
            spweb.Lists.Delete(list2.ID);
            listTemplateCollection = spsite.GetCustomListTemplates(spweb);
            //列表库模板:可在Moss中将特定列表库另存为模板文件,位于站点中。
            template = listTemplateCollection["Staff"];
            ListLibGuid = spweb.Lists.Add("StaffInfo", "", template);
            list2 = spweb.Lists[ListLibGuid];
            //add
            foreach (StaffCollection st in staffCollections)
            {
                SPListItem item1 = list2.Items.Add();
                item1["员工ID"] = st.ID;
                item1["姓名"] = st.姓名;
                item1["登录名"] = st.登录名;
                item1["EMail"] = st.EMail;
                item1["职位"] = st.职位;
                item1["职位代码"] = st.职位代码;
                item1["部门"] = st.部门;
                item1["部门代码"] = st.部门代码;
                item1["手机"] = st.手机;
                item1["座机"] = st.座机;
                item1.Update();
            }
            #endregion

            #region 更新DeptInfo
            //清空list3
            spweb.Lists.Delete(list3.ID);
            listTemplateCollection = spsite.GetCustomListTemplates(spweb);
            //列表库模板:可在Moss中将特定列表库另存为模板文件,位于站点中。
            template = listTemplateCollection["Dept"];
            ListLibGuid = spweb.Lists.Add("DeptInfo", "", template);
            list3 = spweb.Lists[ListLibGuid];
            //add
            foreach (DeptCollection dept in deptCollections)
            {
                SPListItem item1 = list3.Items.Add();
                item1["部门ID"] = dept.ID;
                item1["部门名称"] = dept.名称;
                item1["负责人ID"] = dept.负责人ID;
                item1["负责人姓名"] = dept.负责人姓名;
                item1["负责人登录名"] = dept.负责人登录名;
                item1["负责人EMail"] = dept.负责人EMail;
                item1.Update();
            }
            #endregion
        }

    }
}
----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SAPTimer
{
    class SAPDataTimerInstaller : SPFeatureReceiver
    {
        const string MY_TASK = "SAPDataTimer";

        /// <summary>
        /// 在功能被安装以后被调用
        /// </summary>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// 在功能被卸载的时候被调用
        /// </summary>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// 在功能被激活的时候被调用
        /// </summary>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // 取得当前站点的作用域
            SPSite site = properties.Feature.Parent as SPSite;

            SPWeb web = site.RootWeb;

            // 确保在安装此功能之前系统不被调用
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == MY_TASK)
                    job.Delete();
            }

            //安装TimerJob
            SAPDataTimer timer = new SAPDataTimer(MY_TASK, site.WebApplication);
            //设置记时器的工作计划表,在这里是每日启动一次,运行时间00:00-01:00,更多的Timer请参看SDK,最小是只执行一次,最长是一天
            SPDailySchedule schedule = new SPDailySchedule();
            schedule.BeginHour = 0;
            schedule.EndHour = 1;

            //每分钟
            //SPMinuteSchedule schedule = new SPMinuteSchedule();
            //schedule.BeginSecond = 0;
            //schedule.EndSecond = 59;
            //schedule.Interval = 1;

            //每小时
            //SPHourlySchedule schedule = new SPHourlySchedule();
            //schedule.BeginMinute = 40;
            //schedule.EndMinute = 45;
       
            timer.Schedule = schedule;
            timer.Update();
        }

        /// <summary>
        /// 在功能被冻结的时候被调用
        /// </summary>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            // 删除这个功能
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == MY_TASK)
                    job.Delete();
            }
        }
    }
}
在C盘features文件夹下新建和项目同名的文件夹,并新建feature.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature  Id="DC421BF5-6EAE-4d2b-A1E3-CF3B987DEC10"
          Title="SAPTimer;"
          Description="SAPimerInstaller;"
          Version="12.0.0.0"
          Scope="Site"
          xmlns="http://schemas.microsoft.com/sharepoint/"
          ReceiverAssembly="SAPTimer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a17149b5e6de8152"
          ReceiverClass="SAPTimer.SAPDataTimerInstaller">
</Feature>
经测试,对3个列表库共2000多条记录进行一次更新时间是58秒

你可能感兴趣的:(定时器)