Module 1: Code Snippets: Getting Started: Building Web Parts in SharePoint 2010

The following code shows how to modify the Text property of a label when a Visual Web Part loads.
NOTE: This code assumes there is an ASP.NET Label control called message in the Visual Web Part

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BonnevilleTestBed.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
message.Text = "Welcome to SharePoint 2010 Development";
}
}
}

 

The following code shows how to render a welcome message when a standard Web Part loads:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace BonnevilleTestBed.BonnevilleStandardWP
{
[ToolboxItemAttribute(false)]
public class BonnevilleStandardWP : WebPart
{
public BonnevilleStandardWP()
{
}

protected override void CreateChildControls()
{
//Get the currently logged-on user's name from the SharePoint object model
string username = SPContext.Current.Web.CurrentUser.LoginName;
LiteralControl myMessage = new Literalcontrol("<H3>Welcome " + userName + " to SharePoint 2010 Development</H3>");
this.Controls.Add(myMessage);
base.CreateChildControls();
}

protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
}
}

 

The following code shows how to iterate through all lists and subwebs in a SharePoint site, and add them to a treeview control in a Visual Web Part.
Note: This code assumes there is a TreeView control called siteStructure in the Visual Web Part. Note also how the addWebs() method is called recursively.

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

    }
}
}

 

The following code shows how to use a DateTimeControl and ListViewByQuery control to display tasks that are due before the date chosen by the user.
Note: The DateTimeControl includes an event handler that refreshes the query definition for the ListViewByQuery control when the user selects a different date.

protected override void CreateChildControls()
{
SPWeb thisWeb = null;
DateTimeControl filterDate = new DateTimeControl();
filterDate.DateOnly = true;
filterDate.AutoPostBack = true;
thisWeb = SPContext.Current.Web;
filterDate SelectedDate = DateTime.Today;
filterDate.DateChanged += new EventHandler(filterDate_DateChanged);
this.Controls.Add(filterDate);
MyCustomView = new ListViewByQuery();
MyCustomView.List = thisWeb.Lists["Tasks"];
SPQuery query = new SPQuery(MyCustomView.List.DefaultView);
query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Due' />";
//CAML Query. Note that there must be a using statement for
//Micoroft.SharePoint.Utilities to use SPUtility class as shown
  query.Query = "<Where><Leq><FieldRef Name='Due' />"
+ "<Value Type='DateTime'>"
+ SPUtility.CreateISO8601DateTimeFromSystemDateTime(FilterDate.SelectedDate)
+"</Value></Leq></Where>";
MyCustomView.Query = query;
this.Controls.Add(new LiteralControl("<br />"));
this.Controls.Add(MyCustomView);
EnsureChildControls();
base.CreateChildControls();
}
void filterDate_DateChanged(object sender, EventArgs e)
{
DateTimeControl filterDate = (DateTimeControl)sender;
SPQuery query = new SPQuery(MyCustomView.List.DefaultView);
query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Due' />";
query.Query = "<Where><Leq><FieldRef Name='Due' />"
+ "<Value Type='DateTime'>"
+ SPUtility.CreateISO8601DateTimeFromSystemDateTime(FilterDate.SelectedDate)
+ "</Value></Leq></Where>";
MyCustomView.Query = query;
}