Module 6 – Code Snippets: Accessing External Data with Business Connectivity Services in SharePoint 2010

The following code snippets are taken from a demonstration video included in Module 6 of the Getting Started Course for SharePoint 2010 Developers (http://www.mssharepointdeveloper.com) This first snippet defines a simple class that was used in the demo. This class is used to represent the Customer entity, and contains public properties that match the columns in…

0

Module 10 – Code Snippets: Creating Dialogs and Ribbon Controls for SharePoint 2010

The following markup is a simple example of how to add a control to the Site Actions menu. This example adds a menu item that navigates the user directly to the Solutions Gallery, without requiring them to go to the Site Settings page first:  <?xml version=”1.0″ encoding=”utf-8″?><Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>  <CustomAction Id=”NewUIActionsMenu”    GroupId=”SiteActions”    Location=”Microsoft.SharePoint.StandardMenu”    Sequence=”1970″    Title=”Manage Solutions”>    <UrlAction Url=”/_catalogs/solutions/Forms/AllItems.aspx” /> …

1

Module 9 – Code Snippets: Sandboxed Solutions for Web Parts in SharePoint 2010

The following example shows code that attempts to run in a sandboxed solutuon. There are four buttons, each of which attempt a different type of action, as follows:  – renderWebInfo_Click: This operation will be allowed to run in the sandbox.  – renderWebInfoElevated_Click: This operation will not be allowed to run in the sandbox. The sandbox will…

0

Module 7 – Code Snippets: Developing Business Processes with SharePoint 2010 Workflows

The following code shows how to return data from a workflow initiation form to the workflow.Note: This code relies on there being an ASP.NET TextBox on the initiation form named MaxPrice. Note also that the code snippet that follows this one uses the data returned. namespace ApproveRejectProducts.Workflow1{  public partial class GetMaxPrice : LayoutsPageBase  {    protected void Page_Load(object…

0

Module 8 – Code Snippets: Creating Silverlight User Interfaces for SharePoint 2010 Solutions

The following code shows how to use the SharePoint client object model in a Silverlight application. The code performs the following actions in the Silverlight application:  – Obtains a reference to the current SharePoint site in the application’s APP.XAML file  – Updates a progress bar and status label in real time, based on accessing and retrieving data from…

3

Module 5 – Code Snippets: Accessing SharePoint 2010 Data with Client-Side APIs

The following code shows how to use the client object model from a console application. The code first of all lists the Web templates available for a specific Web, and then creates a new Web based on a specific template: ClientContext clientCtx = new ClientContext(“http://intranet.contoso.com”);Site thatSite = clientCtx.Site;clientCtx.Load(thatSite.RootWeb);Web thatWeb = thatSite.RootWeb;WebTemplateCollection templates = thatWeb.GetAvailableWebTemplates(1033, true);clientCtx.Load(templates);clientCtx.ExecuteQuery();foreach…

0

Module 4 – Code Snippets: Accessing SharePoint 2010 Data with Server-Side APIs

The following code shows how to obtain a reference to a specific Web, then to a specific list, and then how to enumerate the fields in the list: SPSite thisSite = SPContext.Current.Site;using (SPWeb targetWeb = thisSite.AllWebs[“Project Management”]){  SPList targetList = targetWeb.Lists[“Announcements”];  foreach (SPField fld in targetList.Fields)  {    if(!fld.Hidden)    {      LiteralControl output = new LiteralControl(fld.InternalName +…

0

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 =…

0

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 =…

0

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…

0