使用QuickPart開發WebPart

來源:互聯網
上載者:User

      在MOSS開發中,每個朋友都能夠很自然的想到WebPart,的確,在當今的web開發中,為了滿足更多使用者的需求,個人化定製成為了開發人員的一個大難題,不過現在大家似乎不用在為這個問題犯困了,實際上微軟已經幫我們解決了,那就是開發WebPart,但是由於很多朋友不知道WebPart的工作機制,使得開發起來難度較大,現在筆者就介紹一種簡單的WebPart開發方式------使用QuickPart;那麼QuickPart是什麼東西呢?讓我來告訴您,QuickPart就是一種可以將ASP.NET裡頭的WebUserControl打包成標準的SharePoint WebPart,下面筆者通過一些例子介紹如何如何使用QuickPart進行WebPart的快速開發.

     此例子要實現的效果是將文件庫的目錄結構綁定至TreeView控制項,採用物件模型(Object Model)編程

         

     筆者建立的是ASP.NET Web應用程式,使用的語言是Visual C#,您可根據自己擅長的程式設計語言建立ASP.NET網站或者ASP.NET Web應用程式,這都是可以的

    

      在編碼之前,筆者先配置環境,使得每次一產生項目,就能夠自動的把我們要的.dll和.ascx拷貝到對應的SharePoint網站目錄的BIN檔案夾和WPRESOURCES下,右擊項目名稱,選擇屬性,在跳出的頁面上選擇”建置事件”,編輯其產生後的命令列

     

      中命令的意思是將產生的.dll檔案拷貝至SharePoint網站所在的目錄” C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin”下,將專案檔夾下的所有使用者控制項拷貝至” C:\Inetpub\wwwroot\wss\VirtualDirectories\80\wpresources”目錄下,當然,您如果不想配置該命令列,每次產生項目後也可手動拷貝,單擊確定

      

      您可以將項目下的Default.aspx刪除,添加一個Web使用者控制項,筆者取名為TreeCatalog.ascx,往頁面上拖拽一個TreeView控制項,ID設為tvTreeCatalog,筆者建議使用有意義的ID號

     

       由於需要使用到SharePoint命名空間,所以筆者這裡將Microsoft.SharePoint.dll引用至項目中,該.dll所在目錄為” C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI”

      

      TeeCatalog.ascx.cs代碼如下:

Code
Using Microsoft.SharePoint;   
Using Micorosft.SharePoint.WebControls;   
    
protected void Page_Load(object sender, EventArgs e)   
        {   
            //Page Init   
                if (!IsPostBack)   
                {   
                   //calling the method   
                   BindDocLibrary();   
                }   
       }   
    
//main method,bind the document library   
        private void BindDocLibrary()   
        {   
            //get the current web content,include list,listitem,folder and so on   
            SPWeb spWeb = SPContext.Current.Web;   
            //attention please!your document library must called "我的文件"   
            SPList spList = spWeb.Lists["我的文件"];   
            //add the node into treeview by calling the method   
            tvTreeCatalog.Nodes.Add(CreateNodeByList(spList));   
     }   
    
private TreeNode CreateNodeByList(SPList spList)   
        {   
            TreeNode node = new TreeNode();   
            //this node is a root node,also is the root node of document library   
            node.Text = spList.Title;   
            //get the root folder url of document library   
            node.Value = spList.RootFolder.Url;   
            foreach (SPFolder subFolder in spList.RootFolder.SubFolders)   
            {   
                //sharepoint site hava a default folder named "Forms",we don't need it   
                if (subFolder.Name != "Forms")   
                {   
                    //add the node into treeview by calling the method   
                    node.ChildNodes.Add(CreateNodeByFolder(subFolder));   
                }   
            }   
            return node;   
      }   
    
private TreeNode CreateNodeByFolder(SPFolder spFolder)   
        {   
            TreeNode node = new TreeNode();   
            node.Text = spFolder.Name;              
            node.Value = spFolder.Url;   
            //appoint the node imageurl   
            node.ImageUrl = SPControl.GetContextWeb(Context).Url + "/_layouts/images/folder.gif";   
            foreach (SPFolder subFolder in spFolder.SubFolders)   
            {   
                //here is uses a recursive process!    remark:recursive 遞迴   
                node.ChildNodes.Add(CreateNodeByFolder(subFolder));   
            }   
            return node;   
      }  
Using Microsoft.SharePoint;
Using Micorosft.SharePoint.WebControls;
 
protected void Page_Load(object sender, EventArgs e)
        {
            //Page Init
                if (!IsPostBack)
                {
                   //calling the method
                   BindDocLibrary();
                }
       }
 
//main method,bind the document library
        private void BindDocLibrary()
        {
            //get the current web content,include list,listitem,folder and so on
            SPWeb spWeb = SPContext.Current.Web;
            //attention please!your document library must called "我的文件"
            SPList spList = spWeb.Lists["我的文件"];
            //add the node into treeview by calling the method
            tvTreeCatalog.Nodes.Add(CreateNodeByList(spList));
     }
 
private TreeNode CreateNodeByList(SPList spList)
        {
            TreeNode node = new TreeNode();
            //this node is a root node,also is the root node of document library
            node.Text = spList.Title;
            //get the root folder url of document library
            node.Value = spList.RootFolder.Url;
            foreach (SPFolder subFolder in spList.RootFolder.SubFolders)
            {
                //sharepoint site hava a default folder named "Forms",we don't need it
                if (subFolder.Name != "Forms")
                {
                    //add the node into treeview by calling the method
                    node.ChildNodes.Add(CreateNodeByFolder(subFolder));
                }
            }
            return node;
      }
 
private TreeNode CreateNodeByFolder(SPFolder spFolder)
        {
            TreeNode node = new TreeNode();
            node.Text = spFolder.Name;           
            node.Value = spFolder.Url;
            //appoint the node imageurl
            node.ImageUrl = SPControl.GetContextWeb(Context).Url + "/_layouts/images/folder.gif";
            foreach (SPFolder subFolder in spFolder.SubFolders)
            {
                //here is uses a recursive process!    remark:recursive 遞迴
                node.ChildNodes.Add(CreateNodeByFolder(subFolder));
            }
            return node;
      } 

 
        以上四個方法就是本例子的代碼,由於頁面較長,筆者就不了,注釋也都寫上,核心的思想就是使用遞迴演算法,請注意,當您直接使用筆者的代碼時請確保您的sharepoint網站下存在名為”我的文件”的文件庫

      

      產生項目,可以在輸出框中看到已經自動將.dll和.ascx檔案複製到sharepoint網站的bin和wpresources目錄下

     

      開啟sharepoint網站首頁,進入”文檔中心”子網站,依次選擇”網站操作”->”編輯網頁”

           

      在左欄地區單擊”添加Web組件”

     

      找到QuickPart,二者選一個即可,單擊添加按鈕

     

      此時頁面仍然處於編輯狀態,並且在左欄的WebPart地區成功添加了ProviderQuickPart組件,下面我們編輯它

     

      選擇”編輯”->”修改共用Web組件”

          

      在頁面右邊彈出的SideBar上就可以看到我們產生的使用者控制項TreeCatalog了,選擇它並單擊確定按鈕

     

      選擇”退出編輯模式”

     

      當您重新開啟sharepoint網站時就可以看到將名為”我的文件”的文件庫成功綁定到TreeView控制項上了.

      本文來自CSDN部落格:http://blog.csdn.net/showilove/archive/2009/08/31/4503768.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.