zenmbu 发表于 2015-9-29 08:55:48

SharePoint 应用的开发学习笔记(二)

  使用Visual Studio.NET 开发Web Parts(Web部件)

对SharePoint进行扩充的一个最好的方法就是开发自己的Web部件,但是在开发Web部件之前首先要下载Web Part Templates for Visual Studio .NET:(http://www.microsoft.com/downloads/details.aspx?FamilyId=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en)。

Web部件与普通的asp.net自定义控件没有什么区别,如果你具有开发asp.net自定义控件的经验的话,开发Web部件就轻车熟路了。
下面是我自己开发的一个很简单的Web部件,如图右下角的SimpleWebPart(实现了简单的加法运算):



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 MyWebParts
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty(&quot;Text&quot;),
ToolboxData(&quot;<{0}:WebPart1 runat=server></{0}:SimpleWebPart>&quot;),
XmlRoot(Namespace=&quot;MyWebParts&quot;)]
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
protected TextBox txtValue1;
protected TextBox txtValue2;
protected TextBox txtResult;
  protected Label lblOp;
protected Label lblEq;
protected Button btnCalc;
  //private const string defaultText = &quot;&quot;;
  //private string text = defaultText;
  //[Browsable(true),
//   Category(&quot;Miscellaneous&quot;),
//   DefaultValue(defaultText),
//   WebPartStorage(Storage.Personal),
//   FriendlyName(&quot;Text&quot;),
//   Description(&quot;Text Property&quot;)]
//public string Text
//{
//   get
//   {
//    return text;
//   }
//
//   set
//   {
//    text = value;
//   }
//}

private bool calcEnabled = true;
[Browsable(true),
   Description(&quot;是否启用计算功能!&quot;),
   WebPartStorage(Storage.Personal),
   FriendlyName(&quot;允许计算&quot;),
   DefaultValue(true),
   Category(&quot;自定义&quot;)
]
public bool CalcEnabled
{
   get
   {
    return calcEnabled;
   }
   set
   {
    calcEnabled = value;
    if( this.btnCalc != null )
    {
   this.btnCalc.Enabled = 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;
//   WebPartToolPart wptp = new WebPartToolPart();   
//   CustomPropertyToolPart custom = new CustomPropertyToolPart();
//   custom.Expand(&quot;自定义&quot;);
//   toolparts = custom;
//   toolparts = wptp;   
//   return toolparts;
//}

/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name=&quot;output&quot;> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
   //output.Write(SPEncode.HtmlEncode(Text));
   EnsureChildControls();
   output.RenderBeginTag(&quot;div&quot;);
   //RenderChildren(output);
   this.txtValue1.RenderControl(output);
   output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,&quot;bold&quot;);
   output.RenderBeginTag(&quot;span&quot;);
   this.lblOp.RenderControl(output);
   output.RenderEndTag();//span
   
   this.txtValue2.RenderControl(output);
   
   this.lblEq.RenderControl(output);
  this.txtResult.RenderControl(output);
   this.btnCalc.RenderControl(output);
  output.RenderEndTag(); //div
   
}
protected override void CreateChildControls()
{
   //初始化文本框
   txtValue1 = new TextBox();
   txtValue2 = new TextBox();
   txtValue1.Text = &quot;1&quot;;
   txtValue2.Text = &quot;2&quot;;
  lblOp = new Label();
   lblOp.Text = &quot; + &quot;;
   lblEq = new Label();
   lblEq.Text = &quot; = &quot;;
   txtResult = new TextBox();
   txtResult.ReadOnly = true;
   btnCalc = new Button();
   btnCalc.Text = &quot;Calculate&quot;;
   btnCalc.Click += new EventHandler(btnCalc_Click);
   btnCalc.Enabled = calcEnabled;
   Controls.Add(txtValue1);
   Controls.Add(lblOp);
   Controls.Add(txtValue2);
   Controls.Add(lblEq);
   Controls.Add(txtResult);
   Controls.Add(btnCalc);
   
   base.CreateChildControls ();
}
  public void btnCalc_Click(object sender,System.EventArgs e)
{
   if( txtValue1.Text.Trim().Length > 0 && txtValue2.Text.Trim().Length > 0 )
   {
    this.txtResult.Text = Convert.ToString(Convert.ToDouble(txtValue1.Text) + Convert.ToDouble(txtValue2.Text));
   }
}
  }
}

从上面的代码可以看出,一般来说自定义的WebPart 都继承自Microsoft.SharePoint.WebPartPages.WebPart,而不是继承自WebControl。

详细的可以参考:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/CreateABasicWP.asp
页: [1]
查看完整版本: SharePoint 应用的开发学习笔记(二)