Saturday, January 16, 2010

01/17

 

Simple webpart Creation

  • Create a C#-> Sharepoint->Webpart project in vs2008(sharepoint developer tool 9.0 need to be preinstalled)
  • Select deploy option: to GAC or to Bin
  • Put deployment path on debug property of project
  • Deploy to server
  • At this point, the web part component should be shown up in web page dialog of server.

 

 

SP Webpart

  • Can use quickpart tool to deploy ascx control to sp as webpart (issue: can not be used if want to call other controls in pages like webpartmanager because in ascx can not get instance of webpartmanager)
    • deploy quickpart.dsp to projecct
    • copy .ascx to wprecourse folder
    • copy .dll to bin folder

Webpart Debug

  • Debug –>Attach to process (wcwp.exe : process of sp)

Two Webparts Sample:

 

Need to put IPostBackEventHandler to cause post back event by script.

 

AjaxEnabledWebPart

 

  • Setup Ajax Environment
    • Install Microsoft Asp.ent 2.0 Ajax Extensions
    • 下载并安装stsadm.ajaxifymoss自动配置web.config
  • Code:
    • Create a webpart project
    • Add System.Web.Extensions reference which includes scriptmanager and update panel
    • Sample code (note : oninit /createchildcontrols)

        using System;
        using System.Runtime.InteropServices;
        using System.Web.UI;
        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 AjaxEnabledWebPart
        {
            [Guid("1bda9749-1270-4886-90a0-b65be455361b")]
            public class AjaxWebPart : System.Web.UI.WebControls.WebParts.WebPart
            {

                private Label label;

                private TextBox textBox;

                private UpdatePanel up;

                private Button button;

                private ScriptManager _AjaxManager;

                //自定义一个ScriptManager用来存储获取的ScriptManager

                [WebPartStorage(Storage.None)]

                public ScriptManager AjaxManager
                {

                    get { return _AjaxManager; }

                    set { _AjaxManager = value; }

                }

                //页面初始化事件

                protected override void OnInit(EventArgs e)
                {

                    base.OnInit(e);

                    //获取页面上的ScriptManager

                    _AjaxManager = ScriptManager.GetCurrent(this.Page);

                    //如果页面上没有ScriptManager时追加一个

                    if (_AjaxManager == null)
                    {

                        _AjaxManager = new ScriptManager();

                        _AjaxManager.EnablePartialRendering = true;

                        Page.ClientScript.RegisterStartupScript(

                            typeof(AjaxWebPart),

                            this.ID,

                            "_spOriginalFormAction = document.forms[0].action;",

                            true);

                        if (this.Page.Form != null)
                        {

                            string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                            if (!string.IsNullOrEmpty(formOnSubmitAtt)

                                && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                            {

                                this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                            }

                            this.Page.Form.Controls.AddAt(0, _AjaxManager);

                        }

                    }

                }

                //创建各个控件,并填充UpdatePanel

                protected override void CreateChildControls()
                {

                    base.CreateChildControls();

                    this.EnsureUpdatePanelFixups();

                    up = new UpdatePanel();

                    up.ID = "UpdatePanel1";

                    up.ChildrenAsTriggers = true;

                    up.RenderMode = UpdatePanelRenderMode.Inline;

                    up.UpdateMode = UpdatePanelUpdateMode.Always;

                    this.textBox = new TextBox();

                    this.textBox.ID = "TextBox";

                    up.ContentTemplateContainer.Controls.Add(this.textBox);

                    this.label = new Label();

                    this.label.Text = "Enter your name.";

                    up.ContentTemplateContainer.Controls.Add(this.label);

                    this.button = new Button();

                    button.CausesValidation = false;

                    button.ID = "Button1";

                    button.Text = "Say Hello";

                    button.Click += new EventHandler(HandleButtonClick);

                   up.ContentTemplateContainer.Controls.Add(this.button);

                   _AjaxManager.RegisterAsyncPostBackControl(this.button);

                    this.Controls.Add(up);

                }

                private void HandleButtonClick(object sender, EventArgs eventArgs)
                {

                    this.label.Text = "Hello " + this.textBox.Text;

                }

                /*在MOSS中,为了正确解析某些特殊的URLs,例如包含汉字等两比特字符的URL,

                 * Windows SharePoint Services JavaScript使用form onSubmit wrapper重载默认的form action,

                 * 下面这个方法用来恢复默认的form action,如果你的URL里不包含汉字等双字节的字符,

                 * 那么,恭喜,你可以使用ASP.NET AJAX UpdatePanels

                 * (具体请参考上文中提到的「moss开发团队blog」) */

                private void EnsureUpdatePanelFixups()
                {

                    if (this.Page.Form != null)
                    {

                        string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                        if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                        {

                            this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                        }

                    }

                    ScriptManager.RegisterStartupScript(

                        this,

                        typeof(AjaxWebPart),

                        "UpdatePanelFixup",

                        "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;",

                        true);

                }

            }

        }

  • QuickPart Webpart Development
    • Run InstallSolution.bat on server of Quickpart.zip(with quickpart.wsp) which will deploy quickpart.wsp to sp server
    • Deploy quickpart to specified site
      • Admin site -->全局配->解决方案管理-->部署解决方案—>Quickpart
    • Active quickpart in site
      • Site-->网站集管理—>Active quickpart
    • Create a sharepoint project and add a web user control(.ascx)  componet(can not add directly but copy/paste from an asp.net applicaton)
    • Set the start browser with url with destination site
    • on build events of project , adding the following cmd(which will copy *.dll to “bin” folder and *.ascx” to “wpresource” .
      • copy "$(TargetPath)"  C:\Inetpub\wwwroot\wss\VirtualDirectories\39329\bin  
        copy "$(ProjectDir)*.ascx"  C:\Inetpub\wwwroot\wss\VirtualDirectories\39329\wpresources

         

 

 

'

0 Comments:

Post a Comment

<< Home