SlideShare a Scribd company logo
Building SharePoint
Web Parts




Rob Windsor
rwindsor@portalsolutions.net
@robwindsor
What Are Web Parts?
• Small “chunks” of user interface and functionality
• Aggregated together to build page content
• Managed by end-users
   Add/remove web parts on a page
   Determine where web parts are placed on the page
   Set web part properties
• Concept and implementation not specific to
  SharePoint or ASP.NET
DEMO
iGoogle
Web Part History
• SharePoint 2003
   First version with web part infrastructure
   Web part types and infrastructure are specific to SharePoint
• ASP.NET 2.0
   Web part types and infrastructure added to ASP.NET
• SharePoint 2007
   Adds support for ASP.NET web parts
   Continues support for SharePoint web parts for backwards
    compatibility
• SharePoint 2010
   Same support as SharePoint 2007 in farm solutions
   Adds support for web parts in sandboxed solutions
Working with SharePoint Web Parts
• Web parts can be added to web part zones or wiki content
• Each web part has common “chrome”
    Title, border, verbs menu
• Closing a web part hides it; deleting a web part removes it
  from page
Web Part Properties
• Web parts properties used to tailor display and functionality
    Zip/Postal code in weather web part
    Stock code in stock ticker web part
• Properties support customization and personalization
    Customization effects shared view of part
    Personalization effects current users view of part
Web Part Gallery
• Site collection scoped
• Both .DWP and .WEBPART files as items
• SharePoint can discover new candidates from
  Web.config
• Maintain metadata
• Available out-of-the-box parts determined by
  edition and site template
Web Part Gallery
DEMO
Working with Web Parts
SharePoint Web Part Page Structure
• Inherits from WSS WebPartPage base class
• Contains one SPWebPartManager control
• Contains one or more WSS WebPartZone controls
          SPWebPartManager


        WebPartZone (Left)   WebPartZone (Right)    Editor Zone

                                                   Editor Part 1
           Web Part 1            Web Part 3

                                                   Editor Part 2
                                 Web Part 4
           Web Part 2

                                                   Catalog Zone
                                 Web Part 5
                                                   Catalog Part 1


                                                   Catalog Part 2
Building a Simple Web Part
• ASP.NET web parts are server controls
    WebPart type derives from Panel
    User interface described with code
• Override RenderContents
    Use HTML markup and JavaScript to build interface
• Override CreateChildControls
    Use Server Controls to build interface
• Two methods can be combined
  protected override void RenderContents(HtmlTextWriter writer)
  {
      writer.RenderBeginTag(HtmlTextWriterTag.H2);
      writer.Write("Hello, World");
      writer.RenderEndTag();
  }
Chrome and Web Part Gallery Properties
• Can set web part properties to change chrome
  values
• Can be done in three places
   Using code in the web part constructor
   Using CAML in the .webpart file
   Editing the web part properties in the browser
• Web part gallery group name set in element
  manifest
DEMO
Building a Simple Web Part
Composite Web Parts
• Contain server controls as children
• Declare controls as fields
• In CreateChildControls
     Create controls
     Set properties
     Add controls to web part Controls collection
     Use LiteralControl to add literal markup
• Populate controls in OnPreRender
Composite Web Parts
protected DropDownList ListsDropDown;
protected GridView ItemsGridView;

protected override void CreateChildControls() {
    ListsDropDown = new DropDownList();
    ListsDropDown.AutoPostBack = true;
    ListsDropDown.SelectedIndexChanged +=
        new EventHandler(ListsDropDown_SelectedIndexChanged);
    Controls.Add(ListsDropDown);
    Controls.Add(new LiteralControl("<br/><br/>"));

    ItemsGridView = new GridView();
    Controls.Add(ItemsGridView);
}
DEMO
Building a Composite Web
Part
Deploying Web Parts
• Add .webpart file to web part gallery
• Register assembly that implements the web part as
  safe control in web.config
• Copy assembly to:
   Global Assembly Cache
   bin folder of IIS Web Application
      Executes with reduced trust
Deploying Web Parts - 2
• Visual Studio tools automate deployment process
  during development
