Copied to clipboard

Flag this post as spam?

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


  • Max Mumford 266 posts 293 karma points
    Jan 17, 2011 @ 12:26
    Max Mumford
    0

    Object scope issue in .net & cs code behind

    Hi all,

    I am writing an extension for the umbraco package CMSImport that organises your posts into their respective date folders.

    This is the first time I have written any .net and I am having a scope problem (I think...)

    I have a .aspx page with a listbox that should get auto populated with the nodes from umbraco on page load but visual studio is showing an error saying "The name 'listOfNodes' does not exist in the current context".

    Here is my code behind:

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using umbraco.presentation.nodeFactory;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    
    namespace umbracoDocumentApi
    {
        public partial class editContent : System.Web.UI.UserControl
        {
            public void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    //get details for the current node
                    Node currentPage = Node.GetCurrent();
    
                    //use those details to go to the top level node
                    Node topPage = Node.GetCurrent();
                    while (topPage.Parent != null)
                    {
                        topPage = topPage.Parent;
                    }
    
                    //and add each child of the top level node (every page on the site) to the drop down box
                    foreach (Node Child in topPage.Children)
                    {
                        listOfNodes.Items.Add(Child.Id.ToString());
                    }
                }
            }
    
            protected void go_click(object sender, EventArgs e)
            {
                if (Page.IsValid)
                {
                    Node Parent = new Node(int.Parse(listOfNodes.SelectedValue));
    
                    foreach (Node Child in Parent.Children)
                    {
                        //create blog date folder nodes ///////////////////////////////////////////////////////////////////////////
    
                        //for each child of the selected node, load create dates
                        DateTime createDate = Child.CreateDate;
                        int year = createDate.Year;
                        int month = createDate.Month;
                        int day = createDate.Day;
    
                        //load date page doc type
                        DocumentType BlogDateFolderDocTypeId = DocumentType.GetByAlias("umbracoBlogDateFolder");
    
                        //load author info
                        User author = User.GetUser(0);
    
                        //create year, month and date folders
                        Document createYearDateFolder = Document.MakeNew(year.ToString(), BlogDateFolderDocTypeId, author, int.Parse(listOfNodes.SelectedValue));
                        Document createMonthDateFolder = Document.MakeNew(month.ToString(), BlogDateFolderDocTypeId, author, createYearDateFolder.Id);
                        Document createDayDateFolder = Document.MakeNew(day.ToString(), BlogDateFolderDocTypeId, author, createMonthDateFolder.Id);
    
                        //publish those folders
                        createYearDateFolder.Publish(author);
                        createMonthDateFolder.Publish(author);
                        createDayDateFolder.Publish(author);
    
                        //update the umbraco cache
                        umbraco.library.UpdateDocumentCache(createYearDateFolder.Id);
                        umbraco.library.UpdateDocumentCache(createMonthDateFolder.Id);
                        umbraco.library.UpdateDocumentCache(createDayDateFolder.Id);
    
                        //move blog posts into their appropriate blog date folders //////////////////////////////////////////////////
    
                        //specify document to be moved
                        Document itemToMove = new Document(Child.Id);
    
                        //move the document
                        itemToMove.Move(createDayDateFolder.Id);
    
                    }
    
                    //refresh umbraco library
                    umbraco.library.RefreshContent();
                }
            }
        }
    }

     

    Here is my .aspx page code:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="organisePosts.aspx.cs"
        Inherits="umbracoDocumentApi.organisePosts" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <div style="border: 1px solid #ccc; padding: 10px">
            <p>
                <asp:ListBox ID="listOfNodes" runat="server"></asp:ListBox>
            </p>
            <asp:Button ID="go" runat="server" Text="Go" OnClick="go_click" />
        </div>
    </body>
    </html>
    
    Can anybody shed any light?
    Thanks,
    Max.
  • Max Mumford 266 posts 293 karma points
    Jan 24, 2011 @ 11:52
    Max Mumford
    0

    Anybody?

  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Jan 24, 2011 @ 12:45
    Richard Soeteman
    1

    Hi,

     Your class uses umbracoDocumentApi.editContent and derives from Usercontrol. So it doesn't match with the page. On the aspx page change Inherits="umbracoDocumentApi.organisePosts to Inherits="umbracoDocumentApi.editContent" and in the code behind change  System.Web.UI.UserControl
     to System.Web.UI.Page

    Cheers,

    Richard

     

  • Max Mumford 266 posts 293 karma points
    Jan 24, 2011 @ 13:12
    Max Mumford
    0

    Thanks very much, that fixed the errors I was getting. There is now one more:

    Error 1 'System.Security.Principal.IPrincipal' does not contain a definition for 'GetUser' and no extension method 'GetUser' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?) \umbracoDocumentApi\organisePosts.aspx.cs 57 40 umbracoDocumentApi

    Any ideas?

  • Richard Soeteman 4045 posts 12898 karma points MVP 2x
    Jan 24, 2011 @ 13:18
    Richard Soeteman
    0

    If you hover over the Author variable what is the type it gives you?

  • Max Mumford 266 posts 293 karma points
    Jan 24, 2011 @ 13:37
    Max Mumford
    0

    There was no popup if I hover over author. I found this article however:

    http://forum.umbraco.org/yaf_postst6870_Cant-get-GetUser-function-to-work.aspx

    and here is my new code:

    int userID = 0;

    umbraco.BusinessLogic.User author = new User(userID);

    which compiles :) Thanks for your help.

    Max.

Please Sign in or register to post replies

Write your reply to:

Draft