8 steps to add SVC service to SharePoint on-premises solution programmatically
When you make a SharePoint 2019 on-premises solution, you can make not only SPFX solutions, but also old-school WSP-solutions. It allows you not only to use SharePoint API, but also to use your own API services. With SharePoint 2019 you can use WCF service. For example, it’s a good way when you develop an application which uses the external database. This works also for SharePoint 2013 and SharePoint 2016.
Step1. Execute Visual studio as Administrator! It’s very important
Step2. Create ‘SharePoint 2019 – Empty Solution’. For example, I called it 'AbbLogViewer'.
Step 3. Choose Farm solution, not SandBox. It’s easy.
Step 4. In the project add new Item with a type “WCF Service”, I called it ‘LogRequest’. It adds 2 files – interface and implementation.
In the interface file (ILogRequest.cs) you should write URL templates, request types, format and other things. Like this:
[OperationContract]
[WebInvoke(
Method = "GET",
UriTemplate = "Read/lid={listItemId}",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json
)]
string GetDataFromService(string listItemId);
In the class file (LogRequest.cs) you should implement the method GetDataFromService(string listItemId). For example,
public string GetDataFromService (string listItemId)
{
string s = "Text from the service";
return s;
}
Step 5. Build the solution. Later you need to use the information about dll library.
Step 6. Add mapped SharePoint folder ISAPI to the solution.
Step 7. Create a folder inside ISAPI. I usually call it the same as project folder.
Step 8. Create a text file inside the created in previous step folder and call it, for example, ‘getdata.svc’.
<%@ServiceHost Language="C#" Debug="true"
Service="ABBLogViewer.LogRequest, ABBLogViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c14def01a094764"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
To get the value of the assembly token ‘7c14def01a094764’ (it’s an example!) you can execute the command in PowerShell:
([system.reflection.assembly]::loadfile("C:\Projects\AbbLogViewer\AbbLogViewer\bin\Debug\AbbLogViewer.dll")).FullName
The result is something like this:
AbbLogViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c14def01a094764
So just update the value of the token and deploy the solution.
And then use this URL to make the request:
http://mydevsharepoint/sites/mycommonsite/_vti_bin/Logrequest/getdata.svc/ReadFile/lid=1