spca2013 - sharepoint nightmares - coding patterns and practices

51

Upload: nccomms

Post on 09-May-2015

1.608 views

Category:

Technology


7 download

DESCRIPTION

SharePoint Nightmares - Coding Patterns and Practices

TRANSCRIPT

Page 1: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 2: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

SharePoint Nightmares Coding Patterns and Practices

Donald Hessing | @dhessing

Page 3: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

3Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Who am I?

Donald Hessing Principal Consultant | Thought Leader SharePoint

@Capgemini Netherlands

(Virtual) Technology Solution Professional for Microsoft

Work full time on SharePoint since 2007

[email protected] | @dhessing

Page 4: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

4Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Agenda

1. SharePoint Anti-Patterns

2. Data Access

3. Dispose and PowerShell

4. REST versus CSOM

5. SharePoint provisioning techniques

Demos:

Service Locator Pattern 2010/2013

PowerShell Dispose

Page 5: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

SharePoint Anti-Patterns

Page 6: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 7: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

7Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

True or False

web.Lists[“Pages”] == web.Lists[“Pages”]

Page 8: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 9: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

9Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Anti Pattern SPList[]

1: web.Lists[“Events”].EnableAttachments = false;

2: web.Lists[“Events”].Update();

SPList object in line 1 is not the same as in line 2

1: SPList list= web.Lists[“Events”];

2: list.EnableAttachments = false;

3: list.Update();

Page 10: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

BDEADEE2-C265-11D0-BCED-00A0C90AB50F

SP.SPRequest

Page 11: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

11Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

SP.SPRequest

SPRequestSPSite

_m_Request : SPRequest

SP.SPRequest (Unmanaged)

ClassID:

BDEADEE2-C265-11D0-BCED-00A0C90AB50F

OWSSVR.DLL

Microsoft.SharePoint.dll

Unmanaged

Unmanaged

Page 12: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

12Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

SPRequest

SPRequest is a wrapper for unmanaged class SP.SPRequest

SP.SPRequest is a COM Object that is coming from SharePoint Portal

Services back in 2001

Most retrieval and write operations to SharePoint (SPSite, SPWeb, SPList)

are still implemented by SP.SPRequest

SP.SPRequest (Unmanaged)

ClassID:

BDEADEE2-C265-11D0-BCED-00A0C90AB50F

OWSSVR.DLL

More:

http://hristopavlov.wordpress.com/2009/01/19/understanding-sharepoint-sprequest/

Page 13: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

13Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Anti Pattern SPList[]

1: web.Lists[“Events”].EnableAttachments = false;

2: web.Lists[“Events”].Update();

SPS.SPRequest

.EnabledAttachments (true)

SPS.SPRequest

.EnabledAttachments (false)

Page 14: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

14Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Anti-Pattern SPList.Items

Show the items of from SPList

SPList list = SPContext.Current.List;

for(int i=0;i<100 && i<list.Items.Count;i++)

{

SPListItem listItem = list.Items[i];

ShowTitle(listItem["Title"].ToString());

}

Page 15: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

15Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Good or Bad?

Show the items of from SPList

SPList list = SPContext.Current.List;

for(int i=0;i<100 && i<list.ItemsCount;i++)

{

….

}

SPList.ItemCount is an optimized property for performance reasons. This

property can occasionally return unexpected results. If the precise number is

required (in loops) than the SPList.Items.Count property should be used to

preven index out of range exceptions!

Page 16: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

16Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Improved Pattern

SPQuery query = new SPQuery();

query.RowLimit = 100;query. ViewFields = “<FieldRef Name='Title' />”;query. ViewFieldsOnly = true;

SPListItemCollection items = SPContext.Current.List.GetItems(query);

for (int i=0;i<items.Count;i++) {

SPListItem listItem = items[i];ShowTitle(listItem["Title"].ToString());

}

Page 17: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

17Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Anti-Pattern SPList.Items

Each call of .Items retrieves all items from the database!

Not needed when: SPList.Items[0], SPList.Items.Count

SPList.Items can’t benefit from indices

SPList.Items can LOCK your content database