• Visual Studio deployment has conflict detection
   Will replace .webpart file in gallery
• Deployment via Powershell or stsadm does not
  have conflict detection
   Deployed .webpart files will not be replaced
DEMO
Web Part Deployment
Web Parts in Sandboxed Solutions
• Split page rendering
    Page code runs in ASP.NET worker process
    Sandboxed web part code runs in sandbox worker process
    HTML markup and JavaScript from two worker processes merged
• Sandbox restrictions
      Reduced set of server object model API
      Restricted access outside SharePoint
      No elevation of permissions
      Cannot deploy files to SharePoint system folders
      Must be ASP.NET web parts
      Can only use ASP.NET controls
      No web part connections
Visual Web Parts
•    Visual web parts combine a web part with a user control
•    User controls have full design experience in Visual Studio
•    User interface and code behind defined in user control
•    Web part “injects” user control dynamically using
     Page.LoadControl
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/.../MyUserControl.ascx";

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }
Visual Web Parts and Sandboxed Solutions
• Native visual web part template not compatible
  with sandboxed solutions
   Attempts to deploy user control to system folders
• Visual Studio Power Tools contains a sandbox
  compatible template
   Markup in the user control is converted to code at design
    time
DEMO
Visual Web Parts
Web Part Properties
• Web Parts support persistent properties
• Configure properties via attributes
    Personalizable(param)
       Directs SharePoint to persist property value
       Parameter indicates if property may be personalized
    WebBrowsable(true)
       Directs SharePoint to generate interface to edit property
    WebDisplayName, WebDescription
       The prompt and tooltip that accompany the data entry element
    Category
       The group in which the properties will be shown

             [Personalizable(PersonalizationScope.Shared)]
             [WebBrowsable(true)]
             [WebDisplayName("Year")]
             [Category("Pluralsight")]
             public int Year { get; set; }
DEMO
Web Part Properties
Deactivating Web Part Features
• Deactivating a Feature that provisions .webpart
  files to the web part gallery effectively does nothing
    Files provisioned by a module are not automatically
     removed on deactivation
    That is, custom web parts remain in the gallery after the
     Feature has been deactivated
• You should add code to the feature receiver to
  remove the .webpart files from the gallery on
  deactivation
Deactivating Web Part Features
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    var site = properties.Feature.Parent as SPSite;
    if (site == null) return;

    var web = site.RootWeb;
    var list = web.Lists["Web Part Gallery"];

    var partNames = new string[] {
        "Simple.webpart", "Composite.webpart", "Visual.webpart",
        "Sandboxed.webpart", "Properties.webpart"
    };

    foreach (var partName in partNames)
    {
        var item = list.Items.OfType<SPListItem>().
            SingleOrDefault(i => i.Name == partName);
        if (item != null) item.Delete();
    }
}
DEMO
Web Part Properties
Creating Connectable Web Parts
• Pass data from one web part to another
• Loosely-coupled connection
     Communication managed by WebPartManager
     Provider web part supplies data
     Consumer web parts retrieve data
• Interface used to define data contract
     ASP.NET has standard connection contracts
     Can use custom connection contracts
• SharePoint provides user interface elements to establish connections
• Not compatible with sandboxed solutions
Web Part Connections
DEMO
Web Part Connections

More Related Content

PDF
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
PDF
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
PPTX
Gabriel Gayhart - XML Pointer File Example
PPTX
Advanced SharePoint Web Part Development
PDF
Envision IT - Application Lifecycle Management for SharePoint in the Enterprise
PDF
Externalizing Chatter Using Heroku, Angular.js, Node.js and Chatter REST APIs
PPT
.net training | learn .net | Microsoft dot net Course | Microsoft dot net onl...
PPT
Webcast Wcm
Advanced SharePoint 2010 and 2013 Web Part Development by Rob Windsor - SPTec...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Gabriel Gayhart - XML Pointer File Example
Advanced SharePoint Web Part Development
Envision IT - Application Lifecycle Management for SharePoint in the Enterprise
Externalizing Chatter Using Heroku, Angular.js, Node.js and Chatter REST APIs
.net training | learn .net | Microsoft dot net Course | Microsoft dot net onl...
Webcast Wcm

