Home » resources » Recent Articles:

SharePoint Toolkit

SharePoint – Hide actions menu items : A SharePoint solution allowing you to easily hide/show menu items from the Actions menu such as the datasheet view

SharePoint – SPForms Designer : A SharePoint solution allowing you to easily customize the new/edit/display forms associated with lists.

SharePoint Tool Basket MOSS Solution : all the solutions described below + the rating system packaged into a single solution. Do not install it if you have already installed one of the below solutions previously because the solution deployment would fail. If you want to install it, first remove any previsouly installed solution of the below list.

SharePoint – SPFeature Explorer : A windows application allowing developers to easily identify all the features installed in the farm and where they are active.

SharePoint – SPContentType Explorer : A SharePoint application page that's very similar to the standard content type pages but that allows you to see where a content type is used accross an entire site collection

SharePoint – Autocomplete lookup field with AJAX : An AJAX-Enabled SharePoint custom lookup field allowing users to benefit from the advantages of standard lookup fields with the rapidity of AJAX. Note that this solution doesn't require the AJAX extensions nor the .NET framework 3.5 to work. It works on both WSS & MOSS pre and post SP1

SharePoint – List Columns Manager : A SharePoint feature allowing site collection administrators to manage the columns of a list (set them in read-only, hide them, set them visible/invisible in the edit/new/display forms) and to visualize the list's associated event handlers and the CAML of each associated view

SharePoint – Audience to Groups convertor : A SharePoint webpart allowing site collection administrators to create SharePoint groups based on SharePoint audiences and to maintain an automatic synchronization .

http://www.codeplex.com/SharePointPSScripts/ : A set of useful PowerShell scripts that make life of IT & Developers easier

Module 3 – Code Snippets: Building Blocks for Web Part Development in SharePoint 2010

The following code shows how to use the ListViewByQuery control in a Web Part.
Note: The code also includes a CAML query statement that is used by an SPQuery object to set the items to be displayed in the ListViewByQuery control.

protected override void CreateChildControls()
{
  SPWeb thisWeb = SPContext.Current.Web;
  SPList tasks = thisWeb.Lists["Tasks"];
  ListViewByQuery listTasks = new ListViewByQuery();
  listTasks.List = tasks;
  SPQuery taskQuery = new SPQuery(listTasks.List.DefaultView);
  taskQuery.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='Due' />";
  taskQuery.Query = "<Where><Leq><FieldRef Name='Due' /><Value Type='DateTime'>"
    + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now.AddMonths(1))
    + "</Value></Leq></Where>";
  listTasks.Query = taskQuery;
  this.Controls.Add(listTasks);
  base.CreateChildControls();
}

 

The following code shows how to extract the selected people in a PeopleEditor control.
Note: The code runs in a Visual Web Part, and relies on there being a PeopleEditor control named peoplePicker, and an ASP.NET ListBox control called selectedPeople

namespace SharePointControls.ControlExample
{
  public partial class ControlExampleUserControl : UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void getPeople(object sender, EventArgs e)
    {   
      string allPeople = peoplePicker.CommaSeparatedAccounts;
      string[] selected = allPeople.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
      foreach (string p in selected)
      {
        selectedPeople.Items.Add(p);
      }
    }
  }
}

 

The following code shows how to use the SPGridView control in a Web Part to display list items from a list:

protected override void CreateChildControls()
{
  SPGridView taskGrid = new SPGridView();
  taskGrid.AutoGenerateColumns = false;
  BoundField taskTitle = new BoundField();
  taskTitle.DataField = "Title";
  taskTitle.HeaderText = "To Do…";
  taskGrid.Columns.Add(taskTitle);
  SPWeb thisWeb = SPControl.GetContextWeb(Context);
  SPList taskList = thisWeb.Lists["Tasks"];
  SPDataSource listSource = new SPDataSource();
  listSource.List = taskList;
  SPDataSourceView view = listSource.GetView();
  taskGrid.DataSource = listSource;
  if (view.CanSort) 
  {
    taskGrid.AllowSorting = true;
  }
  taskGrid.DataBind();
  Controls.Add(taskGrid);
  base.CreateChildControls();
}

Module 2 – Code Snippets: What Developers Need to Know About SharePoint 2010

The following code shows two event handlers:
  - The first one (FieldAdded) updates existing list items when a field is added to a list.
  – The second handler (ListDeleting) prevents the deletion of a list.

