1. 程式人生 > >創建你第一個SharePoint 2010 應用程序----完整推薦總結

創建你第一個SharePoint 2010 應用程序----完整推薦總結

ron cti set sha right itl 表格布局 下界 連接

版權聲明:本文為博主原創文章。未經博主同意不得轉載。 https://blog.csdn.net/u012025054/article/details/32957597

創建你第一個SharePoint 2010 應用程序

本文中,你會學到: 1. 創建解決方式,使用server端對象模型和可視Web Part部件讀取和寫入列表數據。 2. 使用Visual Studio 2010 創建並部署解決方式。 3. 使用Chart Web Part呈現列表數據。 4. 在一個Web部件頁面集成解決方式中的不同可視Web部件。

終於效果:

技術分享圖片

準備:

首先要創建兩個必要的列表Customer Sales和Total Sales。第一個列表儲存關於公司和FY10季度銷售信息。它包括一個Company欄(單行文本,由Title欄改動而生)和四個欄Q1。Q2。Q3,Q4(單行文本),像這樣: 技術分享圖片
第二個列表由兩個欄組成。Year(單行文本)和Sales(數字)。像這樣: 技術分享圖片

創建應用程序:

總共分成四個步驟。首先開發一個添加記錄到Customer Sales列表的功能。

創建一個空白解決方式。提供項目框架。 1. 打開Visual Studio 2010. 2. 點擊文件--新建項目。 3. 導航到其它項目類型,選擇Visual Studio 解決方式。 4. 點擊空白解決方式。命名MyFirstSPSolution。

加入記錄到Sales列表 主要界面是一個可視Web部件。 1. 右擊解決方式,選擇加入--新建項目。

2. 選擇空白SharePoint項目,命名CustomerSalesWebPart,點擊確定。部署為場解決方式。 3. 右鍵單擊項目,選擇加入--新建項--可視Web部件,命名CustSalesVWP。

4. 右鍵單擊CustSalesVWP.ascx,選擇視圖查看器。 5. 創建例如以下界面,包括6個標簽、5個文本框、2個鏈接button。 代碼應該是這種:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustSalesVWPUserControl.ascx.cs" Inherits="CustomerSalesWebPart.CustSalesVWP.CustSalesVWPUserControl" %>

AddClear
6. 右擊CustSalesVWP.ascx。點擊查看代碼。切換到代碼視圖。

