Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting Russia – HostForLIFE.eu :: How to Display The List Data in a Web Part in SharePoint 2010?
Today, we will discussed about display the list data in a web part. But First, open Visual Studio & create a new Web Part Solution.The Web Part solution shown on the below picture:
In the .cs file –> “DisplayListData.cs” file of the web part, you should declare a Personalizable Control which creates a Text box in the Edit Web Part Properties. If you want multiple values you should declare it again & save the value in other string as you can see on the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
//Create a textbox in the Edit Web Part Properties to get the List Name [Category("Custom Properties"), Personalizable(PersonalizationScope.User), WebBrowsable(true), WebDisplayName("List Name"), WebDescription("Enter List Name")] //Get the value from the textbox & save it in the string public string sListName { get; set; } //Create another textbox in the Edit Web Part Properties to get the field name [Category("Custom Properties"), Personalizable(PersonalizationScope.User), WebBrowsable(true), WebDisplayName("Column Name"), WebDescription("Enter List Column Name")] //Get the value from the textbox & save it in the string public string sListColumnName { get; set; } Now create the Label Controls to display the items in the web part Literal ltrl = new Literal(); Label lblListName = new Label(); Label lblColumnName = new Label(); Label lblDisplayError = new Label(); protected override void CreateChildControls() { ltrl.Text = ""; lblListName.Text = "List Name entered : " + sListName; lblListName.Font.Bold = true; lblColumnName.Text = "List Column Name entered : " + sListColumnName; lblColumnName.Font.Bold = true; lblDisplayError.Text = ""; this.Controls.Add(lblListName); this.Controls.Add(lblColumnName); this.Controls.Add(lblDisplayError); try { if (sListName != null && sListColumnName != null) { using (SPWeb currentWeb = SPContext.Current.Web) { SPList oList = currentWeb.Lists[sListName]; if (oList != null) { ltrl.Text = "Title<hr style='width:100px;text-align:left;'>"; foreach (SPListItem item in oList.Items) { ltrl.Text += "<i>" + item[sListColumnName] + "</i><br>"; } } } } else { lblDisplayError.Text = "Edit the Webpart and enter list name"; } this.Controls.Add(ltrl); } catch (Exception ex) { lblDisplayError.Text = ex.Message.ToString(); } } protected override void Render(HtmlTextWriter writeHtml) { try { lblListName.RenderControl(writeHtml); writeHtml.Write("<br /><br />"); lblColumnName.RenderControl(writeHtml); writeHtml.Write("<br /><br />"); ltrl.RenderControl(writeHtml); } catch (Exception ex) { //Log } } |
Finally bulid the Solution & deploy in your web application. And then, add our custom created web part on the page & go to “Edit Web Part Properties” of that web part. Now, we can see our custom created text boxes.
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.
Print article | This entry was posted by Peter on October 22, 2015 at 8:33 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |