现在的位置: 首页 > 综合 > 正文

[SPS]SharePoint 2003 WebPart 开发笔记

2011年12月08日 ⁄ 综合 ⁄ 共 4179字 ⁄ 字号 评论关闭

SharePoint 2003 WebPart 开发笔记

第一章 开发一个Web Part Project

第一节 新建一个Web Part Library project 项目

1.在项目中填加对Microsoft.SharePoint.dll的引用

       a)如果你在Microsoft.SharePoint server上开发,可以在引用中直接找到。

       b)如果没有,那就要将Microsoft.SharePoint.dll文件copy到你的目录中,加以引用。

2.填加System.Xml的引用。

第二节 在开发以前的准备工作

1.指定Build output path

       a) 指定到Assembly所在目录。如:C:\inetpub\wwwroot\bin

       b) 想发布GAC(C:\windows\assembly),只能用copy .net 命令gacUtil.exe注册。

       c) 发布到其它目录中(for example, \inetpub\wwwroot\mydir or mydir2\bin),这样,你需要修改web.config文件,在<configuration>中加入下面的节点

<runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

        <probing privatePath="mydir;mydir2\bin"/>

    </assemblyBinding>

</runtime>

 

2.设置版本号

       a) 打开文件AssemblyInfo.cs,修改成

[assembly: AssemblyVersion("1.0.0.0")]

 

3.为装配件设置强名

       WebPart是在Internet or Intranet上开发设计的,为了在创建WebPart时的安全原因,你必须用强名来指定你的WebPart,这样用户才能准确的打到你提供的WebPart

       a) 生成一个强名文件Sn.exe –k c:\keypair.snk

       b) 填加强名引用

[assembly: AssemblyKeyFile("c:\\keypair.snk")]

 

第三节 代码开发

假定你是使用WebPart Library进行开发的。

(一)定义Toolbox data

设置类的属性值。在类声明前加上对该类的属性设置。

[ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>")]    // 注意其中Target的定义与类名相同

public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart{

}

 

(二)定义XML namespace

如果想成功导入用户自定义WebPart,你必须为每一个WebPart设置XML namespace属性。这个动作是在Web Part Class 定义的时候完成的。

[XmlRoot(Namespace="MyWebParts")]

 

(三)逻辑实现

//--------------------------------------------------------------------

// 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 MyWebParts

{

    /// <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));   

        }

    }

}

 

 

注意:默认情况下,信认级别设置成WSS_Minimal,它不能访问SharePoint object model。为了能设置 SaveProperties 属性,你必须执行下面三个动作。

       a) Create a custom policy file for your assembly.

       b) Install your assembly in the global assembly cache.

       c) Increase the trust level for the entire virtual server.

 

第四节 配置您 Web part

(一)注册你的Web Part 作为一个SafeControl

作为一个安全办法,Windows SharePoint Services 要求你在Web.Config文件中注册Web Parts assemblynamespace作为一个Safecontrol

抱歉!评论已关闭.