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

SharePoint(WSS)学习(2) 开发WebPart

2012年12月14日 ⁄ 综合 ⁄ 共 3733字 ⁄ 字号 评论关闭
WebPart可以看作是SharePoint的一种插件,是用来实现特定功能的组件。本篇开发一个非常简单的WebPart,了解一下其开发、部署。

1.开发
新建项目,选择SharePoint的Webpa模板,项目名称为SimpleCalculator。

可以看到解决方案管理器里自动添加了Microsoft.SharePoint的引用;添加了*.snk,*.cs,*.webpart,*.xml等几个文件,我们将它改成友好的文件名称:

实现SimpleCalculator:

using System;
using System.Runtime.InteropServices;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace SimpleCalculator
{
    [Guid(
"05dd0d64-0c29-47ab-8a00-8f95d6ef67cc")]
    
public class Calculator : System.Web.UI.WebControls.WebParts.WebPart
    {
        TextBox txt1
= new TextBox();
        DropDownList ddl1
= new DropDownList();
        TextBox txt2
= new TextBox();
        HtmlGenericControl lbl1
= new HtmlGenericControl("span");
        TextBox txt3
= new TextBox();
        Button btn1
= new Button();
        HtmlGenericControl lbl2
= new HtmlGenericControl("span"); 

        
protected override void CreateChildControls()
        {
            txt2.Width
= ddl1.Width = txt1.Width = 40;

            ddl1.Items.Add("+");
            ddl1.Items.Add(
"-");
            ddl1.Items.Add(
"*");
            ddl1.Items.Add(
"/");

            lbl1.InnerHtml = "=";

            btn1.Text = "Calculate";
            btn1.Click
+= new EventHandler(btn1_Click);

            lbl2.Style.Add("color", "red");

            this.Controls.Add(txt1);
            
this.Controls.Add(ddl1);
            
this.Controls.Add(txt2);
            
this.Controls.Add(lbl1);
            
this.Controls.Add(lbl2);
            
this.Controls.Add(btn1);
        }

        void btn1_Click(object sender, EventArgs e)
        {
            
try
            {
                
double db1 = double.Parse(txt1.Text);
                
double db2 = double.Parse(txt2.Text);
                
double result = 0.0;

                switch (ddl1.SelectedValue)
                {
                    
case "+":
                        result
= db1 + db2;
                        
break;

                    case "-":
                        result
= db1 - db2;
                        
break;

                    case "*":
                        result
= db1 * db2;
                        
break;

                    case "/":
                        result
= db1 / db2;
                        
break;
                }

                lbl2.InnerHtml = result.ToString("F2");

            }
            catch (Exception ex)
            {
                lbl2.InnerHtml
= ex.Message;
            }
        }
    }
}

Calculator文件中自动添加了以下SharePoint命名空间(在本例中不需要使用)
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages; 一个GUID,这是这个WebPart的唯一标志。 F5一键即可实现编译、部署。

在WebPart列表页面可以看到我们刚在部署的WebPart。

退出编辑模式可以SimpleCalcuator能正确显示和运行,没有修饰的UI很不漂亮;-)

部署成功,那就看看自己的文件部署到哪去了呢?

可以看到,dll文件竟然部署到GAC里了,对于一个测试的程序来说,这不是我们想要的。
显示所有的文件,看到有个pkg文件夹,manifest.xml的DeploymentTarget设为WebApplication即可。

<?xml version="1.0" encoding="utf-8"?>
<Solution SolutionId="4658fa3d-5de4-4b46-85a8-5988cc6c977d" xmlns="http://schemas.microsoft.com/sharepoint/">
  
<FeatureManifests>
    
<FeatureManifest Location="Calculator\feature.xml" />
  
</FeatureManifests>
  
<Assemblies>
    
<Assembly Location="SimpleCalculator.dll" DeploymentTarget="WebApplication" />
  
</Assemblies>
</Solution>

修改后,dll文件在站点的bin目录,但再把该webpa添加到页面会出现以下错误:
Assemblies that implement ASP.NET Web Parts and are installed into a partially trusted location, such as the bin directory, must be compiled with the AllowPartiallyTrustedCallersAttribute set for import to succeed.
在程序集添加属性
[assembly: AllowPartiallyTrustedCallersAttribute()],该方法位于System.Security命名空间中。

*.webpart文件中,Title属性为WebPart的名称,Description为WebPart的描述,上图显示的ToolTip就是描述的内容。

飘遥的BLOG:http://www.cnblogs.com/zxjay/
作者:飘遥(周振兴)

抱歉!评论已关闭.