`
wangyihust
  • 浏览: 433645 次
文章分类
社区版块
存档分类
最新评论

SharePoint 2003 WebPart 开发笔记

阅读更多
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>
    <assemblyBindingxmlns="urn:schemas-microsoft-com:asm.v1">
        <probingprivatePath="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 Part’s assembly和namespace作为一个Safecontrol。
下面将一个WebPart Assembly注册成一个SafeControl,编辑c:\inetpub\wwwroot\web.config文件,将下面的节点填加到<SafeControls>块中。
格式说明:
<!--
SafeControl Attributes:
    Assembly="[Assembly]" - The .NET assembly in which the control is contained. This attribute can also contain version, culture,_u97 ?nd 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.
-->

<SafeControl
   Assembly="SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def148956c61a16b"
   Namespace="MyWebParts"
   TypeName="*"
   Safe="True"
/>
注意:将你的Web Part’s assembly.替换为PublicKeyToken的值,要正确获得这个值,请使用sn.exe命令。
sn.exe -T c:\inetpub\wwwroot\bin\SimpleWebPart.dll
除了这种办法,还有一种就是将你的Assembly拷备到GAC下,查看它的属性,也能获得。
第五节 创建Web Part Definition 文件(.dwp)
一个非常简单的DWP文件为一个单独的WebPart完成了属性设置。通过dwp的导入,可以实现将web part在sharepoint中灵活托放。
       在一个Web part definition文件中,Assembly和TypeName是必须被设置的。
内容如下
<?xmlversion="1.0"?>
<WebPartxmlns="http://schemas.microsoft.com/WebPart/v2">
   <Assembly>AssemblyName(with no .dll extension), Version=VersionNumber, Culture=Culture, PublicKeyToken=PublicKeyToken</Assembly>
   <TypeName>WebPartNamespace.WebPartClassName</TypeName>
   <Title>DefaultWebPartTitle</Title>
   <Description>WebPartDescription</Description>
</WebPart>
第六节 向Web Part Page中导入你的Web Part
1. Open a Web Part Page on your server.
2. Click Modify My Page or Modify Shared Page (depending on whether you're in Personal View or Shared View) in the upper right corner of the page and click Add Web Parts from the menu.
Click Import.
3. Browse to the location of the SimpleWebPart.dwp file and click the Upload button. After uploading, the page will refresh and "My Simple Web Part" should be displayed under Imported Web Part.
4. Drag the icon next to "My Simple Web Part" to a zone on the Web Part Page.
5. Type some text in the text box, and then click Set Web Part Title to test the part.
第二章 创建一个Web Part的自定义用户属性
WebPart本身就已经内置了一部分基本的属性,如WebPart的高度,标题等。但当你需要对WebPart进行更复杂的设置时,本身自带的属性已经不能满足你的要求,这时,你就必须使用自定义属性了。
第一节 创建自定义用户属性
(一)修改XML Settings
除了一些特殊的要求外,你完全可以象在ASP.net的Web UserControl那样定义自定义控件。由于Web Part 的属性被用户定义,并且以XML形式保存在SharePoint storage System中,所以你必须作下面这些,才能将自定义的属性保存起来。
1. 添加System.Xml.dll的引用。
2. 在你定义的属性的同时,定义XML namespace属性。If you add the XML namespace at the root level, you are not required to assign XML namespaces at the property level. An XML namespace assigned at the property level will override the XML namespace assigned at the root level. The XmlRoot attribute has the following form:
[XmlRoot(Namespace="name of namespace")]
(二)创建属性的符加信息
在每一个Web Part自定义属性都需要你设置符加的方法,一个典型的属性设置如下
[AttributeDeclaration1, AttributeDeclaration2,...]
public PropertyType PropertyVariable
private PropertyType PrivatePropertyVariable
{
    get
{
return PrivatePropertyVariable;
}
set
{
    PrivatePropertyVariable = value;
}
这些你定义的属性将可以确定当用户使用你的WebPart时,它的存储方式以及个性化特性。
(三)设置自定义属性的属性
你设置的这些属性将在添加WebPart过程中的WebPart属性框中显示出你设置并存储的值。大部分的属性是System.ComponentModel namespace中提供的类,你可以在开发过程中引用和定义它们。有些属性,如WebPartStorage属性,它就是用来设置WebPart的属性用的。下面列举了属性及定义:
Attribute
Purpose
Browsable
Set to false if you don’t want to display the custom property in the property pane. Also, if you set the WebPartStorage attribute to Storage.None, your property won't display in the property pane.
Category
The title of the section of the property pane that you want created to display the custom property. If you don't specify the Category attribute or if you specify the Category attribute as "Default", your custom property is displayed in the Miscellaneous section of the property pane.
Note  If you specify one of the default categories, such as Appearance, your Category attribute setting is ignored, and your property is displayed in the Miscellaneous section.
DefaultValue
The default value of the custom property. Specifying the default value minimizes the Web Part's storage requirements by storing the property's value only if it is different from the default.
Description
The contents of the tool tip that appears when you pause the mouse pointer over the custom property in the property pane.
FriendlyNameAttribute
The caption displayed for the custom property in the property pane. If you don't specify this attribute, the actual property name will be displayed in the property pane.
ReadOnly
Set to true if you want the custom property to be read-only in the property pane.
WebPartStorage
Set to Storage.Shared to display the custom property in the property pane when the user is in shared view of the page. Set to Storage.Personal to display the custom property in the property pane when the user is in Shared or Personal view of the page. Set to Storage.None if you don’t want the setting to persist for the custom property. The custom property won’t be displayed in the property pane.
HtmlDesignerAttribute
Used to associate a property builder with the property.
(四)一个简单的Web Part中包含用户自定义属性的例子
下面的这个例子将演示如何定义下面的自定义属性: string, bool, int, float, enum, System.DateTime, and System.Drawing.KnownColor.
//--------------------------------------------------------------------
// File : WebPartCustomProperties.cs
//
// Purpose : A sample Web Part that implements custom properties
//       of the following types: string, bool, int, float, enum,
//       System.DateTime, and System.Drawing.KnownColor
//
//           After building and installing this Web Part, display
//           the property pane to see how the user interface
//           for setting their values is rendered.
//---------------------------------------------------------------------
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;
namespace WebPartLibrary2
{
    ///<summary>
    /// Summary description for CustomPropertyWebPart.
    ///</summary>
    [DefaultProperty("Text"),
    ToolboxData("<{0}:CustomPropertyWebPart runat=server></{0}:CustomPropertyWebPart>"),
    XmlRoot(Namespace="WebPartLibrary2")]
    public class CustomPropertyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        const string c_MyStringDefault = "Sample String";
        const bool c_MyBoolDefault = false;
        const int c_MyIntDefault = 20;
        const float c_MyFloatDefault = 33.33f;
        public enum myFarmEnum
        {
            barn=0,
            tractor,
            hay,
            pitchfork
        };
        protected myFarmEnum _myEnum;
        // Private variables
        private string _myString;
        private bool _myBool;
        private int _myInt;
        private float _myFloat;
        private System.DateTime _myDateTime;
        private System.Drawing.KnownColor _myColor =
            System.Drawing.KnownColor.Red;
        // Constructor
        public CustomPropertyWebPart()
        {
            // Initialize private variables.
            _myString = c_MyStringDefault;
            _myBool = c_MyBoolDefault;
            _myInt = c_MyIntDefault;
            _myFloat = c_MyFloatDefault;
            _myEnum = myFarmEnum.hay;
            _myDateTime = System.DateTime.Now;
        }
        // Creates a custom property that is a string.
        // This property will be displayed as a text box in the
        // property pane.
        // Create a custom category in the property sheet.
        [Category("Custom Properties")]
            // Assign the default value.
        [DefaultValue(c_MyStringDefault)]
            // Property is available in both Personalization
            // and Customization mode.
        [WebPartStorage(Storage.Personal)]
            // The caption that appears in the property sheet.
        [FriendlyNameAttribute("Custom String")]
            // The tool tip that appears when pausing the mouse pointer over
            // the friendly name in the property pane.
        [Description("Type a string value.")]
            // Display the property in the property pane.
        [Browsable(true)]
        [XmlElement(ElementName="MyString")]
            // The accessor for this property.
        public string MyString
        {
            get
            {
                return _myString;
            }
            set
            {
                _myString = value;
            }
        }
        // Creates a custom property that is a Boolean value.
        // This property will display as a check box in the
        // property pane.
        [Category("Custom Properties")]
        [DefaultValue(c_MyBoolDefault)]
        [WebPartStorage(Storage.Personal)]
        [FriendlyNameAttribute("Custom Boolean")]
        [Description("Select to set value to True.")]
        [Browsable(true)]
        [XmlElement(ElementName="MyBoolean")]
            // The accessor for this property.
        public bool MyBool
        {
            get
         &nb
分享到:
评论

相关推荐

    sharepoint webpart 开发 SmartPart

    SharePoint WebPart 开发是构建 SharePoint 应用程序的核心部分,SmartPart 是一个非常有用的工具,它简化了 WebPart 开发的过程。SmartPart 允许开发者通过创建 UserControl 来快速构建 SharePoint WebParts,这与...

    Sharepoint webpart 自定义开发(很详细)

    WebPart 的自定义开发是 SharePoint 开发中的核心技能,可以帮助企业根据特定需求定制化工作环境。 首先,我们需要了解WebPart的基础知识。WebPart是SharePoint页面上的可视化单元,它可以显示数据、执行功能或提供...

    SharePoint WebPart 用户控件包装器

    **用户控件包装器**是一种辅助 SharePoint 开发者的工具,它能显著简化将 ASP.NET 用户控件转换为可在 SharePoint Portal Server 2003 或 Windows SharePoint Services 2.0 中使用的 WebPart 的过程。通过这种方式,...

    有关SharePoint WebPart的一些文档笔记

    **SharePoint WebPart详解** SharePoint WebPart是微软SharePoint平台中的核心组件,它是一种可重用、可配置且可...通过熟练掌握WebPart,开发人员可以构建出满足各种业务需求的高效、灵活的SharePoint解决方案。

    SharePoint_WebPart_入门指南(全)

    **SharePoint WebPart 入门指南**...通过学习以上内容,并结合提供的PDF和PPT资源,你可以深入了解SharePoint WebPart的基本原理和实践技巧,逐步掌握WebPart的开发和管理,从而更好地利用SharePoint构建高效协作环境。

    SharePoint 2013 WebPart Demo

    在 "SP2013WebPart" 压缩包中,很可能是包含了一个或多个示例 WebPart 的源代码和部署文件,用于帮助用户理解和实践 SharePoint 2013 中的 WebPart 开发。 1. **WebPart 基础** - **定义**: WebPart 是 SharePoint...

    SharePoint AJAX webpart

    SharePoint AJAX WebPart是开发SharePoint应用程序时常用的一种技术,它允许用户在不刷新整个页面的情况下更新部分网页内容,提供更流畅的用户体验。这个自定义的SharePoint.Ajax.Library是为了解决SharePoint内置...

    关于sharepoint 2010 webpart 编程的几个例子

    关于 SharePoint 2010 WebPart ...SharePoint 2010 WebPart 编程需要了解 WebPart 生命周期、事件处理和开发步骤等内容,通过这些知识点,开发者可以创建功能强大且灵活的自定义控件,提高 SharePoint 站点的业务价值。

    SharePoint 开发实录:5,WebPart 发布

    在SharePoint开发中,WebPart是一种关键的组件,它允许开发者构建可重用的、自定义的用户界面元素,这些元素可以直接嵌入到SharePoint页面上。本篇将深入探讨WebPart的发布流程及其相关技术。 首先,WebPart是...

    vs.2003webpart开发

    VS.2003 WebPart 开发主要涉及的是在 Microsoft SharePoint Services (SPS) 平台上创建自定义 Web 部件的过程。WebPart 是 SharePoint 中的基本构建块,允许用户通过组合和定制不同的功能模块来构建自己的页面。以下...

    SharePoint WebPart入门教程 PPT

    SharePoint WebPart是微软 SharePoint 平台上的一个重要组成部分,它允许开发者和用户自定义和增强 SharePoint 网站的功能和用户体验。本入门教程PPT将引导我们深入理解WebPart的概念、工作原理以及如何创建和使用...

    如何在sharepoint2013里制作自定义webpart

    在SharePoint 2013中制作自定义WebPart是一项重要的开发任务,它允许你根据组织的需求定制工作区,提供个性化的用户体验。WebPart是SharePoint中的可重用组件,可以独立开发并添加到页面中,以展示内容、提供交互...

    sharepoint webpart 2

    开发SharePoint WebPart通常涉及以下步骤: 1. **选择WebPart类型**:可以基于现有控件创建,如ASP.NET服务器控件,或自定义开发。 2. **实现WebPart类**:继承自`System.Web.UI.WebControls.WebParts.WebPart`或...

    Microsoft Office Sharepoint Server 2007开发系列课程(3):MOSS2007之WebPart开发

    在本课程中,我们将深入探讨Microsoft Office SharePoint Server (MOSS) 2007的WebPart开发,这是MOSS2007开发系列课程的第三部分。WebPart是SharePoint平台的核心组件之一,它们提供了构建高度自定义和交互式用户...

    SharePoint开发之WebPart说明.pdf

    根据提供的文件信息和部分内容,我们可以从中提取出关于SharePoint开发中WebPart组件的相关知识点。 首先,提到的Microsoft Office SharePoint Server 2007是微软公司推出的一款企业级内容管理及协作平台。该平台的...

    webpart开发学习资料

    【标题】"WebPart开发学习资料"涉及到的是在Web开发领域中的一个重要概念——WebPart,它是微软 SharePoint 平台上的核心组件之一,用于构建高度可定制的交互式Web应用程序。WebPart是SharePoint中实现模块化开发的...

    sharepoint 2010 扩展webpart自定义属性边栏字段

    当我们需要更高级的自定义功能,比如扩展边栏字段,来增强Web Part的灵活性时,就需要深入到SharePoint的开发层面。下面,我们将详细探讨如何在SharePoint 2010中扩展Web Part自定义属性,以实现边栏字段的定制。 1...

    sharepoint二次开发

    7. WebPart 的开发:WebPart 的开发需要了解 SharePoint 的组件模型和业务逻辑,可以通过 Visual Studio 等开发工具来开发和部署 WebPart。 8. SharePoint 的配置文件:SharePoint 的配置文件包括 web.config 和 ...

Global site tag (gtag.js) - Google Analytics