MANNING
Wictor Wilén
IN ACTION
www.it-ebooks.info
SharePoint 2010 Web Parts in Action
www.it-ebooks.info
www.it-ebooks.info
SharePoint 2010
Web Parts in Action
WICTOR WILÉN
MANNING
Greenwich
(74° w. long.)
www.it-ebooks.info
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
180 Broad Street, Suite 1323
Stamford, CT 06901
Email: [email protected]
©2011 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
9 ■ Programming and caching for performance 221
10 ■ Dynamic interfaces in Web Parts 242
11 ■ The Client Object Model and Silverlight Web Parts 274
12 ■ Making Web Parts mobile 291
13 ■ Design patterns and testability 309
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
BRIEF CONTENTS
viii
PART 3 DASHBOARDS AND CONNECTIONS 333
14 ■ Connecting Web Parts 335
15 ■ Building pages and dashboards 359
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
ix
contents
preface xvii
acknowledgments xviii
about this book xx
about the cover illustration xxiii
about Web Parts xxiv
PART 1 INTRODUCING SHAREPOINT 2010 WEB PARTS 1
1
Introducing SharePoint 2010 Web Parts 3
1.1 What is a Web Part? 5
1.2 Why use Web Parts? 7
1.3 Introducing the Web Part infrastructure 9
Web Part page 9
■
Web Part Manager 10
interface improvements 26
2.2 The Web Part Gallery 27
What is the Web Part Gallery? 28
■
Uploading or adding new Web Parts
to the gallery 29
■
Editing Web Parts in the gallery 29
■
Grouping the
Web Parts 30
■
Permissions on Web Parts in the gallery 30
2.3 Out-of-the-box Web Parts 31
SharePoint Foundation 2010 31
■
SharePoint Server 2010 33
2.4 Working with Web Parts and pages 38
Creating a page using the web interface 38
■
Adding Web Parts
using the web interface 39
■
Filtering a Web Part using Web Part
connections 41
2.5 Customizing and personalizing pages 42
Shared view 43
■
Personal view 43
■
3.2 Developing for SharePoint 2010 in Visual Studio 2010 57
3.3 Building your first Visual Web Part 59
The Visual Web Part template 60
■
The SharePoint Customization
wizard 61
■
Explore the SharePoint Project Items 62
■
Adding
functionality to your Visual Web Part 65
■
Build and deploy the
Visual Web Part 67
■
Take your Web Part for a test drive 69
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
CONTENTS
xi
3.4 Traditional Web Part projects 70
Create an empty SharePoint project 71
■
Adding the Web Part to the
project 71
■
Adding controls to the Web Part 73
■
Adding the button-
click event manually 74
■
SharePoint
themes 102
■
Web compliancy 104
■
Web Part icons 105
4.4 Web Part verbs 106
Adding verbs to a Web Part 107
■
Event flow when using Web Part
verbs 108
4.5 Summary 109
5
Making Web Parts customizable 110
5.1 Web Part properties 111
Adding a property 111
■
Customizing the property 114
■
Custom
categories 115
■
Default values on properties 116
■
Properties used in
the Web Part class and defined in SharePoint 119
■
Properties in Visual
Web Parts 119
Visual appearance of the Editor Part interface 133
5.4 Advanced properties 135
Define the property 136
■
Problems with complex types 137
Create a type converter 137
5.5 Runtime filters 140
The AuthorizationFilter property 140
■
Create a runtime filter 141
Create the user filter control 143
■
Register the runtime filter 145
Use the filter 146
■
Overriding the default Target Audiences
filter 147
5.6 Summary 147
6
Web Part resources and localization 148
6.1 Linking and embedding resources 149
Using the SharePoint root folders 149
■
Using class resources 151
Embedded resources 153
■
Resources in SharePoint libraries 155
URL Expression Builder in SharePoint 2010 Server 156
6.2 Localization resources in Web Parts 157
Localization methods 157
7.3 Sandboxed solutions 184
What is sandboxing? 185
■
Configuring the sandbox 186
Deploying and installing sandboxed solutions 187
■
Full-trust
proxies 187
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
CONTENTS
xiii
7.4 Web application targeted solutions 190
Building solutions for web application deployment 190
■
Code
Access Security policies 192
■
Custom CAS policies 192
7.5 Upgrading solutions 194
How to upgrade 195
■
Upgrading Features 195
■
Assembly
redirection 197
■
Upgrading Web Parts 198
7.6 Summary 200
8
8.5 Other debugging tools 217
ASP.NET tracing 218
■
Internet Explorer Developer Tools or
Firebug 219
■
Fiddler web proxy 219
8.6 Summary 220
9
Programming and caching for performance 221
9.1 Programming for performance 222
Computational performance 222
■
Object and memory
usage 222
■
Scalability 222
■
Perceived performance 223
9.2 Programming for performance in SharePoint 223
Proper handling of SharePoint objects 224
■
Make smarter queries
to SharePoint 225
■
Asynchronous operations 227
■
Improve
performance of resources 230
9.3 Caching techniques 232
What is the ECMAScript Client Object Model? 275
■
Working
with the Client Object Model and Web Parts 276
11.2 Silverlight Web Parts 279
SharePoint Silverlight Web Part 279
■
Building a Silverlight Share-
Point application 280
■
Input parameters to the Silverlight Web
Part 283
■
Packaging the Silverlight Web Part 284
■
Preconfiguring
the Silverlight Web Part 286
■
Custom Silverlight Web Part 287
Enabling custom Silverlight Web Part interaction with SharePoint 289
11.3 Summary 290
12
Making Web Parts mobile 291
12.1 SharePoint 2010 mobile interface 292
Using the mobile SharePoint 2010 interface 292
■
Page, form, and
Web Part support 293
■
Supported mobile Web Parts 294
Use the SharePoint Service Locator 321
13.4 Testing Web Parts 326
Unit-testing 326
■
Creating a test project 327
■
Mock
objects 328
■
Test the Web Part 329
13.5 Summary 332
PART 3 DASHBOARDS AND CONNECTIONS 333
14
Connecting Web Parts 335
14.1 Introducing Web Part connections 336
14.2 Connecting Web Parts 337
Connecting using the web interface 337
■
Connecting using
SharePoint Designer 338
■
Connection permissions 339
14.3 Creating connectable Web Parts 340
Connect using custom interfaces 340
■
Connect using standard
connections 346
■
Ajax and Web Part connections 352
14.4 Filter Web Parts 354
xvii
preface
I really love working with SharePoint! I’ve been doing it full-time for the last half
decade, including lots of late nights and weekends. One area that I’ve found particu-
larly interesting is Web Parts. I like the nature of a Web Part since it can act as a stand-
alone application inside the powerful SharePoint platform—or it can interact both
with SharePoint and other Web Parts. A couple of years ago I owned a company that
built its own portal software (which integrated with SharePoint, but that’s another
story), and that product was based on a similar concept. When we started building our
solutions on SharePoint, Web Parts was the way to go since we were used to the
approach. One day I came up with an idea for a book about Web Parts. Yes, nearly all
SharePoint platform books cover Web Parts, but not as deeply as I wanted to. I felt that
there were much more to say about them. This book is the result.
As I was working on the outline, SharePoint 2010 was unleashed from Redmond,
and I thought that this must be it—perfect timing. After signing a contract with Man-
ning I thought, this can’t be too hard, just start writing. I couldn’t have been more
wrong! Writing a book isn’t easy, especially if English isn’t your native language, and
writing about a product that isn’t yet production-ready doesn’t make it any easier. But,
you do learn a lot in the process, and that is why I decided, in the end, to continue.
Nearly every day over the last eight months of writing this book, I learned something
new about SharePoint, or about writing, and that’s my reward.
Now it’s your turn to learn something new. I hope you enjoy reading this book as
much as I enjoyed writing it.
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
xviii
acknowledgments
This is the section I always read in a book, and one I’ve always wanted to write. From
reading others’ acknowledgments, I knew that writing a book is never a solo mission;
there are many people involved in many ways. And it was no different for my book.
www.it-ebooks.info
xx
about this book
This book focuses on Web Part development in SharePoint 2010 using Visual
Studio 2010. It takes you on a journey from learning what a Web Part is and how you
can use Web Parts, to building your own Web Parts in different and advanced ways.
When you have read it, you should be a fairly skilled SharePoint Web Part developer,
ready to take on challenging projects.
How this book is organized
This book covers Web Part development from basic to advanced scenarios and is
divided into three parts. Each chapter can stand on its own and you can use it as a ref-
erence when looking at a particular scenario.
The first part, consisting of chapters 1 and 2, is about Web Parts in general—to get
you started. These two chapters explain the parts of a Web Part, how a Web Part fits
into SharePoint, and how to use Web Parts in the SharePoint user interface, as well as
in SharePoint Designer.
Part 2 is where the action begins. It consists of 11 chapters that start you off build-
ing basic Web Parts using Visual Studio 2010. You will learn how to build configurable
and personalizable Web Parts and how to package and deploy them in a maintainable
way. Once you have learned the basics, discussion of advanced topics in subsequent
chapters, such as building contextual-aware and Silverlight Web Parts, will show you
how to take your Web Parts one step further. One of these (chapter 8) is dedicated to
troubleshooting; you will learn how to debug Web Parts and how to make trouble-
shooting easier—or avoid it entirely. New techniques introduced in SharePoint 2010,
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
ABOUT THIS BOOK
xxi
such as the Client Object Model, the Sandbox, and PowerShell are also covered. Part 2
ends with a chapter that focuses on design patterns and the latest guidelines released
font
like
this,
and if you are reading on an eReader or in PDF
format, you will see that the code is color coded. Some code listings and snippets con-
tain annotations to highlight important topics.
You can download the source code for many of the samples in this book from the
publisher’s website at www.manning.com/SharePoint2010WebPartsinAction.
Software requirements
To take full advantage of this book you need to have a copy of SharePoint 2010 Foun-
dation and Visual Studio 2010 (not the Express version). Preferably, you should have
access to a virtual environment. Chapter 3 has detailed information about software
requirements.
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
ABOUT THIS BOOK
xxii
Author Online
The purchase of SharePoint 2010 Web Parts in Action includes access to a private
forum run by Manning Publications where you can make comments about the
book, ask technical questions, and receive help from the author and other users. To
access and subscribe to the forum, point your browser to www.manning.com/
SharePoint2010WebPartsinAction, and click the Author Online link. This page pro-
vides information on how to get on the forum once you are registered, what kind of
help is available, and the rules of conduct in the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful
dialogue between individual readers and between readers and the author can take
place. It’s not a commitment to any specific amount of participation on the part of the
Dress codes have changed since then and the diversity by region, so rich at the
time, has faded away. It’s now often hard to tell the inhabitant of one continent from
another. Perhaps, trying to view it optimistically, we’ve traded a cultural and visual
diversity for a more varied personal life. Or a more varied and interesting intellectual
and technical life.
We at Manning celebrate the inventiveness, the initiative, and the fun of the com-
puter business with book covers based on the rich diversity of regional life of two cen-
turies ago brought back to life by the pictures from this travel guide.
Download from Wow! eBook <www.wowebook.com>
www.it-ebooks.info
xxiv
about Web Parts
Before we start our journey into SharePoint 2010 Web Parts, let’s take a look at the his-
tory of SharePoint and how Web Parts came to be.
Microsoft announced the concept of Web Parts in the middle of 2000, which was
before SharePoint existed as a product. It seems like ages ago. The aim of Web Parts
was the same then as it is now. At the time, Web Parts was a part of the since long-
forgotten Digital Dashboard, an
ASP, XML, and VBScript-based portal framework. “In
Web Parts, we are providing the building blocks for next-generation digital dashboard
solutions…,” wrote Bob Muglia in June 2000, later President of Microsoft’s Server and
Tools Business.
When SharePoint Portal Server 2001, called Tahoe, was released, it was based on
the Digital Dashboard and used Web Parts to create the portal. This was the first real
step towards the SharePoint of today. SharePoint at the time was not based on Micro-
soft
SQL Server but rather on the Microsoft Exchange Web Storage System (WSS) and
its focus was on document management rather than portals.
In 2003, Windows SharePoint Services 2.0 and SharePoint Portal Server 2003 were
released, based on the relatively new Microsoft .