+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Restful service

Restful service

6 posts, 0 answered
  1. Arun
    Arun avatar
    10 posts
    Registered:
    03 Nov 2011
    28 Aug 2014
    Link to this post

    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

     

  2. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    02 Sep 2014
    Link to this post
    Hi Arun,

    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" %>
    Then in the service interface define the service and operation contracts, as in a standard wcf service.
    In the service class it is important to add the following attributes:
    Copy Code
    [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:
    Copy Code
    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)
            {
            }
        }
    }
    2.Register the service:
    Copy Code
    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
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Arun
    Arun avatar
    10 posts
    Registered:
    03 Nov 2011
    03 Sep 2014 in reply to Nikola Zagorchev
    Link to this post

    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..

  4. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    04 Sep 2014
    Link to this post
    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
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  5. Arun
    Arun avatar
    10 posts
    Registered:
    03 Nov 2011
    09 Sep 2014 in reply to Nikola Zagorchev
    Link to this post

    Hi,

     

    Thanks for you reply,

     

    I tried the same code, but when I try to authenticate with "http://localhost/Sitefinity/Services/Security/Users.svc/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

  6. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    12 Sep 2014
    Link to this post
    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
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
6 posts, 0 answered