一步一步webpart-如何利用.net制作第一个webpart?(3)

首先,你已经装好了sharepoint 2003 ,有关sharepoint 2003 安装问题请见我博客的另一篇文章:sharepoint 安装宝典
         另,有很多网友想下载 sharepoint 2003,我用的是公司的.若有知道下载地址的朋友,不妨提供给广大需要者.
开始制作第一个webpart!
用VS.net创建两个项目:
一. 创建"  Web项目":WebProject1
      添加一个“Web用户控件":WebUserControl1.ascx,去掉 WebUserControl1.ascx的"CodeBehind”和“Inherited”等属性代码如下:
<%@ Control Language="c#" AutoEventWireup="false" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Calendar id="Calendar1" runat="server">
</asp:Calendar>

  
二. 创建 "Web Part Library项目:WebPartLibrary1
    在装好webpart后,打开.net即可看到web part Library如图所示:

WebPart1.cs的代码如下:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartLibrary1
{
    
/// <summary>
    
/// Description for WebPart1.
    
/// </summary>

    [DefaultProperty("Text"),
        ToolboxData(
"<{0}:WebPart1 runat=server></{0}:WebPart1>"),
        XmlRoot(Namespace
="WebPartLibrary1")]
    
public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
    
{
        
一.在WebPart1类中定义一个用来保存第一步中创建的UserControl的对象
        
private const string defaultText = "";

        
private string text = defaultText;

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


            
set
            
{
                text 
= value;
            }

        }

        
        
/// <summary>
        
///    This method gets the custom tool parts for this Web Part by overriding the
        
///    GetToolParts method of the WebPart base class. You must implement
        
///    custom tool parts in a separate class that derives from 
        
///    Microsoft.SharePoint.WebPartPages.ToolPart. 
        
///    </summary>
        
///<returns>An array of references to ToolPart objects.</returns>

//        public override ToolPart[] GetToolParts()
//        {
//            ToolPart[] toolparts = new ToolPart[2];
//            WebPartToolPart wptp = new WebPartToolPart();
//            CustomPropertyToolPart custom = new CustomPropertyToolPart();
//            toolparts[0] = wptp;
//            toolparts[1] = custom;
//            return toolparts;
//        }
        二.重载WebPart1的父类的CreateChildControls()方法,在其中载入第一步创建的UserControl:
        
三.在RenderWebPart()方法中输出载入的UserControl:
    }

}


  配置WebPart,即修改 WebPart1.dwp 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
    
<Title>李燕平的WebPart1</Title>
    
<Description>第一个webpart</Description>
    
<Assembly>WebPartLibrary1</Assembly>
    
<TypeName>WebPartLibrary1.WebPart1</TypeName>
    
