Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 06, 2015 @ 12:36
    Owain Williams
    0

    Using UmbracoTemplatePage instead of ViewPage

    How would I convert this so it works with Umbraco.Web.MVC.UmbracoTemplatePage? I want to be able to use CurrentPage.

     @using Skybrud.Social.Twitter
        @using Skybrud.Social.Twitter.Objects
        @using Skybrud.Social.Umbraco.Twitter.PropertyEditors.OAuth
        @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    
        @{
    
            // Get the OAuth information stored in the property
            TwitterOAuthData twitter = Model.GetPropertyValue("twitterFeed") as TwitterOAuthData;
    
            // Check whether the OAuth data is valid
            if (twitter != null && twitter.IsValid) {
    
                // Gets an instance of TwitterService based on the OAuth data
                TwitterService service = twitter.GetService();
    
                // Get information about the authenticated user
                TwitterUser user = service.Users.GetUser(twitter.Id).Body;
    
                // Get recent status messages (tweets) from the authenticated user
                TwitterStatusMessage[] recent = service.Statuses.GetUserTimeline(user.Id, 5).Body;
    
                <fieldset>
                    @foreach (TwitterStatusMessage status in recent) {
                    <p>@status.Text</p>
                    }
                </fieldset>
    
            }
    
            if (CurrentPage.HasValue("instagramSocial", true))
                {
                    <p>@CurrentPage.instagramSocial</p>
                }
    
        }
    
  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 09, 2015 @ 09:22
    Owain Williams
    0

    Hate to bump this but really want to the use of CurrentPage with this. Hope someone can help as I can't work it out.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 11, 2015 @ 15:13
    Sebastiaan Janssen
    0

    I think it's really not necessary to use CurrentPage since you have Model now with the same capabilities? If you tell us what specifically do you miss from CurrentPage we can help.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 12, 2015 @ 10:53
    Owain Williams
    0

    Hi, Thanks for the reply. I am trying to access a document type which belongs to another page

    i.e On my homepage, I have a 'Social media' tab, which holds my social media usernames, I want to be able to use the code above on other pages as well as the homepage but don't want to have to enter my social media details or create a 'Social media' tab on every page I create.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 12, 2015 @ 11:55
    Sebastiaan Janssen
    0

    Alright, that's pretty similar to how you would do it with CurrentPage so it would look something like this:

    var rootNode = Model.Content.AncestorOrSelf(1);
    var twitterUserName = rootNode.GetPropertyValue<string>("twitterUserName");
    

    To explain: Model.Content.AncestorOrSelf(1) means: from the current page (Model.Content) go up through the list of all of the ancestors and stop at level 1. That's presumably where your homepage lives.

    Then find the property with alias twitterUserName on the homepage and get the string value of it as filled in on the homepage (rootNode).

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 13, 2015 @ 08:02
    Owain Williams
    0

    Thanks Sebastiaan, but I am getting a compilation error:

    CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
    

    It is highlighting the line

    var rootnode = Model.Content.AncestorOrSelf(1);
    

    I'm uinheritting Umbraco.Web.Mvc.UmbracoViewPage, do I need anything else?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 13, 2015 @ 08:16
    Sebastiaan Janssen
    0

    Ah, alright, I see what's going on. So you haven't explicitly been given the content. Alright, should be easy to fix:

    To get to the content like I listed above use the following:

    var currentContent = Umbraco.TypedContent(UmbracoContext.PageId.Value);
    var rootNode = currentContent.AncestorOrSelf(1);
    var twitterUserName = rootNode.GetPropertyValue<string>("twitterUserName");
    
  • Anders Bjerner 487 posts 2995 karma points MVP 8x admin c-trib
    Jul 13, 2015 @ 12:42
    Anders Bjerner
    0

    Hi Owain,

    I think Sebastiaans initial code example was right, but you probably need to integrate it a bit into the view depending on which class you're inheriting from.

    If you can post your entire cshtml file, we can work on from there ;)

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 13, 2015 @ 20:49
    Owain Williams
    0

    Hi, Thanks for your help!

    Here is all my code.

    Master template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
    
        <title>@Umbraco.Field("pagetitle")</title>
    
    
        <!-- Bootstrap Core CSS -->
        <link href="/css/bootstrap.min.css" rel="stylesheet">
    
        <!-- Custom CSS -->
        <style>
        body {
          /* padding-top: 70px; */
            /* Required padding for .navbar-fixed-top. Remove if using .navbar-static-top. Change if height of navigation changes. */
        }
        </style>
    
        <link href="~/css/style2.css" rel="stylesheet" />
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    
    </head>
    
    <body class="home page page-id-39 page-template page-template-page-custom page-template-page-custom-php custom-background-empty custom-font-enabled single-author l-wide">
        <header id="masthead" class="site-header" role="banner">
            <nav id="site-navigation" class="main-navigation navbar navbar-default navbar-static-top" role="navigation">
    
                <div class="container">
    
                    <!-- Brand and toggle get grouped for better mobile display -->
                    <div class="navbar-header">
    
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <a class="navbar-brand" href="/">RunningBeside.me</a>
                    </div>
                    <!-- Collect the nav links, forms, and other content for toggling -->
                    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                        @Umbraco.RenderMacro("siteNav")
                    </div>
                    <!-- /.navbar-collapse -->
                </div>
                <!-- /.container -->
            </nav>
    
        </header>
        <div id="primary" class="site-content">
            <div id="content" role="main">
                <div class="container">
    
                    @RenderBody()
    
    
        <footer id="colophon" role="contentinfo">
    
    
            <div id="supplementary">
                <div class="container">
                    <div class="row2">
                        <div class="col-md-4">
                            <div id="first" class="widget-area" role="complementary">
                                <aside id="text-2" class="widget widget_text">
                                    <div class="textwidget">
                                        <img style="margin-bottom:25px; margin-top:7px;" src="/Css/images/branding.png">
                                        <p>Mandy and Owain Williams run in and around Edinburgh, Scotland. Running all distances from 1 mile to marathons. </p>
                                        <p>This site gives you a glimpse in to where they train, race and run. They try and blog as much as possible, but running always comes first!</p>
    
                                    </div>
                                </aside><aside id="social_links-widget-2" class="widget social_links">
                                    <h3 class="widget-title">Stay Connected</h3>        <ul class="social-links ">
                                        <li>
                                            <a class="facebook" href="@Umbraco.Field("facebook", recursive: true)" target=""><i class="fa fa-facebook"></i></a>
                                        </li>
    
                                        <li>
                                            <a class="twitter" href="@Umbraco.Field("twitterSocial",recursive: true)" target=""><i class="fa fa-twitter"></i></a>
                                        </li>
    
    
                                        <li>
                                            <a class="instagram" href="@Umbraco.Field("instagramSocial",recursive: true)" target=""><i class="fa fa-instagram"></i></a>
                                        </li>
    
    
    
                                    </ul>
                                </aside>
                            </div><!-- #first .widget-area -->
                        </div>
                        <div class="col-md-4">
                            <div id="second" class="widget-area" role="complementary">
                                <aside id="featured-posts-2" class="widget widget_featured_entries">
                                    <h3 class="widget-title">Recent Tweets</h3>   
    
     @Html.Partial("twitterFeed")
    
    
                                </aside>
                            </div><!-- #second .widget-area -->
                        </div>
    
                        <div class="col-md-4">
                            <div id="third" class="widget-area" role="complementary">
                                <aside id="featured-posts-2" class="widget widget_featured_entries">
                                    <h3 class="widget-title">Instagram</h3>
                               @Html.Partial("instagram")
                                    </aside>
                            </div><!-- #third .widget-area -->
                        </div>
                    </div>
                </div>
    
    
    
            </div><!-- #supplementary -->
    
    
            </div>
    <center>@Umbraco.Field("footerText", altText: "Copyright ", recursive: true)</center>
      </footer>
        <script src="/js/jquery.js"></script>
        <!-- Bootstrap Core JavaScript -->
        <script src="/js/bootstrap.min.js"></script>
    
    </body>
    </html>
    

    instagram partial:

       @using Skybrud.Social.Instagram
        @using Skybrud.Social.Instagram.Objects
        @using Skybrud.Social.Instagram.Responses
        @using Skybrud.Social.Umbraco.Instagram.PropertyEditors.OAuth
        @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    
        @{
    
            // Get the OAuth information stored in the property
            InstagramOAuthData instagram = Model.GetPropertyValue("instagram") as InstagramOAuthData;
    
            // Check whether the OAuth data is valid
            if (instagram != null && instagram.IsValid) {
    
                // Gets an instance of InstagramService based on the OAuth data
                InstagramService service = instagram.GetService();
    
                // Gets information about the authenticated user
                InstagramUserResponse self = service.Users.GetSelf();
    
                // Get recent media from the authenticated user
                InstagramRecentMediaResponse recent = service.Users.GetRecentMedia(self.Body.Data.Id, 4);
    
    
    
                <fieldset>
                    @foreach (InstagramMedia media in recent.Body.Data) {
                    <a href="@Umbraco.Field("instagramSocial")"><img src="@media.Thumbnail" alt="@media.CaptionText" height="110px" width="110px" /></a>
                    }
                </fieldset>
    
            }
    
        }
    

    I would use it for a twitter partial as well but if I can pull the data from one field then I can do it for all of them.

    As a side note, what's the best way to learn about all of this, other than the forum?

    Thanks!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 14, 2015 @ 06:22
    Sebastiaan Janssen
    100

    It really does work though :-) In your Instagram partial, after @{ something like:

    var currentContent = Umbraco.TypedContent(UmbracoContext.PageId.Value);
    var rootNode = currentContent.AncestorOrSelf(1);
    InstagramOAuthData instagram = rootNode .GetPropertyValue("instagram") as InstagramOAuthData;
    

    Maybe this even works:

    var instagram = rootNode.GetPropertyValue<InstagramOAuthData>("instagram");
    
  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jul 14, 2015 @ 07:52
    Owain Williams
    0

    Brilliant! It was the second part to your reply that made it work for me!

    I think it's lack of understanding that I'm struggling with most - might look at a Umbraco.TV subscription and see if that helps me with the basics.

    Thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft