How to know if a NodeFactory node is published - razor

#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#using umbraco.NodeFactory;
#using umbraco.MacroEngines;
<ul class="rslides">
#{
var rootNode = Model.Content.AncestorOrSelf(1);
var items = (rootNode.HasValue("featuredSlides")) ? (Umbraco.Core.Dynamics.DynamicXml)rootNode.GetPropertyValue("featuredSlides") : null;
}
#if (items != null)
{
foreach(var item in items)
{
int id = Convert.ToInt32(#item.InnerText);
Node node = new Node(id);
var mediaItem = Umbraco.TypedMedia(node.GetProperty("picture").ToString());
int mediaHeight = Convert.ToInt16(#mediaItem.GetPropertyValue("umbracoHeight"));
int maxHeight = 700;
int height = (mediaHeight > maxHeight) ? maxHeight : mediaHeight;
var url = #node.Url;
<li><img src="/imagegen.ashx?image=#mediaItem.GetPropertyValue("umbracoFile")&width=640"></li>
}
}
</ul>
I would like to know what is the published status of #node.
I'm working with umbraco 6.1.6
I already tried something like var p = #node.GetProperty("published") (publishedCount, publishedStatus ...) but nothing worked.
Any idea?
Thanks.

(Dynamic)Node gives access to (only) published content from the XML cache. Node has properties CreateDate and UpdateDate.

Related

Fetch images tags from a specific webpage Div

I am trying to fetch images tags from a specific div in a web page. Here is the web page link page link
I have used this code:
var webGet = new HtmlWeb();
var document = webGet.Load(txt.Text);
var infos = from info in document.DocumentNode.SelectNodes("//div[#id='custom-description']")
from link in info.SelectNodes("img").Where(x => x.Attributes.Contains("src"))
select new
{
LinkURL = link.Attributes["src"].Value
};
lbl.Text = infos.ToString();
but it returns null value.
Please tell me whats wrong in this code.
Thanks in advance
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument document = web.Load(url);
var rateNode = from info in document.DocumentNode.SelectNodes("//div[#class='class name']")
from link in info.SelectNodes("//img").Where(x=>x.Attributes.Contains("src"))
select new
{
link.Attributes["src"].Value
};
// return View(lstRecords);
string result;
lbl.Text = rateNode.ToString();
foreach (var a in rateNode)
{
int count=0;
Image img = new Image();
img.ID = count + "a";
count++;
img.ImageUrl = a.Value
Controls.Add(img);
}
The Linq query you have written here is looking ok. But the problem here is div with id custom-descriptiondoes not contain img element. Hence query returns null result.

max lengt or else dots - How or what should i write?

I want the script to show max 26 letters and if there is more I want it to make (...) <-- so that you can se there is more letters in the link.
First I put a bit of a script I have for another site containing a variable to do that, however it doesn't work in RSS:
{
temp.Add(titel);
count++;
string titel_kort = titel;
if (titel.Length > 26)
{
titel_kort = titel.Substring(0, 26) + "...";
}
}
And this is the script I want to integrate to:
#using System.Xml.XPath;
#using System.Xml;
#{
try
{
XmlTextReader udBrudRSS = new XmlTextReader("http://tidende.dk/rss.aspx");
XmlDocument doc = new XmlDocument();
doc.Load(udBrudRSS);
XmlNodeList rssItems = doc.SelectNodes("//item");
var count = 0;
foreach (XmlNode node in rssItems )
{
count++;
if (count > 3) { break; }
<div class="nyhedlink">- #node["title"].InnerText</div>
}
}
catch {}
}
You could something like this :
using (var webclient = new WebClient())
{
var data = webclient.DownloadData("http://tidende.dk/rss.aspx");
var oReader = new XmlTextReader(new MemoryStream(data));
var xml = XDocument.Load(oReader);
var values = xml.XPathSelectElements("//item").Take(3).Select(p => new
{
Link = p.XPathSelectElement("//link").Value,
Title = (p.XPathSelectElement("./title").Value.Length > 26) ?
p.XPathSelectElement("./title").Value.Substring(0, 26).Trim() + "..." :
p.XPathSelectElement("./title").Value.Trim()
});
foreach (var item in values)
{
<div class="nyhedlink">- #item.Title</div>
}
}
Sometimes is better use WebClient to make the petition instead of XmlTextReader see this question for a good explanation.

RazorScript Count Not Working

I am using DNN and have (with the help of a co-worker) created a script to print out items from the Form and List module, but need to limit the output to three. However, the script returns nothing. Can anyone point to me where I may have gone wrong?
http://jsfiddle.net/VsF6c/
#using System.Data;
#using DotNetNuke.Entities.Modules;
#using DotNetNuke.Entities.Portals;
#using DotNetNuke.Modules.UserDefinedTable;
#using DotNetNuke.Entities.Users;
#{
var mc = new ModuleController();
var tc = new DotNetNuke.Entities.Tabs.TabController();
int portalId = PortalController.GetCurrentPortalSettings().PortalId;
var flModule = mc.GetModuleByDefinition(portalId, "Form and List");
int moduleId = -1;
int tabId = -1;
var tab = tc.GetTabByName("News", portalId);
if (tab != null) {
tabId = tab.TabID;
}
if (flModule != null) {
moduleId = 968;
}
var ds = (new UserDefinedTableController(moduleId, tabId, new UserInfo())).GetDataSet(true);
}
<ul>
#for (int i = 0; i < 3; i++)
{
DataRow row = ds.Tables["Data"].Rows[i];
<li>
<a href='/news##row["UserDefinedRowId"]' title='#row["Title"]'>#row["Title"]</a>
</li>
}
</ul>