What's hot (20)

PDF
Oracle ADF Task Flows for Beginners
PPTX
SharePoint 2016 Platform Adoption Lessons Learned and Advanced Troubleshooting
PPTX
Introduction to ASP.Net MVC
PPTX
Asp.Net Mvc
PDF
ASP.NET Overview - Alvin Lau
PPTX
SFDC UI - Introduction to Visualforce
PPTX
New Features of ASP.NET 4.0
PPSX
ASP.NET Web form
PPS
11 asp.net session16
PDF
Integrating ASP.NET AJAX with SharePoint
PPTX
SharePoint Development (Lesson 3)
PPTX
SharePoint 2010 Client-side Object Model
PPTX
Oracle ADF Case Study
PPT
ASP.NET 4.0 Roadmap
PPTX
SFDC UI - Advanced Visualforce
PDF
PPTX
Sps redmond 2014 deck
PPSX
All About Asp Net 4 0 Hosam Kamel
PPS
03 asp.net session04
PDF
Grails patterns and practices
Oracle ADF Task Flows for Beginners
SharePoint 2016 Platform Adoption Lessons Learned and Advanced Troubleshooting
Introduction to ASP.Net MVC
Asp.Net Mvc
ASP.NET Overview - Alvin Lau
SFDC UI - Introduction to Visualforce
New Features of ASP.NET 4.0
ASP.NET Web form
11 asp.net session16
Integrating ASP.NET AJAX with SharePoint
SharePoint Development (Lesson 3)
SharePoint 2010 Client-side Object Model
Oracle ADF Case Study
ASP.NET 4.0 Roadmap
SFDC UI - Advanced Visualforce
Sps redmond 2014 deck
All About Asp Net 4 0 Hosam Kamel
03 asp.net session04
Grails patterns and practices
Ad

Viewers also liked (16)

PDF
Trono di spade1
PPTX
SharePoint Logging and Debugging: The Troubleshooter’s Best Friend by Jason H...
PPT
Counseling
PPTX
Part II: SharePoint 2013 Administration by Todd Klindt and Shane Young - SPTe...
PPTX
SharePoint Performance: Best Practices from the Field by Jason Himmelstein - ...
PPTX
Winchester Bluff Presentation 3_1_13
PPTX
Part I: SharePoint 2013 Administration by Todd Klindt and Shane Young - SPTec...
PPTX
How to best setup SharePoint 2013, Web Apps, Workflow Manager with Powershell
PPTX
Sponsored Session: Driving the business case and user adoption for SharePoint...
PPTX
Demystifying share point site definitions SharePoint 2007
PPTX
Microsoft Keynote by Richard Riley - SPTechCon
PPTX
SharePoint Search Center Configuration by Matthew McDermott - SPTechCon
PPTX
Solving Enterprise Search Challenges with SharePoint 2010 by Matthew McDermot...
PPTX
Tutorial: Building Apps for SharePoint 2013 Inside and Outside of the Firewal...
PPTX
Farewell XSL, Welcome Display Templates
PDF
NOW I Get It... What SharePoint Is, and Why My Business Needs It by Mark Rack...
Trono di spade1
SharePoint Logging and Debugging: The Troubleshooter’s Best Friend by Jason H...
Counseling
Part II: SharePoint 2013 Administration by Todd Klindt and Shane Young - SPTe...
SharePoint Performance: Best Practices from the Field by Jason Himmelstein - ...
Winchester Bluff Presentation 3_1_13
Part I: SharePoint 2013 Administration by Todd Klindt and Shane Young - SPTec...
How to best setup SharePoint 2013, Web Apps, Workflow Manager with Powershell
Sponsored Session: Driving the business case and user adoption for SharePoint...
Demystifying share point site definitions SharePoint 2007
Microsoft Keynote by Richard Riley - SPTechCon
SharePoint Search Center Configuration by Matthew McDermott - SPTechCon
Solving Enterprise Search Challenges with SharePoint 2010 by Matthew McDermot...
Tutorial: Building Apps for SharePoint 2013 Inside and Outside of the Firewal...
Farewell XSL, Welcome Display Templates
NOW I Get It... What SharePoint Is, and Why My Business Needs It by Mark Rack...
Ad

