Umbraco get llist of pages created by user - c#

Is possible to get pages which were created by user in umbraco cms. For example my username is admin, and I want to get list all of pages which were created by me using c# code (page name and page url).

It is possible and pretty straight forward. Here are a couple of examples (I'm using razor to spit out the values, but this could easily be put into a user control, or written out to a file or whatever):
Using DynamicNode:
#{
var userId = 0; //admin
var root = Library.NodeById(-1);
var nodes = root.Descendants().Where("CreatorId == #0", userId);
foreach (var node in nodes)
{
#:#node.Id, #node.Name, #node.Url<br />
}
}
Using NodeFactory and uQuery:
#{
var userId = 0; //admin
var root = new Node(-1);
var nodes = root.GetDescendantNodes(n => n.CreatorID == userId);
foreach (var node in nodes)
{
#:#node.Id, #node.Name, #node.Url<br />
}
}
Just replace 0 withe the Id of your user.

Related

Umbraco Multinode picker, match list with currentpage

I need some help with logics how to loop through the items selected in the multinodepicker on all childrens and match them with currentpage type...
Current code:
#{
var constructionInfo = Umbraco.Content(2032); //Driftinfo
}
#Articles(constructionInfo)
#helper Articles(IPublishedContent page)
{
//ToDo: Match contentpicker or tags with currentpage.
var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter") == Model.Content.DocumentTypeAlias);
var relatedArticles = page.Children;
foreach (var article in children)
{
<article>
<h2>#article.GetPropertyValue("rubrik")</h2>
#article.GetPropertyValue("text")
</article>
}
}
So basicly what I tried doing with
var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter") == Model.Content.DocumentTypeAlias);
Was to match the property with the Model.Content.DocumentTypeAlias. However, I need to somehow match them with the multiple content in the contentpicker as its not single...
Could anyone assist me in finding a solution?
var children = page.Children.Where(x => x.GetPropertyValue<string>("relaterandeFastigheter").Split(',').ToList().Contains(Model.Content.Id.ToString()));
That's how I solved it!

Creating a CSV string in Razor foreach

I am trying to parse multiple page IDs into a cookie in Umbraco Razor.
I have this piece of code
#{
var siteroot = Model.AncestorOrSelf(1);
HttpCookie eventCookie = new HttpCookie("eventCookie");
}
#foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
{
if (DateTime.Today <= child.Date)
{
#RenderPage("~/macroscripts/RenderEventBox.cshtml", child, false)
}
}
#{
Response.Cookies.Add(eventCookie);
}
What I want to do is to create a CSV string in the foreach, with page IDs.
The page IDs should then be inserted in a cookie in which I can check for new IDs since the last time the user visited - for a "This is the new pages since your last visit" functionality.
But I am not sure how to do this.
The outcome that I want should look like
2525,4587,4789,4790,5858,5782,7899
which I then can put into a cookie.
Got it solved quite simple with
#{
var csv = "";
}
#foreach (var child in siteroot.Descendants("Event").OrderBy("Date"))
{
csv += "," + #child.Id;
}

Filter Umbraco dynamic variable by Alias in foreach

I am trying to filter a dynamic variable by the Alias and would like to know if this is possible as when I write a lamba expression eg
foreach (var item in node.Children.Where(x => x.NodeTypeAlias != "ContentContainer"))
This results in an error - use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delagate or an expression tree type..
Please can someone help me on this.
if (sub.NodeTypeAlias == "MenuExternalLink")
{
UrlPickerState urlPicker = UrlPickerState.Deserialize(sub.GetPropertyValue("LinkURL"));
subUrl = urlPicker.Url;
subTarget = urlPicker.NewWindow ? "_blank" : "";
//get root home page node
var homePage = CurrentModel.AncestorsOrSelf(1).First();
//get the node under the homepage where the node name matches the redirect (MenuExternalLink) node name
var menuNode = homePage.Children.Where(x => x.NodeTypeAlias == "NormalTextPage" && x.Name.Contains(sub.Name) && x.GetPropertyValue("umbracoNaviHide") != "1");
//get the nodes under menuNode
foreach (var q in menuNode)
{
//we have the id of the node so get the child nodes based on that id
var node = Library.NodeById(q.Id);
foreach (var item in node.Children.Where("NodeTypeAlias == \"NormalTextPage\""))
{
<li ><a href="#item.Url" >#item.Name</a></li>
}
}
}

item.GetProperty("bodytext").Value Umbraco

Hello I've had an issue with gathering an object from my Umbraco Blog page specificly cropping it down
#foreach (var item in Model.Content.Children.Where("visible==true"))
{
var BodyTextToCrop = item.GetProperty("bodytext").Value.ToString();
#item.Name<br />
#Umbraco.Truncate(BodyTextToCrop, 2, true)
}
Change your statement to the following:
#foreach (var item in Model.Content.Children.Where(x => x.IsVisible()))
{
var BodyTextToCrop = item.GetProperty("bodytext").Value.ToString();
#item.Name<br />
#Umbraco.Truncate(BodyTextToCrop, 2, true)
}
It looks like your where clause is using the dynamic access of the type and Model.Content could be strongly typed in your case.
Take a look at querying - MVC
Snippet on the page:
//dynamic access
#CurrentPage.Children.Where("Visible")
//strongly typed access
#Model.Content.Children.Where(x => x.IsVisible())

How to find out the parent page Id from IPageStructure? (Composite C1)

Is this the best way to get the parentId of a page from the IPageStructure in Composite C1 within a UserControl ?
string pPageId = SitemapNavigator.CurrentPageId.ToString();
Guid parentId = connection.Get<IPage>().Where(p => p.Id == new
Guid(pPageId)).First().GetParentId();
I do use the present pageId as a string, which helps in the above case seeing as .First() can't be used on Guid.
Take a look at the SitemapNavigator instance class you can grab from DataConnection - when you get the data bound instance it gives you access to the current page in form of a PageNode class from which you can access parent, child pages. You also have access to titles, descriptions etc.
// General (verbose) example - getting parent page ID
using (var connection = new DataConnection())
{
var currentPageNode = connection.SitemapNavigator.CurrentPageNode;
var parentPageNode = currentPageNode.ParentPage;
var parentPageId = parentPageNode.Id;
}
If you are doing this from a Razor Function there is already a data connection available (this.Data) so the above can be written like this:
// Razor example - getting parent page ID
var parentPageId = Data.SitemapNavigator.CurrentPageNode.ParentPage.Id;
One option is just to rely 100% on the standard asp.net way using the SiteMapProvider functionality.
Get a specific node like this: var node = SiteMap.CurrentProvider.FindSiteMapNodeByKey("...").
Get the Parent node like this: var parentNode = node.ParentNode.
Get its id like this: var id = parentNode.Key

Categories