Note: this instance eliminates the need to add data controls and avoids some tedious code
The implementation results are as follows:
Reads data and pictures and displays them in two columns per row
The required list is as follows:
1. VS2010 Create sandboxed Solutions
2. Create a WebPart
3. Add code to TestWebpart.cs
Add protected override void Render (HtmlTextWriter writer) method
Note: CSS style files are referenced in the master page and are used directly here.
The entire code is as follows
usingSystem;usingSystem.ComponentModel;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingMicrosoft.SharePoint;usingMicrosoft.SharePoint.WebControls;usingSystem.Xml.Serialization;usingSystem.Linq;usingSystem.Data;namespacetestproject.testwebpart{[ToolBoxData ("<{0}:wptoolpanetest runat=server></{0}:testwebpart>"), xmlroot (Namespace ="Testwebpart")]//Define Toolbox Data Public classTestwebpart:webpart {protected Override voidCreateChildControls () {}protected Override voidRender (HtmlTextWriter writer) {SPSite site=SPContext.Current.Site; SPWeb Web=SPContext.Current.Web; SPList List= Web. lists["Insurance Category"]; varquery = fromSPListItem IteminchList. Items byItem.id AscendingSelectitem; Writer. Write ("<div class= ' default_left_part1_left_part1_5 ' >"); Writer. Write ("<div class= ' default_left_part1_left_part1_4 ' ><table width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' Border= ' 0 ' ><tr>"); inti =1; foreach(SPListItem Iteminchquery) {writer. Write ("<td class= ' default_left_part1_left_part1_4_1 ' ><div><table width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' border= ' 0 ' ><tr><td class= ' default_left_part1_left_part1_4_1_1 ' >"); Writer. Write (item["icons"] ==NULL?"": item["icons"]. ToString ()); Writer. Write ("'/></td><td class= ' default_left_part1_left_part1_4_1_2 ' ><div><a href= '/Lists/List/ Dispform.aspx?id="); Writer. Write (item["ID"]. ToString ()); Writer. Write ("' class= ' default_mylink1_2 ' ><b>"); Writer. Write (item["title"] ==NULL?"": item["title"]. ToString ()); Writer. Write ("</b></a></div><div>"); if(item["subtitle"] !=NULL) {writer. Write (item["subtitle"]. ToString (). Length > the? item["subtitle"]. ToString (). Substring (0, the) +"... ..": item["subtitle"]. ToString ()); } Else{writer. Write (""); } writer. Write ("</div></td></tr></table></div></td>"); ifI2==0) {writer. Write ("</tr><tr>"); } I++; } writer. Write ("</tr></table></div>"); Writer. Write ("</div>"); Base. Render (writer); } }}
After the code is complete, click Test Deployment Sandbox solution in VS2010, insert WebPart part Testwebpart in the SharePoint page, and view the results.
4. Deploying sandboxed Solutions in SharePoint Online
Uploading testproject.wsp in the solution Bin directory to a SharePoint solution
Here's how:
Open the SharePoint site that you want to deploy, site actions-Site Settings-Solutions
On the Solution tab, select Upload solution and activate to insert a WebPart in the page.
Attached: CSS style
. pingan_default_left_part1_left_part1_5{padding-top:2px; width:463px;}. pingan_default_left_part1_left_part1_4{width:463px; }.pingan_default_left_part1_left_part1_4_1{padding-top:10px; Vertical-Align:top; Text-Align:left;}. pingan_default_left_part1_left_part1_4_1_1{Vertical-Align:top; Text-Align:left; width:56px;}. pingan_default_left_part1_left_part1_4_1_2{Vertical-Align:top; Text-Align:left; Padding-left:5px; color:#666666; Line-height:20px;} a.pingan_default_mylink1_2:link{ Line-height:20px; Font-size:12px; COLOR: #666666; TEXT-decoration:none}a.pingan_default_mylink1_2:visited{ Line-height:20px; Font-size:12px; COLOR: #666666; TEXT-decoration:none}a.pingan_default_mylink1_2:hover{ Line-height:20px; Font-size:12px; COLOR: #FA8100; TEXT-Decoration:underline;}
SHAREPOINT2010 Sandbox Solution Foundation development--develop a WebPart to read bound list data and display it in a certain format (plus CSS style)