Getting Started Building Web Parts in SharePoint 2010

 Alik Levin    I followed the instructions provided in Module 1: Getting Started Building Web Parts in SharePoint 2010 webcast. This webcast teaches the following:

  • Create standard ASP.NET Web Parts for SharePoint 2010.
  • Create Visual Web Parts for SharePoint 2010.
  • Use SharePoint controls and data in Web Parts.

Quick Resource Box

The following walkthrough assumes you have Sharepoint 2010 Foundation installed on your dev station. Otherwise consider following the steps outlined in Setting Up the Development Environment for SharePoint Foundation on Windows 7.

Step-by-step Walkthrough

  • Open Visual Studio 2010 in elevated mode.
  • Click “New Project” on Start Page.
  • Under “Installed Templates”,  Expand Sharepoint node and choose 2010.
  • Choose “Visual Web Part” project template, name it MyVisualWebPartProject or any other to your desire.
  • Click OK. Specify your computer name where Sharepoint installed (for example, https://alikl14/) and click Finish button.
  • Drag treeview control onto VisualWebPart1UserControl.ascx at the end of the file, it should look similar to this:

<%\@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%\@ Assembly Name="Microsoft.Web.CommandUI, Version=14..." % <%\@ Register Tagprefix="SharePoint..." %>

...

<%\@ Control Language="C#" AutoEventWireup="true..." %> <asp:TreeView ID="siteStructure" runat="server"> </asp:TreeView>

  • Open the VisualWebPart1UserControl.ascx.cs file
  • Add the following using statement at the top:

using Microsoft.SharePoint;

  • Add the following code to the Page_load event handler:

            SPWeb thisWeb = null;             try             {                 TreeNode node;                 thisWeb = SPContext.Current.Web;                 node = new TreeNode(thisWeb.Title, null, null, thisWeb.Url, "_self");                 siteStructure.Nodes.Add(node);                 TreeNode parentNode = node;                 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)                 {                     addWebs(childWeb, parentNode);                 }             }             catch (Exception)             {                 throw;             }             finally             {                 thisWeb.Dispose();             }

  • Add also the following function to the file:

        private 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)             {                 addWebs(childWeb, parentNode);             }

 

  • Build the project
  • In the solution explorer right click MyWebPartProject and choose “Deploy”.
  • Navigate to your Sharepoint using browser, in my case https://alikl14/
  • Switch to “Edit” mode – click on notepad like icon on up right corner.
  • Click on “Insert” under “Editing Tools”.
  • Click on “Web part”.
  • Click on “Custom” on the left.
  • Choose VisualWebPart1 and drag it on the page.
  • Click “Save” [diskette icon].
  • You should see the tree view control displaying your Sharepoint content.

Related Books