tutorial, part 2: sharepoint 101: jump-starting the developer by rob windsor - sptec…

21
Working with the SharePoint Object Models Rob Windsor [email protected] @robwindsor

Upload: sptechcon

Post on 01-Jun-2015

956 views

Category:

Documents


0 download

DESCRIPTION

Full-Day Tutorial Sunday, March 3 9:00 AM - 5:00 PM

TRANSCRIPT

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

Working with the

SharePoint Object

Models

Rob Windsor

[email protected]

@robwindsor

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

SharePoint Developer APIs

• Server Object Model

Used by client apps running on SP server

• Client Object Models (CSOM)

Remote API

Three entry points: .NET Managed, Silverlight, JavaScript

Façade layer on top of WCF service

Uses batching model to access resources

• REST Web Services (API)

SP 2010: CRUD on list data only

SP 2013: API expanded to be more like CSOM

• SharePoint Web Services

“Legacy” SOAP-based web services

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

Server Object Model

• Can be used when “in the context” of SharePoint

Code-behind, event handlers, timer jobs

ASP.NET applications running in same app. pool

Client applications that run on SharePoint servers

• API implemented in Microsoft.SharePoint.dll

• Core types map to main SharePoint components

SPSite, SPWeb, SPList, SPDocumentLibrary,

SPListItem

SPContext gives access to current context

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

Server Object Model

• The SharePoint version of “Hello, World”

Show the root site of a collection and it’s lists

using (var site = new SPSite("http://localhost/sites/demo/"))

{

var web = site.RootWeb;

ListBox1.Items.Add(web.Title);

foreach (SPList list in web.Lists)

{

ListBox1.Items.Add("\t" + list.Title);

}

}

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

Resource Usage

• SPSite and SPWeb objects use unmanaged resources

Vital that you release resources with Dispose

• General rules:

If you create the object, you should Dispose

var site = new SPSite(“http://localhost”);

var web = site.OpenWeb();

If you get a reference from a property, don’t Dispose

var web = site.RootWeb

There are exceptions to these rules

• Use SPDisposeCheck to analyze code

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

DEMO Hello World with Server OM

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

Event Handlers

• Override methods on known receiver types

SPFeatureReceiver

SPListEventReceiver

SPItemEventReceiver

• Register receiver as handler for entity

Use CAML or code

• Synchronous and asynchronous events

ItemAdding

ItemAdded

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

Sample Feature Receiver

public class Feature1EventReceiver : SPFeatureReceiver

{

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

var web = properties.Feature.Parent as SPWeb;

if (web == null) return;

web.Properties["OldTitle"] = web.Title;

web.Properties.Update();

web.Title = "Feature activated at " + DateTime.Now.ToLongTimeString();

web.Update();

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

var web = properties.Feature.Parent as SPWeb;

if (web == null) return;

web.Title = web.Properties["OldTitle"];

web.Update();

}

}

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

DEMO Event Handlers

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

Client Object Model

• API used when building remote applications

Three entry points: .NET Managed, Silverlight, ECMAScript

Alternative to SharePoint ASMX Web services

• Designed to be similar to the Server Object Model

• Types in COM generally named the same as SOM minus

‘SP’ prefix

• Methods and properties also named the same when

possible

• Many SOM types or members are not available in COM

Example: the COM does not have WebApplication or Farm types

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

Retrieving Resources using Load

• Retrieve object data in next batch

• Object properties loaded in-place

• Some properties not retrieved automatically

Example: child collection properties

• Can explicitly indicate properties to retrieve

var siteUrl = "http://localhost/sites/demo"; var context = new ClientContext(siteUrl); var web = context.Web; context.Load(web, w => w.Title, w => w.Description); context.ExecuteQuery(); Console.WriteLine(web.Title);

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

Retrieving Resources using LoadQuery

• Result of query included in next batch

• Returns enumerable result

var query = from list in web.Lists.Include(l => l.Title) where list.Hidden == false && list.ItemCount > 0 select list; var lists = context.LoadQuery(query);

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

Managed Client Object Model

<System Root>\ISAPI

• Microsoft.SharePoint.Client

281kb

• Microsoft.SharePoint.Client.Runtime

145kb

To Compare:

Microsoft.SharePoint.dll – 15.3MB

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

DEMO Managed Client OM

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

Silverlight Client Object Model

• Very similar to the .NET managed implementation

• Silverlight controls can be hosted inside or outside

SharePoint

Affects how ClientContext is accessed

In SharePoint Web Parts, site pages, and application pages

Use ClientContext.Current

In pages external to the SharePoint Web application

Create new ClientContext

• Service calls must be made asynchronously

ExecuteQueryAsync(succeededCallback, failedCallback)

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

Silverlight Client Object Model

<System Root>\TEMPLATE\LAYOUTS\ClientBin

• Microsoft.SharePoint.Client.Silverlight

262KB

• Microsoft.SharePoint.Client.Silverlight.Runtime

138KB

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

Silverlight Client Object Model

private Web web;

private ClientContext context;

void MainPage_Loaded(object sender, RoutedEventArgs e) {

context = ClientContent.Current;

if (context == null)

context = new ClientContext("http://localhost/sites/demo");

web = context.Web;

context.Load(web);

context.ExecuteQueryAsync(Succeeded, Failed);

}

void Succeeded(object sender, ClientRequestSucceededEventArgs e) {

Label1.Text = web.Title;

}

void Failed(object sender, ClientRequestFailedEventArgs e) {

// handle error

}

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

JavaScript Client Object Model

• Similar to using .NET Managed/Silverlight implementations

Different platform and different language so slightly different API

• Can only be used on pages running in the context of

SharePoint

• Referenced using a SharePoint:ScriptLink control

Use ScriptMode=“Debug” to use debug version of library

• To get intellisense, also add <script> tag

Wrap in #if compiler directive so script isn’t loaded twice

• API uses conventions common in JavaScript libraries

Camel-cased member names

Properties implemented via get and set methods

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

JavaScript Client Object Model

• <System Root>\TEMPLATE\LAYOUTS

• SP.js (SP.debug.js)

380KB (559KB)

• SP.Core.js (SP.Core.debug.js)

13KB (20KB)

• SP.Runtime.js (SP.Runtime.debug.js)

68KB (108KB)

• Add using <SharePoint:ScriptLink>

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

JavaScript Client Object Model

<SharePoint:ScriptLink Name="sp.js" LoadAfterUI="true" Localizable="false" runat="server" ID="ScriptLink1" /> <SharePoint:ScriptLink Name="jquery-1.4.2.min.js" LoadAfterUI="true" Localizable="false" runat="server" ID="ScriptLink2" /> <% #if ZZZZ %> <script type="text/javascript" src="/_layouts/SP.debug.js" /> <script type="text/javascript" src="/_layouts/jquery-1.4.2-vsdoc.js" /> <% #endif %> <script type="text/javascript"> _spBodyOnLoadFunctionNames.push("Initialize");

var web;

function Initialize() {

var context = new SP.ClientContext.get_current();

web = context.get_web();

context.load(web);

context.executeQueryAsync(Succeeded, Failed);

}

function Succeeded() { $("#listTitle").append(web.get_title()); }

function Failed() { alert('request failed'); }

</script>

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

DEMO JavaScript Client OM