Hi,
I'd like to use the REST API (direct) to get a list of the all the most recent blog posts, in a specific blog group, in most-recent-first order.
Could anyone help me with this, please? The best I've got so far is:
http://localhost/cs/api/blogs.ashx/posts/?GroupIds=10&IsActive=true&PageSize=3&SortOrder=Descending
Thanks,
Matt.
Ok, I've seen that (possibly catching on a little late here) that the elements that I wish had more information actually have attributes named Endpoint and that their URL can provide me with another request to get that information. So I've written a routine which gets the initial document and iterates over it, to Nth depth, adding the extra XML document retrieved when an Endpoint element is found. I've included the Transform routine I use, just in case anyone's interested...
protected string ResolveRestRequest(string url, int endPointDepth) { // get initial xml string xmlresponse = GetResponse(url); if (xmlresponse == null) return null; // create xml document XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlresponse); // create xml navigator XPathNavigator nav = doc.CreateNavigator(); // iterate over every element, looking for elements with EndPoint attributes if (nav.MoveToFirstChild()) { bool traversing = true; while (traversing) { // does the current node have an endpoint url? string endpointUrl = nav.GetAttribute("Endpoint", String.Empty); if (endpointUrl != String.Empty && endPointDepth > 0) { // resolve element string subContent = ResolveRestRequest(endpointUrl, endPointDepth - 1); // insert under the current node try { if (subContent != null) { XmlDocument trial = new XmlDocument(); trial.LoadXml(subContent); if (trial.FirstChild.Name != nav.Name) nav.InsertBefore(subContent); //nav.MoveToNext(); } } catch (Exception ex) { } } // find the next element, child, sibling or parent if (!nav.MoveToFirstChild()) // if there's no child node, try for next sibling... while (!nav.MoveToNext()) // if there's no sibling, try to move to the parent... if (!nav.MoveToParent()) // if there's no parent, exit { traversing = false; // break out of both loops break; } } } return nav.OuterXml; }
protected string GetResponse(string url) { //list.Add(url); // get cache key and url to call //url = Servername.TrimEnd('/') + url; try { // create request object WebRequest req = WebRequest.Create(url); req.Credentials = CredentialCache.DefaultCredentials; // add header to authorise req.Headers.Add("Rest-User-Token: " + EncodeTo64(ApiKey + ":" + UserName)); // add impersonation header to appear as a user (not the admin user used to access the api) //req.Headers.Add("Rest-Impersonate-User: jsmith"); // TODO: For the time being, this will only cater for GET requests, but we can easily modify for POST requests // set request type req.Method = "GET"; //req.Method = String.IsNullOrEmpty(reqData) ? "GET" : "POST"; req.ContentType = "text/xml"; // provide content for POST request, if necessary req.ContentLength = 0; // (reqData ?? "").Trim().Length; if (req.Method != "GET") { Stream dataStream = req.GetRequestStream(); byte[] arr = null; // new ASCIIEncoding().GetBytes(reqData); dataStream.Write(arr, 0, arr.Length); dataStream.Close(); } // get response from server WebResponse webResponse = req.GetResponse(); StreamReader reader = new StreamReader(webResponse.GetResponseStream()); // display the content string responseFromServer = reader.ReadToEnd(); // clean up the streams reader.Close(); webResponse.Close(); // return dataset return responseFromServer; } catch (Exception ex) { return null; } }
/// <summary> /// Provides transformation of the returned XML into HTML by using the XSLT file indicated by the XSLFile property. /// </summary> /// <returns></returns> protected string Transform(string xml) { // check that we need to do an xsl transform if (XslFile == null) return xml; // load the xml to be transformed MemoryStream input = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml)); XPathDocument inputDoc = new XPathDocument(input); input.Close(); // configure the compiler XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.ConformanceLevel = ConformanceLevel.Fragment; // create the html output store StringBuilder builder = new StringBuilder(); XmlWriter xmlwriter = XmlWriter.Create(builder, settings); // create the xslt transformer and perform transformation XslCompiledTransform compiled = new XslCompiledTransform(); compiled.Load(MapPath(XslFile)); compiled.Transform(inputDoc, xmlwriter); // output html return builder.ToString(); } /// <summary> /// Encodes a string into Base 64. /// </summary> /// <param name="toEncode"></param> /// <returns></returns> protected static string EncodeTo64(string toEncode) { byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(toEncode); string returnValue = Convert.ToBase64String(toEncodeAsBytes); return returnValue; } /// <summary> /// Decodes a Base 64 string to normal. /// </summary> /// <param name="encodedData"></param> /// <returns></returns> protected static string DecodeFrom64(string encodedData) { byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData); string returnValue = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes); return returnValue; }
You need to make sure you add the appropiate headers to your request to ensure your request can be authenticated - see http://api.communityserver.org/introduction/ for more details.
Community Server Documentation
Nintendo Wiikly
see the link. it might be helpful
---> http://api.communityserver.org/blogs/
Thanks guys, but I am authenticating already. What I was looking for was the best way to get the latest blogs posts from all blogs within one group. I take it from the lack of response on that point that I've nailed the filtering itself? It seems to work fine...
I also see that the information coming back from the Blog Post XML is not quite as complete as I'd like.
For example, on the http://dev.communityserver.com/forums/ page you can see, at the top just under 'Topics', that there is a list of 5 posts. With these there is Title, Date, Author Name, Is Answered or Not and the Number of Replies information.
Could anyone tell me how I would replicate this panel in my own code by using the RESTful services, please?
I am currently pulling back XML using this request:
/api/blogs.ashx/posts/?GroupIds=10&IsActive=true&PageSize=3&SortOrder=Descending
Then I use some XSL to transform the returned XML into HTML for rendering on the site. The problem, of course, is that there is not all the information I want in the initial response and that to get it, as far as I can see right now, I'd have to make numerous calls for each item.
Copyright© 2008 Telligent Systems Inc. All rights reserved CommunityServer.com • Telligent.com