namespace ProductMarketing.ProductTaskEvents
{
  public class ProductTaskEvents : SPListEventReceiver
  {
    public override void FieldAdded(SPListEventProperties properties)
    {
      SPField newField = properties.Field;
      Guid fieldID = newField.Id;
      if (newField.Type == SPFieldType.Text)
      {
        SPList thisList = properties.List;
        foreach (SPListItem item in thisList.Items)
        {
          item[fieldID] = "Please update this existing item…";
          item.Update();
        }
      }
      base.FieldAdded(properties);
    }
    public override void ListDeleting(SPListEventProperties properties)
    {
      properties.ErrorMessage = "This list is critical to the marketing campaign and cannot be deleted.";
      properties.Cancel = true;
    }
  }
}

 

The following event handler shows how to update properties of a SharePoint Web site when a new Web is created

namespace ProductMarketing.ProductMarketingWebEvents
{
  public class ProductMarketingWebEvents : SPWebEventReceiver
  {
    public override void WebProvisioned(SPWebEventProperties properties)
    {
      SPWeb thisNewWeb = properties.Web;
      thisNewWeb.AllowUnsafeUpdates = true;
      thisNewWeb.Title = "Updated in Code";
      thisNewWeb.Update();
      thisNewWeb.AllowUnsafeUpdates = false;
      base.WebProvisioned(properties);
    }
  }
}

 

The following code shows how to enumerate the alerts in a SharePoint site, and also how to enable or disable alerts.
Note: This code is designed to run on an application page that includes:
  – An ASP.NET Label control called alertCount
  – An ASP.NET button that includes a Click event handler set to the EnableAll funtion
  – An ASP.NET button that includes a Click event handler set to the DisableAll funtion

namespace Alerter.Layouts.Alerter
{
  public partial class Alerts : LayoutsPageBase
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      int enabled = 0;
      int disabled = 0;
      SPWeb thisWeb = SPContext.Current.Web;
      foreach (SPAlert alrt in thisWeb.Alerts)
      {
        if (alrt.Status == SPAlertStatus.On)
        {  
          enabled++;
        }
        if (alrt.Status == SPAlertStatus.Off)
        {
          disabled++;
        }
      }
      alertCount.Text = "Enabled Alerts: " + enabled.ToString()
        + "\nDisabled Alerts" + disabled.ToString();
    }
    protected void EnableAll(object sender, EventArgs e)
    {
      SPWeb thisWeb = SPContext.Current.Web;
      foreach (SPAlert alrt in thisWeb.Alerts)
      {
        alrt.Status = SPAlertStatus.On;
        alrt.Update();
      }
    }
    protected void DisableAll(object sender, EventArgs e)
    {
      SPWeb thisWeb = SPContext.Current.Web;
      foreach (SPAlert alrt in thisWeb.Alerts)
      {
        alrt.Status = SPAlertStatus.Off;
        alrt.Update();
      }
    } 
  }
}

 

The following code creates a new SharePoint site (SPWeb object):

SPSite thisSite = SPContext.Current.Site;
thisSite.RootWeb.AllowUnsafeUpdates = true;
SPWeb hrWeb = thisSite.RootWeb.Webs.Add("HRWeb",
  "Human Resources Web",
  "For use by HR Employees",
  1033,
  "STS#0",
  false,
  false);
hrWeb.Dispose();
thisSite.RootWeb.AllowUnsafeUpdates = false;

 

The following code enumerates all of the services provided by the current farm's SPFarm object and displays them in a treeview control in an application page.
Note: The code relies on there being a tree-view named farmContents in the application page.

namespace Hierarchy.Layouts.Hierarchy
{
  public partial class HierarchyViewer : LayoutsPageBase
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      SPFarm thisFarm = SPFarm.Local;
      TreeNode node;
      farmContent s.Nodes.Clear();
      foreach(SPService svc in thisFarm.Services)
      {
        node = new TreeNode();
        node.Text = "(Type=" + svc.TypeName + ") " + svc.DisplayName;
        farmContents.Nodes.Add(node);
      }
    farmContents.CollapeAll();
  }
}

 

The following code enumerates all of the services, web applications, site collections, sites, and lists in a SharePoint farm, and displays them in a treeview control in an application page.
Note: The code relies on there being a tree-view named farmContents in the application page. Also note how the addWebs() function is called recursively to ensure sites and subsites are enumerated and displyed in the treeview.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;