Razor CSHTML IF statement

Anyone here that can help me? I have the following code:
#inherits umbraco.MacroEngines.DynamicNodeContext
#{
var node = #Model.NodeById(1257);
}
<div class="Top10">
<h1>Newest</h1>
#foreach (var article in node.Descendants().Where("Visible && (NodeTypeAlias = \"Article\" || NodeTypeAlias = \"sergrein\" || NodeTypeAlias = \"solomyndagrein\")").OrderBy("createDate desc").Take(10))
{
<h2>#article.createDate.ToString("dd/MM") | #article.title</h2>
}
</div>
What I want is: if #article.title is longer than e.g. 10 characters, it needs to return the 10 characters followed by ... (for example: "this_is_a_..."). If the #article.title is shorter than 10 characters, it can just show the full title length.
How can this truncating be done?
Try this
#(article.title.Length > 10 ? (article.title.Substring(0,10) + " ...") : article.title)
Normally I'd say do this in your Model, but it looks like you're using Umbraco's model.
So you could just do:
#inherits umbraco.MacroEngines.DynamicNodeContext
#{
var node = #Model.NodeById(1257);
}
<div class="Top10">
<h1>Newest</h1>
#foreach (var article in node.Descendants().Where("Visible && (NodeTypeAlias = \"Article\" || NodeTypeAlias = \"sergrein\" || NodeTypeAlias = \"solomyndagrein\")").OrderBy("createDate desc").Take(10))
{
{
var title = article.title;
if (title.Length > 10)
title = title.Substring(0,10) + "...";
}
<h2>#article.createDate.ToString("dd/MMM") | #title</h2>
}
</div>
this shall help,
#{
if(article.title.ToString().Length > 10)
{
article.title = article.title.Substring(0,10) + " ..."; //the format you desire
}
else
{
article.title; // default
}
}

insert selected value from drop down by razor mvc

hi, I had aproject and one of my task as to insert selected value from
drop down to DB field by razor mvc. I did my code but no values inserted ,Also the DDL have items from DB well . my project with razor mvc4.
public ActionResult Create()
{
var data = db.Categories.ToList().Distinct();
foreach (var t in data)
{
s.Text = t.Name;
s.Value = t.Cat_ID.ToString();
items.Add(s);
}
ViewBag.Parent = items;
return View();
}
[HttpPost]
public ActionResult Create(Category category, IEnumerable<HttpPostedFileBase> files)
{
if (Request.Files.Count > 0)
{
var uploadedFile = Request.Files[0];
var fileSavePath = "";
var fileName = "";
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/Uploads/" + fileName);
uploadedFile.SaveAs(fileSavePath);
category.Path = "~/App_Data/Uploads/" + fileName;
}
var data = db.Categories.ToList().Distinct();
List<SelectListItem> items = new List<SelectListItem>();
foreach (var t in data)
{
SelectListItem s = new SelectListItem();
s.Text = t.Name;
s.Value = t.Cat_ID.ToString();
items.Add(s);
if (s.Selected)
{ category.Parent_ID = int.Parse(s.Value); }
}
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
Hi Your code should be something like this. I am assuming that you have model with certain structure. If belo code does not give you a clue then please. Tell how your view and model is and what is your requirement.
Hope this helps.
[HttpPost]
public ActionResult Create(Category category, IEnumerable<HttpPostedFileBase> files)
{
if (Request.Files.Count > 0)
{
var uploadedFile = Request.Files[0];
var fileSavePath = "";
var fileName = "";
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/Uploads/" + fileName);
uploadedFile.SaveAs(fileSavePath);
category.Path = "~/App_Data/Uploads/" + fileName;
}
var data = db.Categories.ToList().Distinct();
//I assume that you need to find the category from db.Caegories which user has selected on the UI and submited the form.
//I assume the parameter category is the one which you want to find from DB.
//category is your model has Parent_Id property which is bound to UI control (ie. dropdown)
var categoryToSave = (from c in data
where c.Cat_ID == category.Parent_ID
select c).firstOrDefault();
if(categoryToSave!=null)
{
//I believe here you want to save this category to some other table.
//Now you have got the category tht user has selected on UI.
//Write the further logic here.....
db.SaveChanges();
}
return RedirectToAction("Index");
}
Regards, Mahesh
you are creating the fresh selectListItem s here in the same loop. You will get seleced == true for any of the item.
May be item, that user has selected on UI (before post) exist in category param if it is your model that is bound to desired dropdown.
I suspect it is a logical error.
Regards, Mahesh