RainyLin的专栏

未曾清贫难成人,不经打击老天真,从来英雄出炼狱,自古富贵入凡尘,醉生梦死谁成器,拓马长枪定乾坤。

原创 MOSS207开发webpart全过程收藏

Posted on 2006-09-27 09:19 Chatterley

WebPart对于学习和研究SharePoint的人,已经不是什么新鲜的概念了。但对于学习.net Framework的人,可能会发现在System.web.ui.webcontrols下会发现多了WebPart的踪影。WebPart作为WebControl中的一种,比WebControl更高级一些,它可以在线Drag and Drop,并可以在线设置它的属性。
以前利用VS2003开发WebPart,得安装WebPart Template For VSNet。当然也可以在VS2005下安装WebPart Template来开发WebPart。在这里我就不介绍用WebPart Template For VSNet来开发WebPart了。

在VS2005下就可以不用安装WebPart Template来开发WebPart。下面我具体介绍其过程:
1、创建 Web Control Library
首先浏览C# Project Templates,然后选择Web Control Library,输入“SampleControl”。
要开发WebPart用于SharePoint,就必须引用Microsoft.SharePoint.dll(必须是安装MOSS的服务器上的)。
最后添加引用如下图:


2、编写WebPart的代码
你开发的WebPart,根据自己开发的功能选择相应的SP(SharePoint)命名空间,具体命名空间,请参考SDK。
开发WebPart必须继承WebPart类。

//--------------------------------------------------------------------
// File: SimpleWebPart.cs
//
// Purpose: A sample Web Part that demonstrates how to create a basic
// Web Part.
//--------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;

namespace Xdian.WebParts.SampleControl
{
    
/// <summary>
    
/// This Web Part changes it's own title and implements a custom property.
    
/// </summary>

    [XmlRoot(Namespace = "MyWebParts")]
    
public class SimpleWebPart : WebPart
    
{
        
private const string defaultText = "hello";
        
private string text = defaultText;

        
// Declare variables for HtmlControls user interface elements.
        HtmlButton _mybutton;
        HtmlInputText _mytextbox;

        
// Event handler for _mybutton control that sets the
        
// Title property to the value in _mytextbox control.
        public void _mybutton_click(object sender, EventArgs e)
        
{
            
this.Title = _mytextbox.Value;
            
try
            
{
                
this.SaveProperties = true;
            }

            
catch
            
{
                Caption 
= "Error Could not save property.";
            }

        }


        
// Override the ASP.NET Web.UI.Controls.CreateChildControls 
        
// method to create the objects for the Web Part's controls.      
        protected override void CreateChildControls()
        
{
            
// Create _mytextbox control.
            _mytextbox = new HtmlInputText();
            _mytextbox.Value 
= "";
            Controls.Add(_mytextbox);

            
// Create _mybutton control and wire its event handler.
            _mybutton = new HtmlButton();
            _mybutton.InnerText 
= "Set Web Part Title";
            _mybutton.ServerClick 
+= new EventHandler(_mybutton_click);
            Controls.Add(_mybutton);
        }


        [Browsable(
true), Category("Miscellaneous"),
        DefaultValue(defaultText),
        WebPartStorage(Storage.Personal),
        FriendlyName(
"Text"), Description("Text Property")]
        
public string Text
        
{
            
get
            
{
                
return text;
            }


            
set
            
{
                text 
= value;
            }

        }


        
protected override void RenderWebPart(HtmlTextWriter output)
        
{
            RenderChildren(output);
            
// Securely write out HTML
            output.Write("<BR>Text Property: " + SPEncode.HtmlEncode(Text));
        }

    }

}

3、编写WebPart定义文件(.dwp)
这个文件是xml文件,任意文本文件更改其扩展名为.dwp就可以了。
我的WebPart的程序集文件为SampleControl.dll。命名空间为Xdian.WebParts.SampleControl。所以我的SampleControl.dwp文件如下:

<?xml version="1.0"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
   
<Assembly>SampleControl, Version=1.0.0.0, Culture=neutral,PublicKeyToken=0e79ac0ff7e9e2cb</Assembly>
   
<TypeName>Xdian.WebParts.SampleControl.SimpleWebPart</TypeName>
   
<Title>SampleWebPart</Title>
   
<Description>Chatterley Create The first WebPart</Description>
</WebPart>

4、生成强名称程序集合
在这点,我在VS2005下,没有找到解决方法,因为在C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin下没有sn.exe这个工具了。
我跑到VS2003的机子下,在cmd下浏览到VS2003下的bin目录。用sn.exe随机生成了一个key.snk。输入sn.exe -h可以看参数。然后在项目文件下的Assembly.cs下加入这么一行文字:
[assembly: AssemblyKeyFile(@"C:\key.snk")]
我是把key.snk放在C:盘下的。最后生成的程序集,可以通过sn.exe -Tp来参看其PublicKeyToken。我采用的方法是用gacutil.exe放入C:/Windows/Assembly下,然后看去属性中的PublicKeyToken。

