Copied to clipboard

Flag this post as spam?

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


  • skiltz 501 posts 701 karma points
    Jan 09, 2011 @ 22:22
    skiltz
    0

    Linq2Umbraco + Unpublished Nodes

    Is there any way in Linq2Umbraco to retreive unpublished nodes?

  • skiltz 501 posts 701 karma points
    Jan 09, 2011 @ 22:29
    skiltz
    0

    ohh looks like examine might be the answer to my problem.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jan 09, 2011 @ 23:18
    Aaron Powell
    0

    The NodeDataProvider doesn't support this and this article explains it: http://www.aaron-powell.com/understanding-linq-to-umbraco

    My LINQ to Umbraco Extensions project will support it: http://www.aaron-powell.com/linq-to-umbraco-extensions (you have to compile it yourself as I don't have a release out)

  • skiltz 501 posts 701 karma points
    Jan 10, 2011 @ 00:46
    skiltz
    0

    Thanks Aaron worked perfectly :)

  • firepol 125 posts 173 karma points
    Sep 07, 2011 @ 10:22
    firepol
    0

    Hi,

    I also want to read the unpublished nodes.

    I already downloaded the source code here: http://www.aaron-powell.com/linq-to-umbraco-extensions (by the way what is the difference btw "default" and "tip" in the downloads? I don't see any....)

    Have you got a code sample, like the one for the standard linq to umbraco here? http://our.umbraco.org/wiki/reference/api-cheatsheet/linq-to-umbraco/understanding-the-generated-classes/datacontext

    This is with the standard LINQ to umbraco generated classes:

    using (var ctx = new MyUmbracoDataContext())
    {
        var pages = from page in ctx.CwsTextpages
                    where page.Bodytext.Contains("Umbraco is cool")
                    select page;
        foreach(var page in pages)
        {
            Console.WriteLine(page.Title);
        }
    }

    How would it be with your extension? (And how to configure it to achive this simple task, I don't need to insert/delete/update, just need to read all the documents of a certain types, including the unpublished ones)

    That would help a lot, thank you.

    Cheers

  • skiltz 501 posts 701 karma points
    Sep 07, 2011 @ 11:44
    skiltz
    0

    Try something like the following:

           using (var ctx = new DocumentDataProvider())
                    {
                        var pages = from page in ctx.LoadTree<CwsTextpages>()
                                    where page.Bodytext.Contains("Umbraco is cool")
                                    select page;
                        foreach (var page in pages)
                        {
                            Console.WriteLine(page.Title);
                        }
                    }

     

  • firepol 125 posts 173 karma points
    Sep 07, 2011 @ 17:14
    firepol
    0

    Hi skiltz, strange I think I posted a reply but I don't see it here. So here I write again...

    I inserted the 3 files from the source code "default" (https://bitbucket.org/slace/linq-to-umbraco-extensions/downloads) in my project, exactly how they are.

    Then I added a code like the one above, for 2 different document types. In my case "Event" and "ContentPage", like this:

                using (var ctx = new DocumentDataProvider())
                {
                    var result = from r in ctx.LoadTree<ContentPage>()
                                 select r;

                    List<ContentPage> cont = result.ToList();
                }

    The line 74 of the DocumentTree.cs (the one below, after the //TODO comment) throws an exception in both cases, Event or ContentPage.

                            // TODO: Address how Convert.ChangeType works in globalisation
                            p.SetValue(linqDoc, Convert.ChangeType(data, p.PropertyType), null);

    Here the exceptions:

    The ContentPage List:

    Invalid cast from 'System.String' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

    The Event List:

    Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.

    You had to modify the DocumentTree.cs? Or for you it worked at once? Any ideas?

    Thanks and cheers

  • skiltz 501 posts 701 karma points
    Sep 07, 2011 @ 17:33
    skiltz
    0

    Hey

    I have made some changes in the past but it was ages ago so couldn't tell you what there were. 

    Here is my compiled version.  You could give it a try and see if it works. http://agentx.info/DocumentDataProvider.dll

    Thanks,
    Matthew

     

  • firepol 125 posts 173 karma points
    Sep 08, 2011 @ 11:37
    firepol
    0

    Hi Matthew,

    if you can send me the modified source code (they are just 3 files) I'd like it more ;)

    But I'll give your dll a try and see...

    Thanks again and cheers

  • firepol 125 posts 173 karma points
    Oct 26, 2011 @ 10:29
    firepol
    0

    Hi Matthew, also with your dll I get an exception like this:

    {"Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'."}

    I think I give up with this DocumentDataProvider... what a pity, really. We can use some nice LINQ queries for the PUBLISHED nodes, and no way to do the same with thje unpublished ones. Obviously in the backend this makes the management in some custom sections just incomplete... whatever... thx anyway for trying to help.

  • skiltz 501 posts 701 karma points
    Oct 26, 2011 @ 10:35
    skiltz
    0

    From memory in my DataContext I changed all my Datetime and Int to Nullables.

     eg:

     private DateTime? _FeatureStartDate;
      /// <summary>
      ///
      /// </summary>
      [UmbracoInfo("featureStartDate", DisplayName = "featureStartDate", Mandatory = false)]
      [Property()]
      [System.Runtime.Serialization.DataMemberAttribute()]
      public virtual DateTime? FeatureStartDate
      {
       get
       {
        return this._FeatureStartDate;
       }
       set
       {
        if ((this._FeatureStartDate != value))
        {
         this.RaisePropertyChanging();
         this._FeatureStartDate = value;
                        this.IsDirty = true;
         this.RaisePropertyChanged("FeatureStartDate");
        }
       }
      }

  • firepol 125 posts 173 karma points
    Oct 26, 2011 @ 17:31
    firepol
    0

    Hi Matthew thanks. When I said "I give up" actually I did some research to see what could I do to deal with the null exception and found this:

    http://web.archive.org/web/20061017103920/http://aspalliance.com/852_CodeSnip_ConvertChangeType_Wrapper_that_Handles_Nullable_Types

    I renamed the "Convert" below the // TODO comment in the DocumentTree.cs file to "Convert2" and created a Class for "Convert2". I took the code from the article above.

    I added a few conversion for null DateTime, Guid and Int, like this:

                if (conversionType == typeof(System.Guid))
                {
                    return new Guid(value.ToString());
                }

                if (conversionType == typeof(System.DateTime))
                {
                    try
                    {
                        return DateTime.Parse(value.ToString());
                    }
                    catch (Exception)
                    {
                        return new DateTime();
                    }
                }

                if (conversionType == typeof(System.Int32))
                {
                    try
                    {
                        return Int32.Parse(value.ToString());
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }

    Now the method works: I get a list of Documents of the Type I want. But... I still have a problem: I can't find the "Published" property (in the Document is vailable). I need that information and I wonder why it's not there.

    The "TDocType" hasn't it... I guess I should extend it... I'm checking how...

    I'll do some more research but if somebody has ideas, let me know, cheers

Please Sign in or register to post replies

Write your reply to:

Draft