Similar to Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor - SPTec… (20)

PPTX
Parallelminds.web partdemo1
PPTX
Parallelminds.web partdemo
PPTX
SharePoint 2010 Training Session 5
PDF
React.js for Rails Developers
PPTX
Deep dive into share point framework webparts
PPTX
Full Stack_Reac web Development and Application
PPTX
SharePoint Web part programming
PPTX
Using MVC with Kentico 8
PPSX
03 asp.net session04
PPT
Seamless Integrations between WebCenter Content, Site Studio, and WebCenter S...
PPTX
MVC 6 - the new unified Web programming model
PDF
ASP.NET AJAX - 20090916
PPT
Using Java to implement SOAP Web Services: JAX-WS
PPS
15 asp.net session22
PPT
SynapseIndia asp.net2.0 ajax Development
PPTX
Lightning web components - Introduction, component Lifecycle, Events, decorat...
PPTX
SharePoint Object Model, Web Services and Events
PDF
Tech Talk Live on Share Extensibility
PDF
An Overview of the React Ecosystem
Parallelminds.web partdemo1
Parallelminds.web partdemo
SharePoint 2010 Training Session 5
React.js for Rails Developers
Deep dive into share point framework webparts
Full Stack_Reac web Development and Application
SharePoint Web part programming
Using MVC with Kentico 8
03 asp.net session04
Seamless Integrations between WebCenter Content, Site Studio, and WebCenter S...
MVC 6 - the new unified Web programming model
ASP.NET AJAX - 20090916
Using Java to implement SOAP Web Services: JAX-WS
15 asp.net session22
SynapseIndia asp.net2.0 ajax Development
Lightning web components - Introduction, component Lifecycle, Events, decorat...
SharePoint Object Model, Web Services and Events
Tech Talk Live on Share Extensibility
An Overview of the React Ecosystem

More from SPTechCon (20)

