15

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page ii - Buch.de · Fox ffirs.tex V3 - 01/30/2009 5:09pm Page i ... (VSTO) and Visual Studio Tools for Applications (VSTA), wherein he developed

  • Upload
    hanhu

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page ii

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page i

Professional Microsoft® SharePoint® 2007Development Using Silverlight™ 2

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiiiChapter 1: Introduction to Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Chapter 2: Your First Silverlight and SharePoint Integration . . . . . . . . . . . . . . . . 23Chapter 3: Creating Custom Field Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Chapter 4: Branding your SharePoint site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111Chapter 5: Custom Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Chapter 6: Advanced Custom Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Chapter 7: Data Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Chapter 8: Bringing It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page ii

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page iii

Professional

Microsoft® SharePoint® 2007 DevelopmentUsing Silverlight™ 2

Steve FoxPaul Stubbs

Wiley Publishing, Inc.

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page iv

Professional Microsoft® SharePoint® 2007 DevelopmentUsing Silverlight™ 2Published byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.com

Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-43400-0

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Library of Congress Cataloging-in-Publication Data

Fox, Steven, 1970-Professional Microsoft SharePoint 2007 development using Silverlight 2 / Steve Fox, Paul Stubbs.

p. cm.Includes index.ISBN 978-0-470-43400-0 (paper/website)

1. Microsoft SharePoint (Electronic resource) 2. Silverlight (Electronic resource)3. Intranets (Computer networks) I. Stubbs, Paul R., 1969- II. Title.

TK5105.875.I6F693 2009006.7’882 — dc22

2008054925

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by anymeans, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, orauthorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 RosewoodDrive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should beaddressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties withrespect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,including without limitation warranties of fitness for a particular purpose. No warranty may be created or extendedby sales or promotional materials. The advice and strategies contained herein may not be suitable for everysituation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting,or other professional services. If professional assistance is required, the services of a competent professional personshould be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that anorganization or Web site is referred to in this work as a citation and/or a potential source of further informationdoes not mean that the author or the publisher endorses the information the organization or Web site may provideor recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work mayhave changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within theUnited States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related tradedress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United Statesand other countries, and may not be used without written permission. Microsoft, SharePoint, and Silverlight aretrademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. All othertrademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product orvendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not beavailable in electronic books.

Fox ffirs.tex V3 - 01/30/2009 5:09pm Page v

To Nicole, for putting up with my ‘‘quiet time’’ when trying to write this book.

— Steve Fox

To my wife, Rosa, who allowed me time away to write during a coast-to-coast move,buying and selling a home, a broken finger, and a new baby girl. Without her support and understanding,

this book would not have been possible.

— Paul Stubbs

Fox f01.tex V3 - 01/30/2009 5:07pm Page vi

About the AuthorsSteve Fox has been in the IT industry for more than 12 years, working in various areas includingsearch and natural language, web development, and more recently Office and SharePoint development.He currently works for Microsoft as a Senior Technical Evangelist. When not working, he enjoyssports — especially hockey — as well as movies and writing. He is hopeful that the Buffalo Sabres willone day win the Stanley Cup. He currently lives in Seattle, Washington.

Paul Stubbs is a Senior Architect Evangelist in the Platform Architecture Team within the Developerand Platform Evangelist group at Microsoft. He focuses on solutions architecture with an emphasis oninformation worker productivity, business-to-customer (B2C) solutions, and Web 2.0 social networking.Paul has also worked with several other groups at Microsoft. He helped ship the first and second versionsof Visual Studio Tools for Office (VSTO) and Visual Studio Tools for Applications (VSTA), wherein hedeveloped a new managed code application programmability development tool for InfoPath 2007. Healso developed programmability features for future versions of Microsoft Office for the Groove team.

Fox f02.tex V3 - 01/30/2009 5:08pm Page vii

CreditsAcquisitions EditorKatie Mohr

Development EditorMaureen Spears

Technical EditorLaurence Moroney

