220 likes | 232 Views
Learn how to develop standard, connected, and visual web parts in SharePoint 2010 using Visual Studio 2010. Explore creating connection interfaces, provider and consumer web parts, and developing user interfaces and code for visual web parts.
E N D
Module 3 Developing SharePoint 2010 Web Parts
Module Overview • Developing Standard Web Parts • Developing Connected Web Parts • Developing Visual Web Parts • Lab: Creating SharePoint 2010 Web Parts by Using Visual Studio 2010
Lesson 1: Developing Standard Web Parts • What Is a Standard Web Part? • Adding Standard Web Parts to Visual Studio 2010 Projects • Overriding Base Class Methods • Exposing Custom Properties in Web Parts
What Is a Standard Web Part? • User's Perspective • Administrator's Perspective • Developer's Perspective • Class that inherits from System.Web.UI.WebControls.WebParts.WebPart
Adding Standard Web Parts to Visual Studio 2010 Projects • Web Part is a SharePoint 2010 project item • Class is pre-configured with common using statements • Typical namespaces used in ASP.NET Web Parts • SharePoint namespaces • Adding additional namespaces • Class is pre-configured with an override for the most common method • CreateChildControls • Adding controls to the programmatic control tree • Overriding additional methods or properties
Overriding Base Class Methods //Class-level object variables DateTimeControlfilterDate; ListViewByQueryMyCustomView; SPQuery query; protected override void CreateChildControls() { filterDate = new DateTimeControl(); filterDate.DateOnly = true; filterDate.AutoPostBack = true; filterDate.DateChanged += new EventHandler(filterDate_DateChanged); SPWebthisWeb = SPContext.Current.Web; MyCustomView = new ListViewByQuery(); MyCustomView.List = thisWeb.Lists["Interviews"]; query = new SPQuery(MyCustomView.List.DefaultView); query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='DueDate' />"; MyCustomView.Query = query; LiteralControlfilterMessage = new LiteralControl("Tasks due by:"); this.Controls.Add(filterMessage); this.Controls.Add(new LiteralControl("<br />")); this.Controls.Add(filterDate); this.Controls.Add(new LiteralControl("<br />")); this.Controls.Add(MyCustomView); } //Event handler for component in the control tree void filterDate_DateChanged(object sender, EventArgs e) { ... }
Exposing Custom Properties in Web Parts //Private class-level variable for internal storage string myVariable = "My Default Value"; //Web Part property decoration [WebBrowsable(true), WebDisplayName("My Custom Property"), WebDescription("Provide a value for the custom property"), Personalizable(PersonalizationScope.Shared), Category("My Category")] public string MyProperty { get { return(myVariable); } set { myVariable = value; } } //Use the property protected override void CreateChildControls() { this.Controls.Add(new LiteralControl(this.myVariable)); }
Lesson 2: Developing Connected Web Parts • What Are Connected Web Parts? • Creating Connection Interfaces • Creating Provider Web Parts • Creating Consumer Web Parts • Connecting Web Parts in SharePoint Sites
What Are Connected Web Parts? • Two or more Web Parts that share data • Provider Web Parts • Consumer Web Parts • Provider and Consumer Web Parts • Scenarios • Master/details views of SharePoint Data • Other linked functionality
Creating Connection Interfaces public interface IJobDef { //Declare interface members string JobDef { get; set; } }
Creating Provider Web Parts public class JobDefinitions : WebPart, IJobDef { //Private class-level variable for internal storage string _JobDef; //Implement JobDef from interface public string JobDef { get { return (_JobDef); } set { _JobDef = value; } } //Create provider connection point [ConnectionProvider("Job")] public IJobDefSendJobName() { return (this); } }
Creating Consumer Web Parts //Private class-level variable for internal storage string jobDefinition = string.Empty; //Create consumer connection point for a method //that consumes the interface [ConnectionConsumer("Job")] public void GetJob(IJobDef Job) { if(Job != null) { jobDefinition = Job.JobDef; } }
Connecting Web Parts in SharePoint Sites • Edit provider or consumer Web Part. • Use the Web Part menu for provider Web Parts • Send <object> To Or • Use the Web Part menu for consumer Web Parts • Get <object> From
Lesson 3: Developing Visual Web Parts • What Is a Visual Web Part? • Adding Visual Web Parts to Visual Studio 2010 Projects • Developing User Interfaces for Visual Web Parts • Developing Code for Visual Web Parts • Deploying Visual Web Parts from Visual Studio
What Is a Visual Web Part? • User's Perspective • Web Part • Administrator's Perspective • ASP.NET user control in the SharePoint file system • Developer's Perspective • ASP.NET User Control with UI designers • Standard Web Part that loads the user control at run time
Adding Visual Web Parts to Visual Studio 2010 Projects • Visual Web Part is a SharePoint 2010 project type, and a SharePoint project item type • ASP.NET user control is added to the project • Designer and markup (.ASCX) • Code-behind file (.CS or .VB) • Standard Web Part is added to the project • Includes Page.LoadControl() and Controls.Add() statements • Adding SharePoint namespaces to the user control
Developing User Interfaces for Visual Web Parts • Design view • Source view • Split view • Adding user interface elements • Toolbox • Markup ... // Add UI Elements <asp:TreeView ID="overviewTree" runat="server" ShowLines="true" EnableViewState="true"> </asp:TreeView>
Developing Code for Visual Web Parts // Add required namespaces using Microsoft.SharePoint; public partial class OverviewUserControl : UserControl { protected void Page_Load(object sender, EventArgs e) { //Create, add, and manipulate UI elements overviewTree.Nodes.Clear(); //Access SharePoint objects SPWebthisWeb = SPContext.Current.Web; ... } }
Deploying Visual Web Parts from Visual Studio • As simple as deploying a standard Web Part • What really happens? • Visual Web Part restrictions • Sandboxed solutions
Lab: Creating SharePoint 2010 Web Parts by Using Visual Studio 2010 • Exercise 1: Creating, Deploying, and Debugging a Simple Web Part by Using Visual Studio 2010 • Exercise 2: Using SharePoint Components in a Web Part • Exercise 3: Creating a Visual Web Part by Using Visual Studio 2010 Logon information Estimated time: 60minutes
Lab Review • What type of project did you create to start with? • How did you create the first standard Web Part? • What components did you use in the standard Web Part? • How did you create the second Web Part? • How did you design the user interface for the Visual Web Part? • Where did you add code for the Visual Web Part?
Module Review • In this module, you have learned to: • Develop Standard Web Parts • Develop Connected Web Parts • Develop Visual Web Parts