最终生成的程序集和配置文件如下:


5、修改WebConfig。
我想大家肯定会在那么多的Config混乱,你就把它放入你想添加WebPart到的SharePoint网站下。如下我把它放如8000端口的网站下(某个GUID网站下)。

当然你也可以把你的WebPart放如全局上,那么你就得修改C:\Program Files\Common Files\Microsoft Shared\web server extensions下的相应文件(不推荐)。

在WebConfig下,添加如下的配置信息:

<SafeControls>
<SafeControl Assembly="SampleControl, Version=1.0.0.0, Culture=neutral,PublicKeyToken=0e79ac0ff7e9e2cb"     Namespace="Xdian.WebParts.SampleControl"     TypeName="*"  Safe="True"/>
</SafeControls>

6、添加WebPart到网站的WebPart Gallery中。
你必须要有网站管理员的权限。进入http://vsts01:8000/下的网站管理。在WebPart管理的列表中,点击“上载”。浏览到我的SampleControl.dwp,然后输入配置信息,上传。最后在WebPart Gallery显示一条信息如下:



7、把WebPart添加到网页显示。
进入编辑网页。找到自己的SampleControl,然后添加(和SPS2003的操作有所不同)。
最后终于把WebPart显示在自己的网站上,不过不要开发太多的WebPart,网站对WebPart的承受能力是有限的。一个网站大概可以承受100个WebPart左右。如果你的网站超过的话,那么你的网站速度会明显变慢。

 

发表于 @ 2007年08月29日 21:23:00|评论(loading...)

新一篇: Windows常用快捷键 | 旧一篇: SPQuer语法

用户操作
[即时聊天] [发私信] [加为好友]
RainyLin
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
RainyLin的公告

web counter
欢迎您的到来

目前专注 于项目管理,BI,WWF,WCF