Senior Production EditorDebra Banninger

Copy EditorCate Caffrey

Editorial ManagerMary Beth Wakefield

Production ManagerTim Tate

Vice President and Executive Group PublisherRichard Swadley

Vice President and Executive PublisherBarry Pruett

Associate PublisherJim Minatel

Project Coordinator, CoverLynsey Stanford

ProofreaderNancy Carrasco

IndexerJ & J Indexing

Fox f03.tex V3 - 01/30/2009 5:08pm Page viii

Acknowledgments

It takes many people to put together a book like this, and often this work goes unseen to the reader. I’dfirst like to thank the staff at Wiley for doing a great job of keeping us on track and making sure that ourcontent was strong and moved along at the right pace. I’d like to especially thank Maureen for keepingthe whip cracking and Katie for believing in this project in the first place. There are, I’m sure, manyothers at Wiley who touched this book in some way, and for those of you I didn’t have direct contactwith but who did work on this book, thanks! I’d secondly like to thank Laurence, acting as our TechnicalReviewer, who raised great questions along the way but kept us on track and made the book what I seeas a much better finished product. And lastly, thanks to the folks at Microsoft who helped us along theway — whether it was answering questions on forums, reviewing content, or spending a few minutesresponding to our e-mails.

— Steve Fox and Paul Stubbs

Fox ftoc.tex V3 - 01/30/2009 5:17pm Page ix

Contents

Introduction xiii

Chapter 1: Introduction to Silverlight 1

Designer and Developer Convergence 1Overview of XAML 2Silverlight Code-Behind 9Developer Environment and Tools 13

Changing the MIME Type 13Editing the web.config File 14Copying the Silverlight DLL to Your GAC 18

Why Silverlight and SharePoint? 18Summary 20Additional References 20

Chapter 2: Your First Silverlight and SharePoint Integration 23

Different Ways to Integrate with SharePoint 23Creating the Silverlight Application 25

Silverlight Solution Project Files 26Creating the Silverlight Application Code-Behind 37

Creating and Deploying the SharePoint Web Part Host Container 47Creating the Web Part Project 47Adding References to the Project 48Configuring the Web Part Project for Deployment 51

Adding the Silverlight Video Player Application to the SharePoint Site 52Troubleshooting Tips 55Summary 56Additional References 56

Chapter 3: Creating Custom Field Types 57

Lists, Columns, and Custom Field Types 57Using List Templates and Customer Lists 58

Developing Your First Custom Field Type 62Creating an ASP-Based Custom Field Type 63Developing a Silverlight-Based Custom Field Type 75

Fox ftoc.tex V3 - 01/30/2009 5:17pm Page x

Contents

Creating the Silverlight and SharePoint Integration 87Deploying the Solution 107

Summary 109Additional References 109

Chapter 4: Branding your SharePoint site 111

Custom Site Page 111Adding a Blank Branding Solution 112Adding a Custom Site Page 113Adding Your Own Content 116

Create Navigation Menu 123Custom List Page 128

Adding a Feature to the Project 129Customizing the List with Silverlight 130Creating a New Custom List View 133

Customizing the Master Page 137Creating a Basic Master Page 138Customizing the Page 141

Summary 144

Chapter 5: Custom Web Parts 145

Creating a Silverlight Control 145Creating a New Silverlight Project 146Editing the New Control 146

Content Editor Web Part 149Getting Your Application to Run 150Adding a Silverlight Plug-In 151JavaScript Only 153Silverlight Streaming 154

Customizing Web Part 154Starting a New Web Part Project 154SharePoint Solution Features 154Displaying the Silverlight Control and Adding References 155Running the Solution 158Deploying the Silverlight Application 159Uninstalling and Updating the Solution 162Render Contents 162Web Part Gallery Category 164Web Part Properties 165Debugging 166

Summary 168

x

Fox ftoc.tex V3 - 01/30/2009 5:17pm Page xi