namespace Hierarchy.Layouts.Hierarchy
{
  public partial class HierarchyViewer : LayoutsPageBase
  {
  protected void Page_Load(object sender, EventArgs e)
  {
    SPFarm thisFarm = SPFarm.Local;
    TreeNode node;
    farmContents .Nodes.Clear();
    foreach(SPService svc in thisFarm.Services)
    {
      node = new TreeNode();
      node.Text = "(Type=" + svc.TypeName + ") " + svc.DisplayName;
      farmContents.Nodes.Add(node);
      TreeNode svcNode = node;
      if (svc is SPWebService)
      {
        SPWebService webSvc = (SPWebService)svc;
        foreach (SPWebApplication webApp in webSvc.WebApplications)
        {
          node = new TreeNode();
          node.Text = webApp.DisplayName;
          svcNode.ChildNodes.Add(node);
          TreeNode webAppNode = node;
          foreach (SPSite site in webApp.Sites)
          {

          try {
            node = new TreeNode();
            node.Text = site.Url;
            webAppNode.ChildNodes.Add(node);
            TreeNode siteNode = node;
            site.CatchAccessDeniedException = false;
            try
            {
              node = new TreeNode(site.RootWeb.Title, null, null, site.RootWeb.Url, "_self");
              siteNode.ChildNodes.Add(node);
              TreeNode parentNode = node;
              foreach (SPList list in site.RootWeb.Lists)
              {
                node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
                parentNode.ChildNodes.Add(node);
              }
              foreach (SPWeb childWeb in site.RootWeb.Webs)
              {

              try {
                addWebs(childWeb, parentNode);

              } finally 

              {

                childWeb.Dispose();

              }
              }
            }
            catch
            { }

            } 

            finally 

            {

              site.Dispose();

            }
            }
          }
        }
      }
      farmContents.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)
      {
        node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
        parentNode.ChildNodes.Add(node);
      }
      foreach (SPWeb childWeb in web.Webs)
      {

        try {
        addWebs(childWeb, parentNode);

        } finally {

          childWeb.Dispose();

        }
      }
    }
  }
}

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;
}

SharePoint 2010: Getting Started with Development on SharePoint 2010 Hands-on Labs in C# and Visual Basic

Use these 10 hands-on lab manuals for SharePoint 2010 to get started learning SharePoint 2010 development. Each lab is available in C# and Visual Basic.

HOL01 – Developing a Visual Web Part in Visual Studio 2010
This hands-on lab introduces the Visual Studio 2010 SharePoint development environment. It shows how to build a Visual Web Part using LINQ to SharePoint, and how to connect one Web Part to another Web Part on the page.

HOL02 – Developing a List Definition and Event Receiver in Visual Studio 2010
This hands-on lab walks you through building a list definition for SharePoint 2010 in Visual Studio 2010. It also shows how to build an event receiver for the list in Visual Studio 2010 and deploy it to SharePoint. After the list and event receiver are deployed, you can use the developer dashboard to evaluate the performance of the event receiver.

HOL03 – Developing Advanced Web Parts for SharePoint 2010 with Visual Studio 2010
This hands-on lab shows how to build a Web Part using several SharePoint-specific controls in Visual Studio 2010. Investigate advanced built-in Web Parts, including the Data View Web Part.

HOL04 – Developing with LINQ to SharePoint in Visual Studio 2010
This hands-on lab explores a variety of LINQ queries on SharePoint 2010, going into more depth than the introductory hands-on lab. It also walks you through an exercise of creating a custom content type in Visual Studio 2010.

HOL05 – Developing for SharePoint 2010 with the Client OM and REST in Visual Studio 2010
This hands-on lab introduces the Client object model for use in calling SharePoint 2010 APIs from a client machine. It also shows the use of ADO.NET Data Services to call REST services in SharePoint 2010.

HOL06 – Developing a BCS External Content Type with Visual Studio 2010
This hands-on lab walks you through building an external content type for Business Connectivity Services using Visual Studio 2010. It also builds a form for Microsoft Outlook and shows the data being edited offline in Outlook.

HOL07 – Developing a SharePoint 2010 Workflow with Initiation Form in Visual Studio 2010
This hands-on lab walks you through building a workflow in Visual Studio 2010 for SharePoint 2010. You add an initiation form to the workflow and use an external data exchange activity in the workflow.

HOL08 – Developing SharePoint 2010 User Interface with Silverlight in Visual Studio 2010
This hands-on lab walks you through building Microsoft Silverlight applications for use in SharePoint 2010. You will access SharePoint 2010 data in Silverlight using the Client object model.

HOL09 – Developing SharePoint 2010 Sandboxed Solutions in Visual Studio 2010
This hands-on lab walks you through building a Sandboxed Solution Web Part for SharePoint 2010. It will also add code to the Web Part that overloads the limits placed by the sandboxed solution, and you will review how the solution is shut down.

HOL10 – Developing SharePoint 2010 User Interface Ribbon and Dialog Customizations
This hands-on lab walks you through adding a custom action to the SharePoint 2010 ribbon, and creating a Web Part that uses the Dialog Framework