PPTX
Deep Dive into the Content Query Web Part by Christina Wheeler - SPTechCon
PPTX
“Managing Up” in Difficult Situations by Bill English - SPTechCon
PPTX
Ten Best SharePoint Features You’ve Never Used by Christian Buckley - SPTechCon
PPTX
Looking Under the Hood: How Your Metadata Strategy Impacts Everything You Do ...
PPTX
Law & Order: Content Governance Strategies by Chrisitan Buckley - SPTechCon
PPTX
What IS SharePoint Development? by Mark Rackley - SPTechCon
PPTX
The SharePoint and jQuery Guide by Mark Rackley - SPTechCon
PPTX
Understanding and Implementing Governance for SharePoint 2010 by Bill English...
PPTX
Integrate External Data with the Business Connectivity Services by Tom Resing...
PDF
Converting an E-mail Culture into a SharePoint Culture by Robert Bogue - SPTe...
PPTX
Tutorial: Best Practices for Building a Records-Management Deployment in Shar...
PDF
Tutorial: Building Business Solutions: InfoPath & Workflows by Jennifer Mason...
PDF
Creating Simple Dashboards Using Out-of-the-Box Web Parts by Jennifer Mason- ...
PDF
Sponsored Session: Better Document Management Using SharePoint by Roland Simo...
PDF
Sponsored Session: The Missing Link: Content-Aware Integration to SharePoint ...
PPTX
Creating a Great User Experience in SharePoint by Marc Anderson - SPTechCon
PPTX
Tutorial: SharePoint 2013 Admin in the Hybrid World by Jason Himmelstein - SP...
PPTX
Business Intelligence in SharePoint 2013 by Jason Himmelstein - SPTechCon
PDF
Piloting with SharePoint—Learn to FLY by Eric Riz - SPTechCon
PDF
Write the Right Requirements by Eric Riz - SPTechCon
Deep Dive into the Content Query Web Part by Christina Wheeler - SPTechCon
“Managing Up” in Difficult Situations by Bill English - SPTechCon
Ten Best SharePoint Features You’ve Never Used by Christian Buckley - SPTechCon
Looking Under the Hood: How Your Metadata Strategy Impacts Everything You Do ...
Law & Order: Content Governance Strategies by Chrisitan Buckley - SPTechCon
What IS SharePoint Development? by Mark Rackley - SPTechCon
The SharePoint and jQuery Guide by Mark Rackley - SPTechCon
Understanding and Implementing Governance for SharePoint 2010 by Bill English...
Integrate External Data with the Business Connectivity Services by Tom Resing...
Converting an E-mail Culture into a SharePoint Culture by Robert Bogue - SPTe...
Tutorial: Best Practices for Building a Records-Management Deployment in Shar...
Tutorial: Building Business Solutions: InfoPath & Workflows by Jennifer Mason...
Creating Simple Dashboards Using Out-of-the-Box Web Parts by Jennifer Mason- ...
Sponsored Session: Better Document Management Using SharePoint by Roland Simo...
Sponsored Session: The Missing Link: Content-Aware Integration to SharePoint ...
Creating a Great User Experience in SharePoint by Marc Anderson - SPTechCon
Tutorial: SharePoint 2013 Admin in the Hybrid World by Jason Himmelstein - SP...
Business Intelligence in SharePoint 2013 by Jason Himmelstein - SPTechCon
Piloting with SharePoint—Learn to FLY by Eric Riz - SPTechCon
Write the Right Requirements by Eric Riz - SPTechCon

Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor - SPTec…

  • 2. What Are Web Parts? • Small “chunks” of user interface and functionality • Aggregated together to build page content • Managed by end-users  Add/remove web parts on a page  Determine where web parts are placed on the page  Set web part properties • Concept and implementation not specific to SharePoint or ASP.NET
  • 4. Web Part History • SharePoint 2003  First version with web part infrastructure  Web part types and infrastructure are specific to SharePoint • ASP.NET 2.0  Web part types and infrastructure added to ASP.NET • SharePoint 2007  Adds support for ASP.NET web parts  Continues support for SharePoint web parts for backwards compatibility • SharePoint 2010  Same support as SharePoint 2007 in farm solutions  Adds support for web parts in sandboxed solutions
  • 5. Working with SharePoint Web Parts • Web parts can be added to web part zones or wiki content • Each web part has common “chrome”  Title, border, verbs menu • Closing a web part hides it; deleting a web part removes it from page
  • 6. Web Part Properties • Web parts properties used to tailor display and functionality  Zip/Postal code in weather web part  Stock code in stock ticker web part • Properties support customization and personalization  Customization effects shared view of part  Personalization effects current users view of part
  • 7. Web Part Gallery • Site collection scoped • Both .DWP and .WEBPART files as items • SharePoint can discover new candidates from Web.config • Maintain metadata • Available out-of-the-box parts determined by edition and site template
  • 10. SharePoint Web Part Page Structure • Inherits from WSS WebPartPage base class • Contains one SPWebPartManager control • Contains one or more WSS WebPartZone controls SPWebPartManager WebPartZone (Left) WebPartZone (Right) Editor Zone Editor Part 1 Web Part 1 Web Part 3 Editor Part 2 Web Part 4 Web Part 2 Catalog Zone Web Part 5 Catalog Part 1 Catalog Part 2
  • 11. Building a Simple Web Part • ASP.NET web parts are server controls  WebPart type derives from Panel  User interface described with code • Override RenderContents  Use HTML markup and JavaScript to build interface • Override CreateChildControls  Use Server Controls to build interface • Two methods can be combined protected override void RenderContents(HtmlTextWriter writer) { writer.RenderBeginTag(HtmlTextWriterTag.H2); writer.Write("Hello, World"); writer.RenderEndTag(); }
  • 12. Chrome and Web Part Gallery Properties • Can set web part properties to change chrome values • Can be done in three places  Using code in the web part constructor  Using CAML in the .webpart file  Editing the web part properties in the browser • Web part gallery group name set in element manifest
  • 14. Composite Web Parts • Contain server controls as children • Declare controls as fields • In CreateChildControls  Create controls  Set properties  Add controls to web part Controls collection  Use LiteralControl to add literal markup • Populate controls in OnPreRender
  • 15. Composite Web Parts protected DropDownList ListsDropDown; protected GridView ItemsGridView; protected override void CreateChildControls() { ListsDropDown = new DropDownList(); ListsDropDown.AutoPostBack = true; ListsDropDown.SelectedIndexChanged += new EventHandler(ListsDropDown_SelectedIndexChanged); Controls.Add(ListsDropDown); Controls.Add(new LiteralControl("<br/><br/>")); ItemsGridView = new GridView(); Controls.Add(ItemsGridView); }
  • 17. Deploying Web Parts • Add .webpart file to web part gallery • Register assembly that implements the web part as safe control in web.config • Copy assembly to:  Global Assembly Cache  bin folder of IIS Web Application  Executes with reduced trust
  • 18. Deploying Web Parts - 2 • Visual Studio tools automate deployment process during development • Visual Studio deployment has conflict detection  Will replace .webpart file in gallery • Deployment via Powershell or stsadm does not have conflict detection  Deployed .webpart files will not be replaced
  • 20. Web Parts in Sandboxed Solutions • Split page rendering  Page code runs in ASP.NET worker process  Sandboxed web part code runs in sandbox worker process  HTML markup and JavaScript from two worker processes merged • Sandbox restrictions  Reduced set of server object model API  Restricted access outside SharePoint  No elevation of permissions  Cannot deploy files to SharePoint system folders  Must be ASP.NET web parts  Can only use ASP.NET controls  No web part connections
  • 21. Visual Web Parts • Visual web parts combine a web part with a user control • User controls have full design experience in Visual Studio • User interface and code behind defined in user control • Web part “injects” user control dynamically using Page.LoadControl private const string _ascxPath = @"~/_CONTROLTEMPLATES/.../MyUserControl.ascx"; protected override void CreateChildControls() { Control control = Page.LoadControl(_ascxPath); Controls.Add(control); }
  • 22. Visual Web Parts and Sandboxed Solutions • Native visual web part template not compatible with sandboxed solutions  Attempts to deploy user control to system folders • Visual Studio Power Tools contains a sandbox compatible template  Markup in the user control is converted to code at design time
  • 24. Web Part Properties • Web Parts support persistent properties • Configure properties via attributes  Personalizable(param)  Directs SharePoint to persist property value  Parameter indicates if property may be personalized  WebBrowsable(true)  Directs SharePoint to generate interface to edit property  WebDisplayName, WebDescription  The prompt and tooltip that accompany the data entry element  Category  The group in which the properties will be shown [Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [WebDisplayName("Year")] [Category("Pluralsight")] public int Year { get; set; }
  • 26. Deactivating Web Part Features • Deactivating a Feature that provisions .webpart files to the web part gallery effectively does nothing  Files provisioned by a module are not automatically removed on deactivation  That is, custom web parts remain in the gallery after the Feature has been deactivated • You should add code to the feature receiver to remove the .webpart files from the gallery on deactivation
  • 27. Deactivating Web Part Features public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { var site = properties.Feature.Parent as SPSite; if (site == null) return; var web = site.RootWeb; var list = web.Lists["Web Part Gallery"]; var partNames = new string[] { "Simple.webpart", "Composite.webpart", "Visual.webpart", "Sandboxed.webpart", "Properties.webpart" }; foreach (var partName in partNames) { var item = list.Items.OfType<SPListItem>(). SingleOrDefault(i => i.Name == partName); if (item != null) item.Delete(); } }
  • 29. Creating Connectable Web Parts • Pass data from one web part to another • Loosely-coupled connection  Communication managed by WebPartManager  Provider web part supplies data  Consumer web parts retrieve data • Interface used to define data contract  ASP.NET has standard connection contracts  Can use custom connection contracts • SharePoint provides user interface elements to establish connections • Not compatible with sandboxed solutions