7. 加入引用 using Microsoft.SharePoint; 8. 終於代碼應該是這種:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace CustomerSalesWebPart.CustSalesVWP
{
    public partial class CustSalesVWPUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void lnkbtnAdd_Click(object sender, EventArgs e)
        {
            //Be sure to update the SharePoint site to your server name.
            using (SPSite site = new SPSite("http://smallville-pc:1528"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["Customer Sales"];
                    SPListItem newItem = list.Items.Add();
                    newItem["Title"] = txtbxCompanyName.Text;
                    newItem["_x0051_1"] = txtbxQ1.Text;
                    newItem["_x0051_2"] = txtbxQ2.Text;
                    newItem["_x0051_3"] = txtbxQ3.Text;
                    newItem["_x0051_4"] = txtbxQ4.Text;
                    newItem.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
        protected void lnkbtnClear_Click(object sender, EventArgs e)
        {
            txtbxCompanyName.Text = "";
            txtbxQ1.Text = "";
            txtbxQ2.Text = "";
            txtbxQ3.Text = "";
            txtbxQ4.Text = "";
        }
    }
}
9. 部署解決方式。

10. 在SharePoint站點,創建新Web部件頁。點擊全部站點內容--創建--頁面--Web部件頁。

命名Sales Dashboard。

11. 加入Web部件,在Custom類中找到CustSalesVWP。點擊加入。 測試這個可視Web部件。確保能正常工作。

加入新紀錄到Customer Sales列表。並前去驗證。 技術分享圖片
點擊Add以後。查看列表: 技術分享圖片
點擊Clear,數據清空: 技術分享圖片

查看Customer Sales數據

接下來我們要創建一個僅僅讀的Web部件。簡單載入並顯示Customer Sales列表中的數據。

在ASP.NET應用程序中,數據網格是很有效的方式。這次我們就用這個功能載入和刷新數據。 1. 文件--新建--項目。 2. 選擇空白SharePoint項目,命名CustomerSalesList。點擊加入到解決方式。 3. 部署為場解決方式。點擊完畢。

4. 右擊項目。選擇加入--新建項。 5. 選中可視Web部件,命名SalesVWP,點擊加入。 6. 右擊SalesVWP.ascx,選擇視圖查看器。 7. 拖曳2個標簽。一個datagrid。一個鏈接button和一個Update Panel。

最後的代碼應該是這種:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SalesVWPUserControl.ascx.cs" Inherits="CustomerSalesList.SalesVWP.SalesVWPUserControl" %>

Load
界面是這種: 技術分享圖片
8. 右擊項目,加入--類。命名CustomerSales。加入部分代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomerSalesList
{
    class CustomerSales
    {
        public string Company { get; set; }
        public string Q1 { get; set; }
        public string Q2 { get; set; }
        public string Q3 { get; set; }
        public string Q4 { get; set; }
    }
}
9. 右擊SalesVWP.ascx。選擇查看代碼。 10. 加入部分代碼。最後是這種:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace CustomerSalesList.SalesVWP
{
    public partial class SalesVWPUserControl : UserControl
    {
        List listOfCustomerSales = new List();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private void updateRefreshTime()
        {
            DateTime currentTime = DateTime.Now;
            string refreshMessage = "Last Refreshed: ";
            lblRefreshTime.Text = refreshMessage + currentTime.ToLongTimeString();
        }

        protected void lnkbtnLoadData_Click(object sender, EventArgs e)
        {
            //Be sure to replace the SharePoint site reference here.
            //確保用自己的server名取代此處的SPSite。
            using (SPSite site = new SPSite("http://smallville-pc:1528"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList custSalesDataList = web.Lists["Customer Sales"];
                    SPQuery myCAMLQuery = new SPQuery();
                    myCAMLQuery.Query = "";
                    SPListItemCollection mySalesListItems =
                        custSalesDataList.GetItems(myCAMLQuery);
                    foreach (SPListItem tempListItem in mySalesListItems)
                    {
                        CustomerSales custSaleInfo = new CustomerSales();
                        custSaleInfo.Company = tempListItem["Title"].ToString();
                        custSaleInfo.Q1 = "$ " + tempListItem["_x0051_1"].ToString() + ".00";
                        custSaleInfo.Q2 = "$ " + tempListItem["_x0051_2"].ToString() + ".00";
                        custSaleInfo.Q3 = "$ " + tempListItem["_x0051_3"].ToString() + ".00";
                        custSaleInfo.Q4 = "$ " + tempListItem["_x0051_4"].ToString() + ".00";
                        listOfCustomerSales.Add(custSaleInfo);
                    }
                }
            }
            custDataGrid.DataSource = listOfCustomerSales;
            custDataGrid.DataBind();
            updateRefreshTime();
            lnkbtnLoadData.Text = "Refresh";
        }
    }
}
11. 右擊這個項目,選擇生成解決方式,成功後部署解決方式。

12. 在之前的頁面點擊站點操作--編輯頁面--加入Web部件。 13. 選中Custom類中的SalesVWP Web部件。點擊加入。 14. 你能夠看到: 技術分享圖片
點擊Load後。看到: 技術分享圖片

查看Total Sales

創建一個綜合銷售量的Web部件。 1. 右擊解決方式,加入--新建項目。 2. 選中空白SharePoint項目,命名TotalSalesVisualWebPart。點擊確定,部署為場解決方式。 3. 右擊項目,加入--新建項,選擇可視Web部件,命名TotalSalesVWP,點擊加入。 4. 拖曳2個標簽、一個datagrid和一個鏈接button。最後代碼視圖是這種:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TotalSalesVWPUserControl.ascx.cs" Inherits="TotalSalesVisualWebPart.TotalSalesVWP.TotalSalesVWPUserControl" %>

Load ? 5. 右擊項目,加入--類,命名SalesTotals。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TotalSalesVisualWebPart
{
    class SalesTotals
    {
        public string compName { get; set; }
        public string Q1 { get; set; }
        public string Q2 { get; set; }
        public string Q3 { get; set; }
        public string Q4 { get; set; }
    }
}
6. 右擊項目。選擇查看代碼。 7. 加入代碼,最後是這種:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Data;
using Microsoft.SharePoint;

namespace TotalSalesVisualWebPart.TotalSalesVWP
{
    public partial class TotalSalesVWPUserControl : UserControl
    {
        int numOfCompanies = 0;
        int totalQ1 = 0;
        int totalQ2 = 0;
        int totalQ3 = 0;
        int totalQ4 = 0;
        int aggSales = 0;
        string[] totalSales = new string[4];
        DataTable salesTable = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void linkbtnLoad_Click(object sender, EventArgs e)
        {
            //Be sure to replace the SharePoint site reference here.
            //確保使用自己的server名取代此處的SPSite。
            using (SPSite site = new SPSite("http://smallville-pc:1528/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList custSalesDataList = web.Lists["Customer Sales"];
                    SPQuery myCAMLQuery = new SPQuery();
                    myCAMLQuery.Query = "";
                    SPListItemCollection mySalesListItems =
                    custSalesDataList.GetItems(myCAMLQuery);
                    foreach (SPListItem tempListItem in mySalesListItems)
                    {
                        SalesTotals tempSalesObject = new SalesTotals();
                        numOfCompanies += 1;
                        tempSalesObject.compName = tempListItem["Title"].ToString();
                        tempSalesObject.Q1 = tempListItem["_x0051_1"].ToString();
                        totalQ1 = totalQ1 + Int32.Parse(tempSalesObject.Q1);
                        tempSalesObject.Q2 = tempListItem["_x0051_2"].ToString();
                        totalQ2 = totalQ2 + Int32.Parse(tempSalesObject.Q2);
                        tempSalesObject.Q3 = tempListItem["_x0051_3"].ToString();
                        totalQ3 = totalQ3 + Int32.Parse(tempSalesObject.Q3);
                        tempSalesObject.Q4 = tempListItem["_x0051_4"].ToString();
                        totalQ4 = totalQ4 + Int32.Parse(tempSalesObject.Q4);
                    }
                }
            }
            totalSales[0] = totalQ1.ToString();
            totalSales[1] = totalQ2.ToString();
            totalSales[2] = totalQ3.ToString();
            totalSales[3] = totalQ4.ToString();

            DataColumn salesColumnQ1 = new DataColumn("Q1");
            salesTable.Columns.Add(salesColumnQ1);
            DataColumn salesColumnQ2 = new DataColumn("Q2");
            salesTable.Columns.Add(salesColumnQ2);
            DataColumn salesColumnQ3 = new DataColumn("Q3");
            salesTable.Columns.Add(salesColumnQ3);
            DataColumn salesColumnQ4 = new DataColumn("Q4");
            salesTable.Columns.Add(salesColumnQ4);

            DataRow salesTotalRow = salesTable.NewRow();
            salesTotalRow[0] = "$ " + totalSales[0] + ".00";
            salesTotalRow[1] = "$ " + totalSales[1] + ".00";
            salesTotalRow[2] = "$ " + totalSales[2] + ".00";
            salesTotalRow[3] = "$ " + totalSales[3] + ".00";

            salesTable.Rows.Add(salesTotalRow);

            totalSalesDataView.DataSource = salesTable;
            totalSalesDataView.DataBind();
            
            updateRefreshTime();
            linkbtnLoad.Text = "Refresh";

            aggSales = totalQ1 + totalQ2 + totalQ3 + totalQ4;
            updateAggSales(aggSales);
        }

        private void updateAggSales(int aggSales)
        {
            string fiscalYear = "FY 10";

            using (SPSite site = new SPSite("http://smallville-pc:1528/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList totalSales = web.Lists["Total Sales"];
                    SPListItem newStat = totalSales.Items.Add();
                    newStat["Title"] = fiscalYear;
                    newStat["Sales"] = aggSales;
                    newStat.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
        private void updateRefreshTime()
        {
            DateTime currentTime = DateTime.Now;
            string refreshMessage = "Last Refreshed: ";
            lblRefreshMessage.Text = refreshMessage + currentTime.ToLongTimeString();
        }
    }
}
8. 右擊此項目。選擇生成。成功後部署解決方式。 9. 在同樣頁面加入Web部件。 10. 在Custom類中找到TotalSalesVWP,點擊加入。 11. 你會看到: 技術分享圖片
點擊Load後: 技術分享圖片

加入一個Chart 圖表 Web Part 部件

以圖表的視圖呈現Total Sales列表的數據。 1. 編輯之前的頁面。

2. 加入Web部件。

在業務數據類中,選中Chart 圖表 Web Part 部件。點擊加入。

3. 點擊編輯此Web部件,自己定義您的圖表。 4. 第一步選擇柱形圖。 技術分享圖片
5. 接收第二步的默認。 6. 第三步,選中顯示圖表標題。命名FY 10 Sales。

點擊自己主動預覽查看圖表布局。

點擊完畢。

技術分享圖片
7. 右擊Web部件下拉箭頭,選擇連接到數據。 8. 選擇連接到列表。點擊下一步。

技術分享圖片 9. 選擇TotalSales列表,點擊下一步。 技術分享圖片
10. 你會看到一個表格布局,點擊下一步。

技術分享圖片
11. 這一步你能夠設置自己定義改變圖標外觀,這裏接收默認。X字段選擇Sales,點擊完畢。 技術分享圖片
在頁面上你會看到: 技術分享圖片
最後完畢的整個頁面是這種: 技術分享圖片

創建你第一個SharePoint 2010 應用程序----完整推薦總結