如何开发部署Feature

Featrue的开发与部署以前没做过,需要的时候做了一次感觉不是很难,下面就是一个简单的Feature部署EventHandler来控制列表里的一列在添加和修改时保持唯一。

①先建立类库写Feature其代码如下;

ContractedBlock.gifExpandedBlockStart.gifCode
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6using Microsoft.SharePoint;
 7
 8namespace BSpaceFeature
 9ExpandedBlockStart.gifContractedBlock.gif{
10    public class BSpaceFT:SPFeatureReceiver 
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        public override void FeatureActivated(SPFeatureReceiverProperties properties)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
14            SPWeb web = (SPWeb)properties.Feature.Parent;
15            web.AllowUnsafeUpdates = true;
16            SPList list=web .Lists ["客户管理"];
17            string assembly = "BSpaceEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4051e4af7309362c";
18            string className = "BSpaceEventHandler.BSpaceEHD";
19            list.EventReceivers.Add(SPEventReceiverType.ItemAdding, assembly, className); 
20            list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assembly, className);
21        }

22        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        {           
24        }

25        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        {
27        }

28        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
29ExpandedSubBlockStart.gifContractedSubBlock.gif        {
30        }

31    }

32}

② 在同一解决方案下面建立类库 来写EventHandler其代码如下;

ContractedBlock.gifExpandedBlockStart.gifCode
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6using Microsoft.SharePoint;
 7
 8namespace BSpaceEventHandler
 9ExpandedBlockStart.gifContractedBlock.gif{
10    public class BSpaceEHD:SPItemEventReceiver 
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        public override void ItemAdding(SPItemEventProperties properties)
13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
14            SPWeb web = properties.OpenWeb();
15            SPList list = web.Lists["客户管理"];
16            SPListItemCollection lists = list.Items;
17            string content = "";
18            using (SPWeb web1 = (properties.OpenWeb()) as SPWeb)
19ExpandedSubBlockStart.gifContractedSubBlock.gif            {
20                SPList List = web1.Lists[properties.ListTitle] as SPList;
21                string sNameTitle = List.Fields["公司名称"].InternalName;
22                content = properties.AfterProperties[sNameTitle].ToString();
23            }

24            string str = "";
25            foreach (SPListItem splist in lists)
26ExpandedSubBlockStart.gifContractedSubBlock.gif            {
27                if (content.Equals(splist["公司名称"].ToString()))
28ExpandedSubBlockStart.gifContractedSubBlock.gif                {
29                    str = splist["公司名称"].ToString();
30                    properties.Cancel = true;
31                    properties.ErrorMessage = "公司名称:" + str + "已经存在,您不能添加!请联系管理员!";
32                    break;
33                }

34            }

35        }

36        public override void ItemUpdating(SPItemEventProperties properties)
37ExpandedSubBlockStart.gifContractedSubBlock.gif        {
38            SPWeb web = properties.OpenWeb();
39            SPList list = web.Lists["客户管理"];
40            SPListItemCollection lists = list.Items;
41            SPListItem item = properties.ListItem;
42            string content = "";
43            string strId = item["ID"].ToString();
44            using (SPWeb web1 = (properties.OpenWeb()) as SPWeb)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
46                SPList list1 = web1.Lists[properties.ListTitle] as SPList;
47                string sNameTitle = list1.Fields["公司名称"].InternalName;
48                content = properties.AfterProperties[sNameTitle].ToString();
49            }

50            foreach (SPListItem splist in lists)
51ExpandedSubBlockStart.gifContractedSubBlock.gif            {
52                if (content.Equals(splist["公司名称"].ToString()))
53ExpandedSubBlockStart.gifContractedSubBlock.gif                {
54                    if (!(strId.Equals(splist["ID"].ToString())))
55                        properties.Cancel = true;
56                    properties.ErrorMessage = "公司名称:" + splist["公司名称"].ToString() + "已经存在!请联系管理员!";
57                }

58            }

59        }
   
60    }

61}

③在Feature类库中建立Feature文件夹,在Feature文件夹下建立Feature.xml文件,如下;

ContractedBlock.gifExpandedBlockStart.gifFeature.xml
 1<?xml version="1.0" encoding="utf-8" ?>
 2<Feature  xmlns="http://schemas.microsoft.com/sharepoint/" 
 3          Id="27E28022-CBA3-470d-86BB-AC70E0F83B6A" 
 4         Title="客户管理" 
 5         Description="use Feature run EventHandler控制公司名称唯一" 
 6         Hidden="False" 
 7         Scope="Web"
 8         ReceiverClass="BSpaceFeature.BSpaceFT"
 9         ReceiverAssembly="BSpaceFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d7b283ecb74bbf80"> 
10</Feature>

④将Feature文件夹(其中包含feature.xml文件)拷贝到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES文件夹下;

⑤在“开始”->运行 cmd 以后输入 cd  C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN之后会进入BIN目录下然后在输入STSADM -o InstallFeature -filename Feature\Feature.xml -force来部署Fatrure

⑥在将写的Feature类生成的BSpaceFeature.dll和EventHandler类生成的BSpaceEventHandler.dll拖到GAC里面;
⑦重启iis即iisreset
⑧Feature部署完毕,在网站设置里的网站功能里面就会出现您所部署的Feature,一激活就OK了~!

注意:如果要卸载Feature只需要在⑤步里STSADM -o unInstallFeature -filename Feature\Feature.xml -force就搞定了~!去爽吧~!

转载于:https://www.cnblogs.com/Employee/archive/2009/06/11/1501200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值