When needed, sort the collection of SPList.Items in a

SPListCollection variable

Use SPQuery for list data retrieval (CAMLQueryBuilder)

Use SPSiteDataQuery for cross list items Use the RowLimit property of the query object

Page 18: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

18Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Tools… See “Building a SP Factory”

SPDisposeCheck.exe

MSOCAF 2010

FXContrib

SPCOP / SPCAF Foundation

FXCop

StyleCop

Visual Studio 2010 / 2012

MSOCAF 2013

SPCOP / SPCAF Foundation

FXCop – Custom Rules

StyleCop

Visual Studio 2012 / 2013

SharePoint 2010 SharePoint 2013

• Only tool for SharePoint artefacts,

metrics, feature dependency,

managed code and C# analyses!

• Includes TFS and 3th Party

integration (FXCop, JSLint,CAT.NET)

Page 19: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

19Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

(Remote) Event Receivers

Setting Unique permissions on large

libraries or sites based on meta data or

person

Synchronize or mirroring solutions

Mixing list data and relational data -

integrity solutions (some relational data

stored in SQL Server)

Remote event receivers are

implemented as a webservice endpoint

hosted on-premise or Windows Azure.

NO Guaranteed delivery!

NO Retry like normal messaging

infrastructure gives you

If you need guaranteed delivery, it’s

better to use the new workflow model!

Do not use for… Remote Event Receivers

Page 20: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

20Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Inherited Permissions

Web object

Document library object

Folder object

Item 1 object

Item 2 object

Item 3 object

Scope 1

+ User 2 (Reader)

+ User 3 (Full Control)

+ User 6 (Contributor)

1

1

1

1

1

1

Page 21: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

21Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Fine Grained Permissions

Web object

Document library object

Folder object

Item 1 object

Item 2 object

Item 3 object

Scope 1

+ User 2 (Reader)

+ User 3 (Full Control)

+ User 1 (Limited Access)

+ User 2 (Limited Access)

+ User 5 (Limited Access)

Scope 2

+ User 5 (Reader)

+ User 2 (Limited Access)

+ User 1 (Limited Access)

Scope 3

+ User 1 (Contributor)

Scope 4

+ User 2 (Contributor)

+ User 6 (Contributor)

1

1

2

3

4

5

Page 22: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

Service Locator Pattern

Page 23: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 24: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 25: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

25Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Tightly Coupled

The implementation of the Service

needs to be available at compile time

The classes cannot be tested in

isolation because they have direct

references to the implementation

(SPWeb, SPSite objects)

Class A can only be created when

Service A implementation is finished

Page 26: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

26Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

The Service Locator Pattern

Class A can now be tested in Isolation as it doesn’t hold any depencies

with the Service A Implementation

Class A is now loosely coupled to the Service Implementation

Page 27: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

27Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Example

Page 28: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

28Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

DEMO

Service Locator and Repository Pattern

Page 29: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

29Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Benefits of these patterns

Service Locator Pattern Decoupled dependency – Logger implementation can now easily be replaced

The service implementation can be tested in isolation by using a proxy (dummy data)

Repository Pattern Central point to access data

Can modify the data layer (storage) without too much impact

Strongly typed access to the data

Microsoft Patterns and Practices Guide provides implementation for both

patterns for SharePoint 2010

No specific version for SharePoint 2013 as for now

Page 30: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

PowerShell and Dispose()

Page 31: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

31Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Dispose() and PowerShell

SharePoint Management Shell ensures disposal of all objects in the

preceding pipeline sequence

Command or single line Pipeline runs a single Thread

Get-SPWebApplication | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select Title

Page 32: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

32Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

SharePoint Management Shell

SharePoint Management Shell is running multiple lines in the same thread,

ensuring thread safety of the SharePoint unmanaged code

This is achieved by setting the ThreadOptions=‘ReuseThread’

SharePoint Management Shell start-up script:

$ver = $host | select version

if ($ver.Version.Major -gt 1)

{$Host.Runspace.ThreadOptions = “ReuseThread”}

Add-PsSnapin Microsoft.SharePoint.PowerShell

