Home » code » Recent Articles:

What’s New for Developers in SharePoint 2010

What’s New for Developers in SharePoint 2010

 

 

Presented by Paul Andrew, Microsoft Corporation
Length: 19 minutes 56 seconds

 

What's New for Developers?

MSDN Webcast: SharePoint Server 2010 (Part 1 of 8): What's New for Developers (Level 100) MSDN Webcast: SharePoint Server 2010 (Part 1): What's New for Developers (Level 100)

In this webcast, we discuss SharePoint Server 2010 from a developer's perspective, and we provide code-based demonstrations of the major new features for building user interfaces, building on the data platform, and general programmability.

MSDN Code Gallery: Microsoft SharePoint Server 2010: Deprecated Types and Methods MSDN Code Gallery: Microsoft SharePoint Server 2010: Deprecated Types and Methods

This resource contains text files that list all of the deprecated types and methods in SharePoint Server 2010 and SharePoint Server 2007.

New Developer Features in SharePoint Foundation 2010

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();

        }
      }
    }
  }
}