利用Feature控制EventHandle

笔者在前面的篇章已经分别介绍了定制一个简单的EventHandleFeature,相信大家对这些也已不再陌生了!如果还有不明白可以回头在复习下。本篇章笔者将把这两者结合起来应用让初学者更加明白这两者的关系,同时也希望对大家有所帮助。

前期准备1)打开VS建立一个空白解决方案,笔者在上面添加两个类库项目,一个取名为Meeting,另一个取名为UseFeatureRunMeeting。然后分别添加1.Sharepoint.dll组建在两个项目属性分别设置2.设置强名称集,3.生成后事件:copy "$(TargetDir)*.dll" C:\WINDOWS\assembly.4.在项目UseFeatureRunMeeting创建一个Features文件夹,在该文件夹下创建一个MyFeature子文件,然后在里面添加一个XML文件命名为Feature.xml.项目结构如下图:

利用Feature控制EventHandle

准备(2)建立一个数据便Meeting

利用Feature控制EventHandle

准备(3)在自己指定的站点创建一个列表为日常会议通知,然后把进入列表设置把会议标题设成可以不要求此栏包含信息

利用Feature控制EventHandle

 最后添加一条项目,此时标题就为空 如图:

利用Feature控制EventHandle

 

准备工作完成,步入正题:

STEP1.

MyMeeting.cs项目代码

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using System.Data.SqlClient;

 

namespace Meeting

//继承列表事件

    public class MyMeeting:SPItemEventReceiver

    {

         //建立一个数据连接

        public SqlConnection connectiongstring()

        {

            SqlConnection con = new SqlConnection("Data source=MOSINGSERVER;Database=MeetingDate;integrated security=true");

            con.Open();

            return con;

 

        }

       //重写列表事件接口的增删改的3个虚方法

        public override void ItemAdded(SPItemEventProperties properties)

        {

            this.ItemEvent(properties);

        }

        public override void ItemUpdated(SPItemEventProperties properties)

        {

            this.ItemEvent(properties);

        }

        public void ItemEvent(SPItemEventProperties properties)

        {

            SPListItem listitem = properties.ListItem;

            int MeetingID = Convert.ToInt32(listitem.ID);

            string Dept = listitem["部门"].ToString();

            string Meetingadress = listitem["会议地点"].ToString();

            string MeetingTime =Convert.ToDateTime(listitem["会议时间"]).ToString();

            listitem["会议标题"] = "请" + Dept + "部门,于" + "(" + MeetingTime + ")" + "在" + Meetingadress + "开会";

            listitem.Update();

            string MeetingTitle = listitem["会议标题"].ToString();

            SqlConnection con = connectiongstring();

            SqlCommand cmd = new SqlCommand("select MeetingID from Meeting where MeetingID='"+MeetingID+"'", con);

            if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)

            {

                cmd = new SqlCommand("update Meeting set MeetingTitle='" + MeetingTitle + "',Dept='" + Dept + "',Meetingadress='" + Meetingadress + "',MeetingDate='" + Convert.ToDateTime(MeetingTime) + "'where MeetingID='"+MeetingID+ "'", con);

                cmd.ExecuteNonQuery();

                con.Close();

 

            }

            else

            {

                cmd = new SqlCommand("insert into Meeting(MeetingTitle,Dept,Meetingadress,MeetingDate)values('" + MeetingTitle + "','" + Dept + "','" + Meetingadress + "','" + Convert.ToDateTime(MeetingTime) + "')", con);

                cmd.ExecuteNonQuery();

                con.Close();

            }

        }

     

        public override void ItemDeleted(SPItemEventProperties properties)

        {

            SPListItem listitem = properties.ListItem;

            int MeetingID =Convert.ToInt32(listitem.ID);

            SqlConnection con = connectiongstring();

            SqlCommand cmd = new SqlCommand("delete from Meeting where MeetingID='"+MeetingID+"'", con);

            cmd.ExecuteNonQuery();

            con.Close();

            listitem.Update();

        }

 

      

 }

}

 

Feature.cs:代码

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

 

namespace UseFeatureRunMeeting

{   //继承Festure事件接口,点下该接口右边实现虚方法重写

    public class Feature:SPFeatureReceiver

    {

 

        //激活Feature时触发列表增删改事件

        public override void FeatureActivated(SPFeatureReceiverProperties properties)

        {

            //throw new Exception("The method or operation is not implemented.");

            SPWeb web = (SPWeb)properties.Feature.Parent;

            web.AllowUnsafeUpdates = true;

            SPList list=web.Lists["日常会议通知"];

            string AseemblyName = "Meeting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=448b0ec450fe56a0";

            string ClassName ="Meeting.MyMeeting";

            list.EventReceivers.Add(SPEventReceiverType.ItemAdded,AseemblyName,ClassName);

            list.EventReceivers.Add(SPEventReceiverType.ItemUpdated,AseemblyName,ClassName);

            list.EventReceivers.Add(SPEventReceiverType.ItemDeleted,AseemblyName,ClassName);

            //web.Close();

        }

        //关闭Feature时,清楚列表增删改事件

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            //throw new Exception("The method or operation is not implemented.");

            SPWeb web = (SPWeb)properties.Feature.Parent;

            web.AllowUnsafeUpdates = true;

            SPList list = web.Lists["日常会议通知"];

            for (int i = list.EventReceivers.Count - 1; i >= 0;i--)

            {

                list.EventReceivers[i].Delete();

            }

        }

 

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)

        {

            //throw new Exception("The method or operation is not implemented.");

        }

 

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

        {

            //throw new Exception("The method or operation is not implemented.");

        }

    }

}

利用Feature控制EventHandle

以上就是本次项目的代码,之后执行项目生成,然后记得IISRESET,然后把MyFeature这个文件和里面的内容复制到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

STEP2

执行批处理:

利用Feature控制EventHandle

在这里笔者强调下,激活某个Feature时,需给它制定具体的一个站点地址。一切都OK了!

激活完作者去刚才通知列表重建一条通知项目,注意笔者没写标题,点确定

利用Feature控制EventHandle

 返回数据库看下,多了条记录;

利用Feature控制EventHandle

利用Feature控制EventHandle

OK 到此这一章节就完了!笔者讲的不详细,但会努力去学好的!希望大家都都包含新人,更希望更我一样的初学者看到我写的能对你们有一些用处。

 

你可能感兴趣的:(event)