Set-location $home

Page 33: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

33Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Multi-line

PowerShell that contains multiple lines require manually disposing of objects

No Help from PowerShell as it doesn’t know when to dispose objects

$site = Get-SPSite "http://sp2013"

Foreach ($web in $site.AllWebs)

{

Write-Host $web.Title

}

Page 34: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

35Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Start-SPAssignment & Stop-SPAssignment

Introduced in SharePoint 2010 to overcome memory pressure

Manages objects for the purpose of proper disposal SPWeb

SPSite

SPSiteAdministration

Page 35: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

36Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

SPAssignment - Global

SCOPE

Page 36: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

37Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

SPAssignment Namespaces

SCOPESCOPE

Page 37: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

38Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Why is this important?

PowerShell cannot claim leaked memory until process termination

Scripts that iterate large Web Applications and performs significant

processing require memory management!

Batch jobs for Reporting (Versioning, Checked-out, Access, Boundaries)

Page 38: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

39Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

PowerShell - CSOM

http://sponlinecmdlets.codeplex.com/

Roel Hans Bethlehem: “Extending PowerShell with CSOM”

Page 39: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

40Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

DEMO

Service Locator and Repository Pattern SP2010

Service Locator SP2013

Page 40: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

App Patterns

Page 41: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

42Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

App-Model Support

Hosting type SharePointHosted

AutoHosted Provider Hosted

SharePoint 2013 On Premises

Windows Classic X X X

Windows Claims √ X √

SAML Claims X X √**

SharePoint Online / Office 365

Windows Claims √ √ √

• No support for the SharePoint Public app store as for now

• SAML might requires additional configurationhttp://blogs.technet.com/b/speschka/archive/2012/12/07/using-sharepoint-apps-with-saml-and-fba-sites-in-sharepoint-2013.aspx

Page 42: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

CSOM versus REST

Page 43: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

44Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Clients

REST versus CSOM

JavaScript

Library

Silverlight

Library

.Net CLR

Library

Client

SharePoint

_api

SharePoint

Foundation

Execute

Query OData /

REST

User Profile Search Taxonomy Feeds More…

SP

Hosted

App

Windows

Phone

Auto

Hosted

App

Windows

8 RT

iOS,

PHP,

Ruby,etc

SP

Hosted

App

Page 44: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

45Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Limitations REST

REST calls seems to be more chatty than JSOM

REST implementation is still a subset of CSOM

Description REST JSOM

Updating Content Types X √

Post to person news feed X/√ √

Large file upload √ X

Batch updates X √

Well documented X / √ √

Supports jQuery, plugins √ X

Environment independent √ X

Page 45: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 46: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

47Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Links

http://spohelper.codeplex.com/

http://www.sharepointnutsandbolts.com/

http://blogs.technet.com/b/speschka/archive/2012/12/07/using-sharepoint-

apps-with-saml-and-fba-sites-in-sharepoint-2013.aspx

http://www.shillier.com/default.aspx

http://www.slideshare.net/bobbyschang/sharepoint-permissions-worst-

practices

Page 47: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

48Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Key takeaways

SharePoint Server Side Object Model is still using unmanaged code

Code Analyses tools can help, but be aware of the coverage (MSOCAF,

FXCop, SPDispose, SPCop)

The Service Locator and Repository pattern can decouple the actual

implementation and hide complexity in SharePoint data access code

Fully understand the implementation of a SharePoint feature before you

actually use it – Be aware – Blogs can be wrong!

Long running and resource intensive scripts need to dispose object as

well!

Page 48: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

49Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Thank You!

01010100 01101000 01100001 01101110

01101011 00100000 01011001 01101111

01110101 0100001

Page 49: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices

50Copyright © Capgemini 2012. All Rights Reserved

Presentation Title | Date

Donald Hessing

Thought Leader SharePoint @Capgemini

Microsoft Certified Master for SharePoint

Contact me:

@dhessing

[email protected]

http://nl.linkedin.com/pub/donald-hessing/4/250/924

Page 50: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices
Page 51: SPCA2013 - SharePoint Nightmares - Coding Patterns and Practices