Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ishan Vyas 67 posts 198 karma points
    Dec 03, 2018 @ 07:25
    Ishan Vyas
    0

    hii guys i want some tips to carry on umbraco web api in my website. example i want to create an about us or blog page as an api in my site so how can i render the data from content node to the web api controller. Can you suggest how the data entered in the content node can be used by api in the controller file. can you suggest any code snippets which i can use to return the data of some already created page in website to use in controller file.

  • Harry Spyrou 212 posts 604 karma points
    Dec 03, 2018 @ 10:01
    Harry Spyrou
    1

    Hi Ishan did you check the umbraco web api documentation?

  • Ishan Vyas 67 posts 198 karma points
    Dec 03, 2018 @ 10:23
    Ishan Vyas
    0

    yes i have gone through those documentation but i dont now how those pages which are published i can use it in api and render those values in the api controller

  • Harry Spyrou 212 posts 604 karma points
    Dec 03, 2018 @ 11:44
    Harry Spyrou
    1

    Show me your controller code and your model if you have? Simplify it if it's too complicated please.

  • Ishan Vyas 67 posts 198 karma points
    Dec 03, 2018 @ 11:59
    Ishan Vyas
    0

    what i am doing is going through some custom work and this is purely for learning purpose, so i am going through series of components which are used in umbraco and practically implement it in my website. i want to learn how i can implement umbraco api on my already built pages, lets say About us/blogs page, i have those page and i want to built an api used to display the data which is stored in the content area of the same . so how can i achieve this?? I may sound a bit inexperienced but i am very much interested in learning this things. Sorry in advance for my silly doubts.😇

  • Harry Spyrou 212 posts 604 karma points
    Dec 03, 2018 @ 12:11
    Harry Spyrou
    1

    This shows you how to implement an API controller for Umbraco:

    https://our.umbraco.com/documentation/reference/routing/webapi/

    However, if you're just looking to get content nodes from umbraco you could use surface controllers.

    Without seeing any code I can't exactly pinpoint what you want to do, but I think the links help a lot.

  • Ishan Vyas 67 posts 198 karma points
    Dec 05, 2018 @ 13:01
    Ishan Vyas
    0

    I have seen the documentation but i want some syntax or code snippets for the same, how to retrieve the umbraco content nodes data into an umbracoapi controller. can you please help me out i am stuck into this and cant find any proper solution. if possible can you give some code snippets regarding my topic of concern?? it will be great help. thanx in advance

  • Ishan Vyas 67 posts 198 karma points
    Dec 05, 2018 @ 13:11
    Ishan Vyas
    0

    i have an blog section and want to create umbraco api for the same, i have an running blog section in my website now i want to use api for the same. below i am giving an example of how my blog code is:

    `@inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "Webbase.cshtml"; }

        <div class="houserro_wrapper">
          <div class="houserro_section greenbg_wrapper">
            <div class="saledocs_wrapper">
              <div class="container">
                <div class="row">
                  <div class="col-sm-12">
                    <div class="about_title green_section">
    
                      <h3 class="post-title" itemprop="name headline" align="center" style="text-align:justify">@Umbraco.Field("blogTitle")</h3>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="container container_for_mobile">
              <div class="container">
                <div class="row">
                  <div class="col-sm-12 contact_section">
                    <div class="content_wrapper Tabs_content_wrapper">
                        <div class="content-area">
                            <p class="post-meta">
                                <time class="dt-published"  itemprop="datePublished"><strong>@Umbraco.Field("articleDate", formatAsDate: true) </strong></time>
                            </p>
    
                            <p font size="6"><strong>
                                By <span>@Umbraco.Field("articleBy")</span>
                                </strong>
                            </p>
    
                            &nbsp;
                            <div class="post-content e-content" itemprop="articleBody">
                            <p><font size="4">@Umbraco.Field("blogDescp")</font></p>
    
    
                            </div><a class="u-url" href="/2018/07/12/one-simple-tool.html" hidden></a>
                         @Html.Partial("PageComments", new PageComment.Comments.Comment())
    
                        </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>`
    

    below is model for the same

    using System;
    

    using System.Collections.Generic; using System.Linq; using System.Web;

    namespace Houseroo1.Models { public class BlogsArticleModel { public string Title { get; set; } public string Intro { get; set; } public string Url { get; set; } public DateTime ArticleDate { get; set; }

    }
    

    }

    And this is the controller of the same :

    using System;
    

    using System.Collections.Generic; using System.Linq; using System.Web; using Umbraco.Web.Mvc; using System.Web.Mvc; using Houseroo1.Models; using Umbraco.Core.Models; using Umbraco.Web; using Houseroo1.Extensions;

    namespace Houseroo1.Controllers { public class BlogsController : SurfaceController { public string GetViewPath(string name) { return $"/Views/Partials/HomeContent/{name}.cshtml"; }

        public ActionResult RenderBlogs(bool isHomepage, int itemsToShow)
        {
    
            BlogsListModel model = new BlogsListModel();
            IPublishedContent homePage = CurrentPage.AncestorOrSelf("home");
            IPublishedContent blogsList = homePage.Children.Where(x => x.DocumentTypeAlias == "blogsList").FirstOrDefault();
            List<IPublishedContent> blogsItems = blogsList.Children.Where(x => x.DocumentTypeAlias == "blogArticle").OrderByDescending(y => y.GetPropertyValue<DateTime>("articleDate")).ToList();
            model.IsHomePage = isHomepage;
    
            List<BlogsArticleModel> blogsArticles = null;
            if(blogsItems != null && blogsItems.Count > 0)
            {
    
                blogsArticles = blogsItems.Select(x => IPublishedContentExtentions.ToBlogsArticleModel(x)).ToList();
            }
            model.Items = itemsToShow > 0 ? blogsArticles.Take(itemsToShow).ToList() : blogsArticles;
            return PartialView(GetViewPath("_BlogSection"), model);
        }
    }
    

    }

    can you help me how to create an api controller for the same. can you advice me some syntax or code snippets from where i can know how its done.

    also i saw a article here but in this the data is been used in normal controller "https://our.umbraco.com/forum/developers/api-questions/29127-Getting-data-from-umbraco-inside-a-controller" i want it in umbracoapi so how can it be done?

  • Harry Spyrou 212 posts 604 karma points
    Dec 05, 2018 @ 13:14
    Harry Spyrou
    0

    Sorry man, would you mind formatting the controller correctly? i can't read it like this.

  • Ishan Vyas 67 posts 198 karma points
    Dec 05, 2018 @ 13:16
    Ishan Vyas
    0
    namespace Houseroo1.Controllers
    
    public class BlogsController : SurfaceController
    {
        public string GetViewPath(string name)
        {
            return $"/Views/Partials/HomeContent/{name}.cshtml";
        }
    
        public ActionResult RenderBlogs(bool isHomepage, int itemsToShow)
        {
    
            BlogsListModel model = new BlogsListModel();
            IPublishedContent homePage = CurrentPage.AncestorOrSelf("home");
            IPublishedContent blogsList = homePage.Children.Where(x => x.DocumentTypeAlias == "blogsList").FirstOrDefault();
            List<IPublishedContent> blogsItems = blogsList.Children.Where(x => x.DocumentTypeAlias == "blogArticle").OrderByDescending(y => y.GetPropertyValue<DateTime>("articleDate")).ToList();
            model.IsHomePage = isHomepage;
    
            List<BlogsArticleModel> blogsArticles = null;
            if(blogsItems != null && blogsItems.Count > 0)
            {
    
                blogsArticles = blogsItems.Select(x => IPublishedContentExtentions.ToBlogsArticleModel(x)).ToList();
            }
            model.Items = itemsToShow > 0 ? blogsArticles.Take(itemsToShow).ToList() : blogsArticles;
            return PartialView(GetViewPath("_BlogSection"), model);
        }
    }
    

    sorry for the formatting

  • Ishan Vyas 67 posts 198 karma points
    Dec 05, 2018 @ 13:21
    Ishan Vyas
    0

    actually i am pretty much newbie so dont know how to carry on api in here. i want to create an umbraco api for the blogs in which i can perform the task and also i can retrieve the already created blogs in backoffice. as seen below in figure.i am working on sample website which is purely for learning basis enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft