【转】SharePoint 2013 开发——开发并部署webpart
webpart我們就不詳細(xì)闡述了,在APP的開發(fā)中,自定義屬性設(shè)置可以通過APP webpart的URL查詢字符串傳遞,它通過IFRAME來顯示遠(yuǎn)程的內(nèi)容。廢話不多說,我們開始實際操作。
打開Visual Studio,新建SharePoint應(yīng)用程序項目,名字我們就叫做SharePointAppPartTest。
參照上一篇完成項目的創(chuàng)建。 右鍵點擊SharePoint項目節(jié)點,選擇添加->新建項,選擇客戶端Web部件(宿主Web),起名叫做ClientWebPartTest,點擊確定并在下一個對話框中保留默認(rèn)完成添加。
我們可以看到解決方案中是如下圖生成的:
SharePoint工程中有一個Elements.xml元素用來說明我們創(chuàng)建的webpart,托管Web應(yīng)用程序中的Pages文件夾下生成了一個對應(yīng)的ASPX頁面。打開Elements.xml文件可以看到如下默認(rèn)生成的內(nèi)容:
<ClientWebPart Name="ClientWebPartTest" Title="ClientWebPartTest 標(biāo)題" Description="ClientWebPartTest 說明" DefaultWidth="300" DefaultHeight="200"><!-- Content 元素標(biāo)識將在客戶端 Web 部件內(nèi)呈現(xiàn)的頁面的位置在查詢字符串上使用模式 _propertyName_ 引用了屬性示例: Src="~appWebUrl/Pages/ClientWebPart1.aspx?Property1=_property1_" --><Content Type="html" Src="~remoteAppUrl/Pages/ClientWebPartTest.aspx?{StandardTokens}" /><!-- 在 Properties 元素中定義屬性。請記得在上述 Content 元素的 Src 特性上放置屬性名稱。 --><Properties></Properties></ClientWebPart>我們來添加幾個屬性,在Properties節(jié)點下,聲明如下四個屬性(string、int、bool、enum):
<PropertyName="myStrProp"Type="string"RequiresDesignerPermission="true"DefaultValue="String default value"WebCategory="My Test Apps"WebDisplayName="A property of type string."></Property><PropertyName="myIntProp"Type="int"RequiresDesignerPermission="true"DefaultValue="0"WebCategory="My Test Apps"WebDisplayName="A property of type integer."></Property><PropertyName="myBoolProp"Type="boolean"RequiresDesignerPermission="true"DefaultValue="false"WebCategory="My Test Apps"WebDisplayName="A property of type boolean."></Property><PropertyName="myEnumProp"Type="enum"RequiresDesignerPermission="true"DefaultValue="1st"WebCategory="My Test Apps"WebDisplayName="A property of type enum."><EnumItems><EnumItem WebDisplayName="First option" Value="1st"/><EnumItem WebDisplayName="Second option" Value="2nd"/><EnumItem WebDisplayName="Third option" Value="3rd"/></EnumItems></Property>都是我們測試中用的,所以名稱有些隨意,實際應(yīng)用中請取有意義的名稱。?屬性創(chuàng)建完之后,如何與webpart進(jìn)行關(guān)聯(lián)呢?我們需要修改Content節(jié)點的Src屬性,修改后的節(jié)點如下所示:
?
<Content Type="html" Src="~remoteAppUrl/Pages/ClientWebPartTest.aspx?{StandardTokens}&StrProp=_myStrProp_&IntProp=_myIntProp_&BoolProp=_myBoolProp_&EnumProp=_myEnumProp_&Editmode=_editMode_" />借助這種方式,APP?webpart的參數(shù)通過URL的查詢字符串傳遞到ASPX頁面,接下來我們到ASPX頁面去處理我們定義的參數(shù)。
打開ClientWebPartTest.aspx頁面,在空的DIV元素內(nèi)加入如下控件:
<asp:Label ID="Label1" runat="server"></asp:Label> <asp:Literal ID="Literal1" runat="server" Text="Hello world from an app part!"></asp:Literal>打開后臺代碼ClientWebPartTest.aspx.cs,在Page_Load方法中加入如下代碼來獲取傳遞的參數(shù):
var intParam = Request.QueryString["IntProp"];var strParam = Request.QueryString["StrProp"];var boolParam = Request.QueryString["BoolProp"];var enumParam = Request.QueryString["EnumProp"];var editMode = Request.QueryString["EditMode"];if ("true" == editMode){Literal1.Text = "The App Part is in edit mode";}else{Literal1.Text = "myIntProp = " + intParam + "<br>" +"myStrProp = " + strParam + "<br>" +"myBoolProp = " + boolParam + "<br>" +"myEnumProp = " + enumParam;} var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);using (var clientContext = spContext.CreateUserClientContextForSPHost()){clientContext.Load(clientContext.Web, web => web.Title);clientContext.ExecuteQuery();this.Label1.Text = "Site Title: " + clientContext.Web.Title + "<br>";}代碼中我又加了一段之前的CSOM,是想用簡單的組合來告訴大家我們其實可以在其中做很多的事情。
F5生成并部署APP,成功之后彈出瀏覽器窗體:
一樣的東西,默認(rèn)會跳轉(zhuǎn)到應(yīng)用程序的Default頁面,我們回到我們的開發(fā)人員網(wǎng)站,點擊右上角的設(shè)置->編輯網(wǎng)頁,選擇插入選項卡,點擊應(yīng)用程序部件。
點擊添加按鈕完成頁面中添加webpart的操作。
好了,webpart中已經(jīng)顯示了我們讓它顯示的內(nèi)容。
我們回到編輯狀態(tài),編輯這個webpart,可以看到我們添加的自定義屬性。我們對屬性進(jìn)行適當(dāng)?shù)男薷牟⒈4妗?/p>
以上就是開發(fā)APP?webpart的大致過程。
另外一點需要說明的是,由于我們在調(diào)試狀態(tài)下,并沒有發(fā)布APP,所以需要Visual Studio處于調(diào)試狀態(tài)下才可以進(jìn)行訪問測試。
總結(jié)
以上是生活随笔為你收集整理的【转】SharePoint 2013 开发——开发并部署webpart的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 隐翅虫飞到身上千万别拍!毒液堪比硫酸
- 下一篇: 当心!尿液变黄是身体在向你"报告"这几件