Services using SharePoint

joeneetscompetitiveΑσφάλεια

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

86 εμφανίσεις

Silverlight Clients
Consuming Custom Web
Services using SharePoint

Leveraging WCF and ASMX web services to deliver
custom tailored business objects to be consumed.

About Me

Brian
Bedard
, MCTS

Sogeti

USA



12
year professional consultant


C
urrently working on Silverlight technologies that utilize SharePoint
integration and collaboration


Active Tweeter


Participate in MSDN Forums for SharePoint 2007 and 2010 development



brian.bedard@us.sogeti.com

@
tigertoy

on Twitter

http://pioneeringsharepoint.blogspot.com



Agenda


Introducing the Model


Introducing the
Serializer


How does the serialization work


How to make a ASMX Web Service


How to register WCF for SharePoint


How to make a WCF Service


Using these services in Silverlight


T
he Model


Three classes:


SharepointListItem


SharepointListNameAttribute


SharepointListFieldAttribute


SharepointListItem

is the model of a SharePoint list


Properties of the class are decorated with
SharepointListFieldAttribute


The class itself is decorated with
SharepointListNameAttribute


These classes provide the mapping of the
SPListItem

data

[
SharepointListName
(“Airline Industry News”)]

[
DataContract
]

public class
AirlineIndustryNews

:
SharepointListItem

{




[
DataMember
]



[
SharepointListField
("ID")]



public
int
? ID { get; set; }



[
DataMember
]


[
SharepointListField
(“
NewsTitle
")]


public String Title { get; set; }

}


The model maps the logical to the physical


But it doesn’t work without a helper

Serializer

(Service Helper)


Generic type


Serializes model data to a
SPListItem


De
-
serializes the
SPListItem

to model data


Performs all the CRUD operations


FetchListItem
,
FetchListItems


UpdateListItem
,
UpdateListItems


DeleteListItem
,
DeleteListItems


Is constrained to only accept classes of type
SharepointListItem


Has facilities for handling attachments,
enums
, child
relationships, and querying


The secret is the core methods Load and Save

How does it work


Service helper uses reflection and the named attributes
to serialize/de
-
serialize the data


Save/Load loops through all the given properties using
reflection and finds a matching fieldname in
SPListItem


When save/load is called, the
SPListItem

is
updated/fetched using the stored list name


Each CRUD method calls Save/Load internally


There’s a switch to detect a new item; ID is null


Facility for attachments, child relationships, querying
and filtering

Querying and Filtering


Introducing
CamlFactory

and
SPQueryFactory


Static class with methods to construct CAML syntax
using English syntax


mostly


Unfortuantely

you need to have a GOOD understanding
of CAML to use this


you just don’t have to write the
CAML syntax


Uses a chaining pattern to accept complex query logic


CamlFactory

will return a string of CAML


SPQueryFactory

will return an
SPQuery

object


Example Query

return
SPQueryFactory.CreateQuery
(


CamlFactory.CreateWhereClause
(


CamlFactory.CreateEqualCriteria
(


CamlFactory.CreateNumberField
(
id.ToString
(),“
AirlineI
d
")


)


),


CamlFactory.CreateOrderByClause
(


CamlFactory.CreateOrderByFieldRef
(
"Modified",
CamlFactory.OrderByType.Decending
)


)

);

Making an ASMX Web Service


Register ASMX web services with SharePoint by creating
a locator and
wsdl

file

1.
Create model class from
SharepointListItem

2.
Add
SharepointListName

attribute

3.
Create properties and add
SharepointListField

attributes

4.
Create web method and instantiate service helper class
for type created in step 1

5.
Call the helper method like
FetchListItem

Example service helper method

public
AirlineIndustryNews

GetAirlineIndustryNews
(
int
? id)

{


SharepointServiceHelper
<
AirlineIndustryNews
> helper =


new
SharepointServiceHelper
<
AirlineIndustryNews
>(
SPContext.Current.Web
);



return
helper.FetchListItem
(1);

}


public class
SharepointServiceHelper
<T> where T :
SharepointListItem

{

p
rotected virtual T
FetchListItem
(
int

id)

{


T item = new T();


SPListItem

result =
Web.Lists
[
SharepointListName
].
GetItemById
(id);


Load(result, ref item);


return item;

}

}


DEMO

Register WCF Service


Create an
HttpModule


Register a
VirtualPathProvider

during
Init


The VPP will implement
FileExists

and filter on “svc”


Register
HttpModule

with web application by modifying
web.config

Making a WCF Web Service


Set up your web application for ASP.NET 3.5 including
System.Web.Extensions

v3.5 by modifying
web.config


Register WCF services with SharePoint

1.
Create model class from
SharepointListItem

2.
Add
SharepointListName

attribute

3.
Create properties and add
SharepointListField

attributes

4.
Create operations contract (Interface)

5.
Create contract implementation (Web method)

6.
Create web method and instantiate service helper class for
type created in step 1

7.
Call the helper method like
FetchListItem

DEMO