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

Forums / Developing with Sitefinity / Calling Webservices from External Website

Calling Webservices from External Website

11 posts, 0 answered
  1. David
    David avatar
    2 posts
    Registered:
    11 Aug 2011
    11 Aug 2011
    Link to this post
    I'm currently working on a server control to allow sitefinity content to be displayed and edited in a non-sitefinity page. As part of this I'm wanting to use the new webservices available in SF4 (using SP3). Using the code example given in http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/windows-authentication.aspx I've been able to authenticate the session and now have a cookie I can add to the webservice request I create.

    I have been able to pull the data out already using the following code but I'm not entirely clear on whether this is the best way to proceed:

            public string GetContent(Guid contentTag)
            {
                string requestString = string.Format("http://localhost/MyCMS/sitefinity/Services/Content/ContentItemService.svc/xml/{0}", contentTag.ToString());
     
                Uri address = new Uri(requestString);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
     
                request.CookieContainer = new CookieContainer();
                foreach (Cookie cookie in cookies)
                {
                    request.CookieContainer.Add(cookie);
                }
     
                request.KeepAlive = false;
                request.Timeout = 15 * 1000;
     
                request.Method = "GET";
                request.ContentType = "application/json; charset=utf-8";
                string xmlData = string.Empty;
     
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                if (request.HaveResponse && response != null)
                {
                    using (Stream s = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            xmlData = sr.ReadToEnd();
                        }
                    }
                }
                return xmlData;


            }


    What I seem to be lacking is a clear example of how to get and save content using the webservices and any best practices that go along with this and I'm hoping someone might have something like this available?? In the first instance I'm only concerned about the actual content itself but the next phase is to handle versioning and authorisation for publishing etc.

    Can anyone help?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    17 Aug 2011
    Link to this post
    Hello David,

    You can download the source code of the Sitefinity migration tool to see how you can call services to get response and data from them.

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. David
    David avatar
    2 posts
    Registered:
    11 Aug 2011
    17 Aug 2011
    Link to this post
    Radoslav,
    I've had a look at the migration tool project but this (unless I'm missing something) appears to be a web service that has been authored to add into the sitefinity project manager - all the calls to get data appear to be made using the API.

    I'm trying to retrieve data without using the API by making calls to the RESTful webservices as shown in the code I posted in my original mail. I need to retrieve content (which I can currently do but I'm not sure if it's been done in the recommended way) but I also need to be able to save content (subject to workflows for that piece of content). Do you have any code examples that don't use the API and communicate solely with the web services? I'm using server side code so this needs to be done in C# rather than jquery.

    Thanks

    David
  4. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    19 Aug 2011
    Link to this post
    Hi David,

    You code seems ok. You can check this thread on StackOverflow to see how a JSON response may be parsed into C# objects:

    http://stackoverflow.com/questions/401756/parsing-json-using-json-net

    Basically you need to create proxy classes that will hold your deserialized values and then you use JavaScriptSerializer to parse the contents that you've already read from the WebRequest.

    All the best,
    Lubomir Velkov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  5. Rich
    Rich avatar
    27 posts
    Registered:
    20 Feb 2010
    16 Jan 2012
    Link to this post
    Lubomir,
    The link you provided is for serializing / deserializing json.  What we would like is an example of saving new content using the web services.  The json object is used for most crud operations in the sitefinity web services, but that really isn't the problem, it's saving new content whether it's xml or json.  Like saving a new image or creating a new Document library using the web services and not the API. 
  6. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    18 Jan 2012
    Link to this post
    Hi Rich,

     I have answered to you in your support ticket. In general - there is no built-in Sitefinity service for images, as they are uploaded through handlers. If you like to upload images through a webservice, you will have to implement it yourself.

    All the best,
    Svetoslav Petsov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  7. Marc
    Marc avatar
    24 posts
    Registered:
    09 Jun 2011
    07 Feb 2012
    Link to this post
    I can read/select from the services but when I try to use the PUT I get a 412 error returned.

    The remote server returned an error: (412) ..::login|session|expired::../Sitefinity/Login/Ajax.

    I am adding the same cookies that were associated with the authentication call to the new webrequest with all the data serialized backup with all changes.  I am obviously missing an authentication step I would assume? 
  8. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    10 Feb 2012
    Link to this post
    Hi Marc,

     Can you check the following blog post:
    http://www.sitefinity.com/blogs/svetlayankova/posts/11-11-01/getting_started_with_restful_services_in_sitefinity.aspx 
    It holds all the details on how authentication should be performed, so that you can consume all the services through an external application. There is also an example in the post. Look through it and let me know if you have further questions.

    Greetings,
    Svetoslav Petsov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  9. Marc
    Marc avatar
    24 posts
    Registered:
    09 Jun 2011
    10 Feb 2012
    Link to this post
    Yeah that is the post I followed to get to where I am now with it.   I authenticate first and keep the cookies around like you do in the post and then I get a module item back.  The item is comes back and I desialize it into my .net Object so I can modify it.  I change a value and make sure all other metadata that is sent back on the PUT action for an update of an item is correct, but when I send the invoke the final request to update the item I get the 412 error.  I can even toss a try catch into the code hop out of the exception and requery using the same cookies for a different module item without a problem. Seems to only toss the 412 when trying to update the item. I can post code if needed, but I pretty much followed your example exactly other than abstracting out some of the strings to constants and organizing the calls differently.
  10. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    14 Feb 2012
    Link to this post
    Hi Marc,

     This error usually happens if you are logged in the Backend through the browser, because the sample doesn't log out previous to logging in. Can you make sure that you are logged out from the browser and then execute the code again?

    Greetings,
    Svetoslav Petsov
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  11. Jared
    Jared avatar
    122 posts
    Registered:
    04 Apr 2013
    14 Jul 2014 in reply to Svetoslav Petsov
    Link to this post

    I am getting an error as well.  I am able to hit the login button and that portion works, but when I get hit the List Data, I get an error on line 110.  It states that "There was an error deserializing the object of type SFWinformsClient.Model.Roles.Role. Encountered unexpected character 'Y'."  Then, it gives me "Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''."  I believe this is part with the value I have for "Role Result", which is "You do not have permission to view this directory or page."

    What I am looking to do is to utilize the RESTful Services to edit a field in a Dynamic Module.  I have a field that is blank and would like to copy the value of the "Title" field into the new blank field I have already created.  I would like to accomplish this via a Windows Form or Console Application that is outside of the current Sitefinity application.

11 posts, 0 answered