FrankLin's .Net
  • QQ聊天:点击这里给我发消息

    公告

     欢迎光临我的博客,有事请留言


    online english

     


  • 文章分类
    收藏
    BI探索
    anchky
    aspnetx
    BI MAN
    DMMan(BI--DataMining)
    ExcelService
    hapyboy
    jinjiazz(MVP)
    microsheen
    Microsoft PerformancePoint Blog
    OLAP expert
    SQL优化
    头头脑脑数据仓库之路
    微软BI开拓者
    数据仓库之路
    旋风的BI blog
    生活就像一块巧克力(BI)
    Blog链接
    an_andy(MSSE)
    BlueFox
    c++ programmers
    Choral(Sharepoint)MicroTechEd Expert
    Exchange
    Exchange
    Exchange
    Exchange
    FreePark(Sharepoint&&RMS)--MCTS
    kaneboy(SharePoint & office Zealot)
    light169(office)
    light169(office)
    LoveCherry
    QuickLee
    SmartJavaer
    sps2007学习者
    sql blog link
    thread tech
    today truely
    VC Blog
    WorkFlowReference
    一网友
    为爱疯狂
    二人行(Thread)
    任宇(Excel)
    任宇(Excel)
    刘帝勇(Exchange开发)
    午夜化蝶
    天轰穿(vs-gridview)
    小山(js)
    山前客栈-sps
    嵌入式window embeded(马宁)
    希望就在明天 sps search code
    思路
    智能手机(Mobile)开发
    李德志
    漂泊雪狼
    细属Ajax
    老赵
    英年早肥的博客
    赏梅斋
    陆冷飞(倒Excel)
    龙少爷(RPService)
    常用技术工具链接
    MySql参考手册
    sharepoint inspector
    sharepoint tips utility pack
    sharepoint tips Utility pack
    SharepointInspector
    SPCAMLEditor
    u2u CAML QUERY BUILDER
    代码链接
    sps code reference
    AD批量导出
    Asynchronous Socket Communication Code
    css2.0教程
    CSS在线手册
    css模板下载
    css酷站欣赏
    div+css教程
    JavaScript效果
    Mobile开发快捷
    webpart webcast地址
    xhtml教程
    极地银狐--ajax tree
    好文收藏
    ASP.NET资料汇集
    Atlas学习手记
    c# .net2.0实战
    c#2.0和c#3.0中文资料
    ClientScript debugger
    DesignPattern精要
    NHibernate入门
    Sharepoint Designer
    SharePort Portal Server2003深入指南
    team edition for tester
    一步一步css布局实录
    一步一步SharePoint
    微软企业库学习宝典(RSS)
    智多星的控件开发好文
    技术论坛社区
    51CTO技术论坛(BI等数据库技术)
    CSDN
    CSDN测试圈
    http://blog.51cto.com/
    Microsoft.Connect(pps refer)
    moss site top100
    MSDN
    MSDN FullTextSearch Forum(Moss)
    msdn validation
    msdn2
    msdn范型介绍
    MVP LINK
    otec forum sps
    otec forum sps
    sharepoint
    Sharepoint Developermania
    SharePoint Fans
    Sharepoint Forum
    SharepointBlog
    sharepoint视频教程
    sharepooint
    sps技术体系分析
    Tolib.com(统录网)
    vc#多线程访问资源
    window中文站,微软技术社区
    三容技术论坛
    博客堂
    大富翁
    微软Visual Studio专区
    微软中国社区
    微软中文技术社区
    赏梅斋-技术讨论群
    技术朋友
    colinborrowman-foreigner moss expert
    Mr Tang-MOSS
    技术网站链接
    Ajax.net(Ajaxpro.net)
    APAC SharePoint Conference 2007
    asp.net
    Asp.net Ajax
    asp.net2.0 ajax extension
    asp酷
    beansoftware
    China XP极限编程
    Codesmith官方网
    css.html.javascript,xml.xpath搜索
    CSSVista
    Data & Object Factory
    DotNetJobs.Cn(Dotnet男孩社区)-office
    DotnetSpider
    dotnet男孩社区
    Exchange的好文
    ExtJs官方网(ajax相关)
    Hibernate官方网
    Microsoft Sharepoint template Site
    OfficeSharePointPro
    Openabc.net(AOP)
    program
    SEO资料站
    Sharepoint China
    Sharepoint Fans社区
    SharePoint Good Site
    Sharepoint Heathersolomon
    Sharepoint pedia
    sharepoint UserGroup and Blog
    SilverLight
    sqlserver developer center
    SqlServer2005链接(RS Tutorial)
    UMLChina
    www.845821.com
    ZDNET软件频道
    中国分布式计算论坛
    中国工作流论坛
    中国联盟程序员
    博客园-- sharepoint
    和讯博客
    微软net Tool样例
    技术文章前沿
    爆强ajaxwidgets
    网页特效观止
    俱乐部列表
    上海.net俱乐部(RSS)
    中山.net俱乐部
    兰州.net俱乐部
    内蒙古.net俱乐部
    北京.net俱乐部
    南京.net俱乐部
    南昌.net俱乐部
    厦门.net俱乐部
    哈尔滨.net俱乐部
    大连.net俱乐部
    天津.net俱乐部
    太原.net俱乐部
    安徽.net俱乐部
    广州.net俱乐部
    徐洲.net俱乐部
    成都.net俱乐部
    新疆.net俱乐部
    昆明.net俱乐部
    杭州.net俱乐部
    武汉.net俱乐部
    沈阳.net俱乐部
    济南.net俱乐部
    深圳.net俱乐部
    湖南.net俱乐部
    烟台.net俱乐部
    珠海.net俱乐部
    石家庄.net俱乐部
    福州.net俱乐部
    苏州.net俱乐部
    西安.net俱乐部
    邯郸.net俱乐部
    郑州.net俱乐部
    重庆.net俱乐部
    长春.net俱乐部
    软考好站
    中国系统分析员
    中国软考联盟
    云峰空间
    系统分析之窗
    老顽童网站
    苏酩的Blog空间(系统分析师)
    行业英语
    软件工程专家网
    软考在线
    生活百科
    相册制作
    陶埙
    玩转VC
    C++ Boost
    C++ Stands Committee
    vc Blog
    VC在线(SourceCode)
    VC知识库
    网友链接
    .net爱好者
    donetbar破解
    一网友
    一网友
    一网友js方法库
    一网友链接
    博客MM PG
    守望者
    技术链接
    苞米花--(.net控件破解,哈哈)
    心情驿站
    行业专家
    chengking(.NET MVP)
    ChengKing(ZhengJian)
    choral(MVP)
    Fabian(MVP)
    FREE PARK
    iditor (MVP)
    IE编程专家
    Jade Blog(PPS)
    Jan Tielen(MVP-Sharepoint)
    Joel Oleson's Sharepoint Land
    just2fun-sps
    KaneBoy
    Microsoft MVP
    Microtoby(MVP-sharepoint)
    Mike.h
    Mike.h
    Mobile.MVP
    MSPlus .net控件大师
    OntoLica
    outofthebox(socket)
    Patric(MVP)
    Point2Share (sharepoint wonderful)
    Reza (MVP Sharepoint)
    Ricardo Costa
    Rickie Lee
    sharepoint mvp stramit caml tool important
    sharepoint MVP站点列表
    Sharepoint2007
    Sharepoint乐土
    THIN(.NET MVP)
    VC++ Expert
    VSTS测试
    VS博客圈-CSDN
    wanghao - sharepoint
    WWFExpert
    大坡视频(3D)
    孟子E章
    将军(牛)
    小山
    张羿(CLR/Silverlight Developer)
    思归
    梅子
    海东的技术资料
    清清月儿
    相濡以沫-sps
    胡涛-sps
    葛涵涛
    蒋晟
    蓝狐
    蜾蜾俊
    邹建(sqlserver)
    长流支流(报表专家)
    陆其明(Video)
    优秀团队列表
    北京.net俱乐部
    存档
    软件项目交易
    Csdn Blog version 3.1a
    Copyright © RainyLin