Ultimamente mi è capitato di dover sviluppare per un cliente un servizio WCF Rest, da utilizzare in un applicazione mobile sotto sharepoint.
Lo sviluppo non è molto differente da un classico servizio WCF, serve solo un pò di accuratezza.
come prima cosa creiamo un progetto sharepoint (in Visual studio 2010)

Inseriamo nel progetto un riferimento alla Cartella "ISAPI" di sharepoint.
Cliccando con il tasto destro sul progetto come da figura:

A questo punto non ci resta che creare il nostro servizio.
Nella cartella "ISAPI" sono stati inseriti 2 file:
1. AccessList.svc
2. Web.config
AccessList.svc:
<%@ ServiceHost Debug="true" Language="C#" Service="[NameSpace].AccessList, [NameSpace], Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxx" CodeBehind="AccessList.cs"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressDataServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx" %>
Microsoft.SharePoint.Client.Services.MultipleBaseAddressDataServiceHostFactory (utilizzare il REST)
Web.config:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<!-- we need this to enable session -->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<behaviors>
<endpointBehaviors>
<!-- our configuration for rest relies on web http -->
<behavior name="RestBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<!-- a selection of security bindings that you can use in the service registration below-->
<binding name="WindowsAuthenticationBasicHttpBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" />
</security>
</binding>
<binding name="NoSecurityHttpBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<!-- register our wcf service -->
<service name="[NameSpace].AccessListBehavior" >
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="RestBehavior"
contract="[NameSpace].IAccessList"
bindingConfiguration="NoSecurityHttpBinding">
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
nel web.config si occupa di esporre il servizio.
Nel file AccessList.cs:
[ServiceBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AccessList : IAccessList
{
SPList oList;
string _CurrentURL = string.Empty;
public System.Collections.Generic.List<entry> GetUFList(string list, ...
{
...
}
}
viene scitto il codice che si occupa di erogare il servizio vero e proprio.
Nel file IAccessList.cs:
è presente il contract del servizio:
[ServiceContract]
public interface IAccessList
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare]
public System.Collections.Generic.List<entry> GetUFList(string list, ... )
Facciamo il deploy sul server Sharepoint, a questo punto il servizio può essere richiamato nel seguente modo:
http://[Server]:[Port]/_vti_bin/listaccess/accesslist.svc/
Approfondimenti:
http://msdn.microsoft.com/it-it/magazine/dd315413.aspx
Nessun commento:
Posta un commento