Restful service
I want to integrate my external application with Sitefinity, And update the content using sitefinity.
I have a module builder created in sitefintiy with title, image and description, so please let me know if I can query the module builder to get the required field values using restful service from remote application.
Thanks,
Arun
Hi
If you want to transfer content from and to Sitefinity application and non Sitefinity application, basically, you have two options - using our publishing system or creating restful services for the items and operations you need. You can find more information about how the publishing system works here. You can find documentation on the matter here.
Regarding the services, there are several ways to define services in Sitefinity. One of them is using the wcf services. The service could be located under SitefinityWebApp or child folder, or under Sitefinity folder if you want authentication to be required, since /sitefinity/.. requires authentication.
What is required for a wcf service is, to inherit our factory:
<%@ ServiceHost Language="C#" Debug="true" Service="SitefinityWebApp.MyService" Factory="Telerik.Sitefinity.Web.Services.WcfHostFactory"
CodeBehind="MyService.svc.cs" %>
[ServiceBehavior(IncludeExceptionDetailInFaults =
true
, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public
class
MyService : IMyService
...
Another option is to register services using the SystemManager of Sitefinity. This is described in the beginning of this blog post.
Basic steps:
1. Create a class that will play the role of a service. It does not matter from what it will inherit from and add the necessary attributes:
namespace
SitefinityWebApp.InfiniteScrollWidget
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public
class
InfiniteScrollWidget : SimpleView
[OperationContract]
[WebInvoke(Method =
"POST"
, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public
DataResult GetData(
int
startIndex,
string
search, SortModel sort)
protected
void
Application_Start(
object
sender, EventArgs e)
Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized +=
new
EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
if
(e.CommandName ==
"Bootstrapped"
)
SystemManager.RegisterWebService(
typeof
(Your Class here),
"Public/MyServicePath.svc"
);
Regards,
Nikola Zagorchev
Telerik
Hi Nikola,
Thanks for you reply,
I tried using the below code to authenticate the users,
private string InvokeRequest(string sitefinityHost, string serviceUrl, string webMethod, string httpMethod, byte[] data)
try
// configure and create the web request
string url = String.Concat(sitefinityHost, serviceUrl, webMethod);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = httpMethod;
request.ContentType = "application/json; charset=utf-8";
// if saved cookies are available, add them to the request
request.CookieContainer = new CookieContainer();
if (cookies != null)
foreach (Cookie cookie in cookies)
if (!cookie.Expired)
request.CookieContainer.Add(cookie);
//// write to the request stream.
if (data != null)
request.ContentLength = data.Length;
byte[] b = null;
using (Stream stream = request.GetRequestStream())
stream.Write(data, 0, data.Length);
// get the response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
cookies = response.Cookies;
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
return reader.ReadToEnd();
catch (Exception ex)
Response.Write(ex.ToString());
return null;
But I am getting error - The remote server returned an error: (401) Unauthorized.
And also the Stream length shows - Length = 'stream.Length' threw an exception of type 'System.NotSupportedException'
Please help..
Hello Arun,
Please, check this blogpost how services that requires authentication could be requested. Here is the sample code from the post that I think you would find helpful. It shows how to authenticate using forms or claims and make an authenticated request to a service.
Regards,
Nikola Zagorchev
Telerik
Hi,
Thanks for you reply,
I tried the same code, but when I try to authenticate with "localhost/.../Authenticate" from my web application to sitefinity application which in my localhost IIS server,
he remote server returned an error: (401) Unauthorized - error is throw at response = (HttpWebResponse)request.GetResponse();
Thanks,
Arun
Hi Arun,
Please, note if you are using claims or forms authentication and get the sample code for the according authentication. You can also take a look at the sample attached for authenticating users in applications using claims through the service. I hope it is useful.
Regards,
Nikola Zagorchev
Telerik