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)
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); } }
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?
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.
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(); } }
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
Linq2Umbraco + Unpublished Nodes
Is there any way in Linq2Umbraco to retreive unpublished nodes?
ohh looks like examine might be the answer to my problem.
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)
Thanks Aaron worked perfectly :)
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
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);
}
}
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
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
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
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.
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");
}
}
}
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
is working on a reply...