In this task, you create a Visual Web Part project in Microsoft Visual Studio 2010.
Figure 1. Select the Visual Web Part project type
. Figure 2: The SampleWebPart project in the Solution Explorer
In this task, you add a Tree View control to the design surface of the Web Part. The Tree View control displays a hierarchical view of lists and Sub Webs.
In this task, you add Microsoft Visual C# code to the project that iterates through all lists and SubWebs in the SharePoint site, and adds them to the TreeView control.
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using System.Web; namespace BonnevilleTestBed.Bonneville { public partial class BonnevilleUserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { SPWeb thisWeb = null; TreeNode node; thisWeb = SPContext.Current.Web; //Add the Web's title as the display text for the tree node, and add the URL as the NavigateUri. node = new TreeNode(thisWeb.Title, null, null, thisWeb.Url, "_self"); //The Visual Web Part has a treeview control called siteStructure. siteStructure.Nodes.Add(node); //Get a reference to the current node, so child nodes can be added in the correct position. TreeNode parentNode = node; //Iterate through the Lists collection of the Web. foreach (SPList list in thisWeb.Lists) { if (!list.Hidden) { node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self"); parentNode.ChildNodes.Add(node); } } foreach (SPWeb childWeb in thisWeb.Webs) { //Call our own helper function for adding each child Web to the tree. addWebs(childWeb, parentNode); childWeb.Dispose(); } siteStructure.CollapseAll(); } void addWebs(SPWeb web, TreeNode parentNode) { TreeNode node; node = new TreeNode(web.Title, null, null, web.Url, "_self"); parentNode.ChildNodes.Add(node); parentNode = node; foreach (SPList list in web.Lists) { if (!list.Hidden) { node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self"); parentNode.ChildNodes.Add(node); } } foreach (SPWeb childWeb in web.Webs) { //Call the addWebs() function from itself (i.e. recursively) //to add all child Webs until there are no more to add. addWebs(childWeb, parentNode); childWeb.Dispose(); } } } }
Learn the core features of SharePoint and become master with our expertise tutorials.
Life Cycle of SharePoint Webparts:
OnInit – Configuration values set using WebBrowsable properties and those in web part task pane are loaded into the web part.
OnLoad User Generated Event – for e.g. button click on the web part.
CreateChildControls – All the controls specified are created and added to controls collection. When the page is being rendered for the first time the method generally occurs after the OnLoad() event. In case of postback, it is called before the OnLoad() event. We can make use of EnsureChildControls() - It checks to see if the CreateChildControls method has yet been called, and if it has not, calls it.
LoadViewState – The view state of the web part is populated over here.
OnPreRender – Here we can change any of the web part properties before the control output is drawn.
RenderContents – Html Output is generated.
Create a SharePoint 2010 Web Part that is meaningful and that utilizes the following features:
The Web Part I’ll be demoing here doesn’t do much of anything other than show you how to accomplish all the above features giving you the power to adapt the code so you can make a meaningful Web part for your own specific needs. Let’s begin: This walkthrough provides the steps for creating a basic custom Web Part that can be added to your site pages. It is a simple Web Part that enables the user to define a custom message that is displayed inside the Web Part. This Web Part derives from the Microsoft ASP.NET Web Part class, which is the recommended practice for Microsoft SharePoint Foundation. Prerequisites ASP.NET & SharePoint development tools in Microsoft Visual Studio 2010 Using visual studio we can develop two types of web parts will discuss in this module
In this task, you build and deploy the Web Part project. Build and deploy the project by using one of the following options:
Create a Web Parts Page In this task, you create a Web Parts page to contain the Web Part, unless one has already been created for you.
Figure 3: A sample Web Part page Add the Web Part to the Web Parts Page In this task, you add the Web Part to the Web Parts page and test the solution.
Figure 4. The Web Part after being added to the zone in the Web Part page
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.