Contents

Chapter 6: Advanced Custom Web Parts 169

Cleaning up Files and Web Parts upon Uninstall 170Using Feature Receivers 170Debugging Feature Receivers 178

Customizing Web Parts 180Using Custom Embedded JavaScript Files 181Using Customized Verbs 182Editing Custom Web Part Properties 186Using Custom Web Part Editor 189

Creating Editor Parts 193Display Mode 193Display Dynamic Silverlight UI Based on Web Part Display Mode 195

Summary 202

Chapter 7: Data Connectivity 205

Initialization Parameters 205Retrieving the InitParameters 207Using Hidden Field 210Using HTML Cookies 214

XML Data Island 220Creating the Server Control 220Setting the URL for an XML Data Source 225

Calling Web Services 225Adding a New Contact List to the SharePoint Site 227Data-Bind to the ListBox 229

Part-to-Part Communication 232Registering Controls 234Setting the ScriptableMethod Attribute 236

Summary 246

Chapter 8: Bringing It All Together 247

The Training List 248Service 250What about WCF? 259Hosting the Silverlight Application 274

Re-Skinning the Training List 276Binding Data to a Silverlight Chart 278The Application in Action 283Summary 284

Index 285

xi

Fox ftoc.tex V3 - 01/30/2009 5:17pm Page xii

Fox f04.tex V3 - 01/30/2009 3:38pm Page xiii

I n t roduc t ion

Two exciting trends in the set of Microsoft products are the recent release of Silverlight 2 and theincreased use of Microsoft Office SharePoint Server (MOSS) 2007. There is a growing community aroundboth of these products, and their convergence — something that developers see as having a great amountof potential — is in its infancy. The goal of this book is to provide developers with the knowledge theyneed to integrate Silverlight and SharePoint in different ways. The hope is not only to enhance the userexperience for SharePoint sites (both Intranet and Internet-facing sites), but also to integrate rich Internetapplications (or RIAs) into the SharePoint experience, thus leveraging the SharePoint infrastructure andintegrating greater and richer application functionality into the context of a SharePoint site.

Who This Book Is ForThis book is primarily for .NET or SharePoint developers. Now that’s a pretty broad community, sowe’d qualify that to say those developers who are interested in integrating Silverlight with SharePoint.That said, the types of skills we envisioned you as having when we wrote the book was a beginningknowledge of Silverlight. We’ll walk you through some of the basics in this book, but if you’re new toSilverlight, it might help to read a couple of introductory articles or, if you’re feeling eager, pick up aSilverlight 2 book. [Introducing Silverlight 2 (2nd Ed.) (MS Press) is a good introductory text.] Also, you’dlikely have an intermediate knowledge of SharePoint. Now if you don’t have either of these, you willstill get something from this book; you may just need to do a little side research after you finish the book.SharePoint is a very big product, so you could apply Silverlight in many different ways. We’ve givenwhat we feel are a few patterns of application, but ultimately, as you learn SharePoint more, you’ll seewhere you can apply the rich functionality enabled by Silverlight to your SharePoint solutions.

As fundamental developer skills, we’ll be using Visual Studio 2008 throughout this book as the coredevelopment tool, so you should be familiar with Visual Studio and .NET Framework 3.5. Paul and Iboth develop primarily in C#, but we feel that there is a large community of Visual Basic (VB) developersout there, and you can just as easily develop in VB as you can in C#, so we included both C# and VBcode samples. This does stretch out the code on the pages a little, but given the appeal of the technologieswe discuss in this book and the ability to use both VB and C# for development against Silverlight andSharePoint, we feel this was a worthwhile trade-off.

All that said, if you are a developer and you’re interested in transforming a SharePoint site using Sil-verlight, then you should read this book.

What This Book CoversAs mentioned above, the two primary technologies covered in this book are Silverlight 2 and MOSS 2007(which we’ll refer to as SharePoint throughout this book). To do the development work, we’ll use tools