Calling Webservices from External Website

Posted by Community Admin on 04-Aug-2018 10:25

Calling Webservices from External Website

All Replies

Posted by Community Admin on 11-Aug-2011 00:00

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("localhost/.../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?

Posted by Community Admin on 17-Aug-2011 00:00

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

Posted by Community Admin on 17-Aug-2011 00:00

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

Posted by Community Admin on 19-Aug-2011 00:00

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

Posted by Community Admin on 16-Jan-2012 00:00

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. 

Posted by Community Admin on 18-Jan-2012 00:00

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

Posted by Community Admin on 07-Feb-2012 00:00

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? 

Posted by Community Admin on 10-Feb-2012 00:00

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

Posted by Community Admin on 10-Feb-2012 00:00

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.

Posted by Community Admin on 14-Feb-2012 00:00

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

Posted by Community Admin on 14-Jul-2014 00:00

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.

This thread is closed