<!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>
说明:
Title : 显示在SPS页面上的标题
Description : 显示在SPS页面上的提示文字
Assembly : 编译出来的dll文件的文件名(勿加.dll
TypeName : 完整的WebPart的类名称(包含Namespace
 三.打开承载SPS站点的虚拟主机的根目录,编辑web.config文件
      可能有的朋友不知道这个目录在那,请见图示:

web.config文件原码:
<configuration>
  
<configSections>
    
<sectionGroup name="SharePoint">
      
<section name="SafeControls" type="Microsoft.SharePoint.ApplicationRuntime.SafeControlsConfigurationHandler, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
      
<section name="RuntimeFilter" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="WebPartLimits" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="WebPartCache" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="WebPartWorkItem" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="WebPartControls" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="SafeMode" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
<section name="OnlineLibrary" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    
</sectionGroup>
  
</configSections>
  
<SharePoint>
    
<SafeMode MaxControls="50" CallStack="false" />
    
<WebPartLimits MaxZoneParts="50" PropertySize="1048576" />
    
<WebPartCache Storage="CacheObject" />
    
<WebPartWorkItem Timeout="7000" />
    
<WebPartControls DatasheetControlGuid="65BCBEE4-7728-41a0-97BE-14E1CAE36AAE" />
    
<!--
SafeControl Attributes:
          Assembly="[Assembly]" - The .NET assembly in which the control is contained. This attribute can also contain version, culture, and public key token information.
          Namespace="[Namespace]" - The .NET namespace in which the control is defined.                                         
          TypeName="[Typename]" - The .NET class name of the control. You can type an asterisk (*) wildcard character to indicate all TypeNames in a Namespace. 
          Safe="[True|False]" - Specifies whether a Web Part or Web Form Control is safe and can be displayed on a Web Parts Page. This attribute is True by default.
-->
    
<SafeControls>
      
<SafeControl Assembly="System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="System.Web.UI.WebControls" TypeName="*" Safe="True" />
      
<SafeControl Assembly="System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="System.Web.UI.HtmlControls" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebPartPages" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.ApplicationPages" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.SoapServer" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Meetings" TypeName="*" Safe="True" />
      
<SafeControl Assembly="Microsoft.SharePoint.Portal, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Portal.WebControls" TypeName="*" />
      
<SafeControl Assembly="Microsoft.SharePoint.Portal, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Portal.WebControls.Alerts" TypeName="*" />
      
<SafeControl Assembly="Ocean.WebParts.ImageViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a2c9a30ebbc2e76e" Namespace="Ocean.WebParts" TypeName="*" Safe="True" />
      
<!--注意1:下面的 <SafeControl Assembly="WebPartLibrary1" Namespace="WebPartLibrary1" TypeName="*" Safe="True" />是手工写的-->
      
<SafeControl Assembly="Ocean.WebParts.TimeShow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=54ab91e7bd3d5c44" Namespace="Ocean.WebParts" TypeName="*" Safe="True" />
      
<SafeControl Assembly="WebPartLibrary1" Namespace="WebPartLibrary1" TypeName="*" Safe="True" />
    
</SafeControls>
    
<OnlineLibrary Url="http://r.office.microsoft.com/r/hlidAwsGallery" />
    
<RuntimeFilter Assembly="Microsoft.SharePoint.Portal, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Class="Microsoft.SharePoint.Portal.Audience.AudienceManager" BuilderURL="audience_chooser.aspx" />
  
</SharePoint>
  
<system.web>
    
<securityPolicy>
      
<trustLevel name="WSS_Medium" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_mediumtrust.config" />
      
<trustLevel name="WSS_Minimal" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\config\wss_minimaltrust.config" />
    
</securityPolicy>
    
<httpHandlers>
      
<add verb="*" path="/_vti_bin/*.aspx" type="System.Web.UI.PageHandlerFactory, System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      
<add verb="*" path="*.aspx" type="Microsoft.SharePoint.ApplicationRuntime.SharePointHandlerFactory, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
    
</httpHandlers>
    
<customErrors mode="On" />
    
<httpRuntime maxRequestLength="51200" />
    
<authentication mode="Windows" />
    
<authorization>
      
<allow users="*" />
    
</authorization>
    
<identity impersonate="true" />
    
<httpModules>
      
<clear />
      
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
      
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
      
<!--    <add name="Session" type="System.Web.SessionState.SessionStateModule"/>-->
    
</httpModules>
    
<globalization fileEncoding="utf-8" />
    
<compilation batch="false" debug="false" />
    
<pages enableSessionState="false" enableViewState="true" enableViewStateMac="true" validateRequest="false" />
     
<!--注意2:我把WSS_Minimal改成了WSS_Medium-->
    
<trust level="WSS_Medium" originUrl="" />
    
<machineKey validationKey="0C99877D609FD63BC48BBB8752DE0C3276A2DB30516ECEA2" decryptionKey="A8B47233EC5BFA84B7644ED6591B597C666CB43E72288192" validation="SHA1" />
  
</system.web>
</configuration>
说明:
      1.<safecontrol...>中的信息要和WebPart1.dwp 一致
       2.<SafeControl Assembly="WebPartLibrary1" Namespace="WebPartLibrary1" TypeName="*" Safe="True" />不要写成
          <SafeControl Assembly="WebPartLibrary1" Namespace="WebPartLibrary1.WebPart1" TypeName="*" Safe="True" />
四.导入创建的webPart到SPS中
导入之前需要将刚才创建的“WebUserControl1.ascx”和“WebPartLibrary1.dll”拷贝到承载SPS站点的虚拟主机的根目录下的“bin”目录下;
导入过程:
1.点击门户网站上的"网站设置";
2.点击"常规设置"下的"管理安全性和附加设置";
3.点击"模板和 Web 部件"下的"管理 Web 部件库";
4.点击"上载 Web 部件";
5.浏览到"WebPart1.dwp",选择"默认web部件"组,保存!OK了,本节结束!!
posted @ 2006-09-20 18:35  青羽  阅读(2283)  评论(2编辑  收藏  举报