Sharepoint XML query from array - c#

I have a list of x people I am searching for in a SharePoint 2010 list. How do I build the or statement in XML similar to the if statement
if( person1 || person2 || person3 || ... personx)
Since every <Or> has to have exactly 2 children, no more, no less, I have this so far:
query.InnerXml = "<Where>" +
"<Or>" +
"<Or>" +
"<Contains>" +
"<FieldRef Name=\"Member\" />" +
"<Value Type=\"Text\">" +
"Person1" +
"</Value>" +
"</Contains>" +
"<Contains>" +
"<FieldRef Name=\"Member\" />" +
"<Value Type=\"Text\">" +
"Person2" +
"</Value>" +
"</Contains>" +
"</Or>" +
"<Contains>" +
"<FieldRef Name=\"Member\" />" +
"<Value Type=\"Text\">" +
"Person3" +
"</Value>" +
"</Contains>" +
"</Or></Where>";
But I cant figure out a good way to make all the other or statements while looping through my list of x people

SharePoint 2010 added an In clause, which simplifies this.
Here's a helper method to put all of the people into Value wrappers:
public static string ValuesAsCAML(IEnumerable<string> values, string type)
{
StringBuilder output = new StringBuilder();
foreach (string value in values)
{
output.AppendFormat("<Value Type=`{0}`>{1}</Value>", type, value);
}
return output.ToString();
}
With that you can now write:
var people = new[] { "Person1", "Person2", "Person3" };
string values = ValuesAsCAML(people, "Text");
var query = string.Format(
#"<Where>
<In>
<FieldRef Name='Member` />
<Values>
{0}
</Values>
</In>
</Where>"
, values);

Related

Results in descending order

I've got a block of code which sums up time togged for various tasks in a project and returns the total hours logged per project (intMinutesLogged). How do I get my results n descending order?
static async void NotifyEntriesByWorkSpace(Dictionary<string, List<TimeEntry>> dicEntriesByWorkspace, string strChatURL)
{
string strMessage = "";
foreach (var kvpEntry in dicEntriesByWorkspace)
{
var lstTimeEntries = kvpEntry.Value;
string strTitle = "";
var intMinutesLogged = 0;
var intMinutesBillable = 0;
var intMinutesNonBillable = 0;
foreach (var objTimeEntry in lstTimeEntries)
{
if (objTimeEntry.Billable)
{
intMinutesBillable += objTimeEntry.TimeInMinutes;
}
else
{
intMinutesNonBillable += objTimeEntry.TimeInMinutes;
}
}
strTitle = Workspaces.getWorkspaceFromCache(kvpEntry.Key).Title;
//Console.WriteLine(intMinutesLogged + ": " + strTitle + "m");
intMinutesLogged = intMinutesBillable + intMinutesNonBillable;
Console.WriteLine(TimeLoggedMessage(intMinutesLogged) + ": " + strTitle + " " + "(Billable: " + TimeLoggedMessage(intMinutesBillable) + ";" + " " + "Non-Billable: " + TimeLoggedMessage(intMinutesNonBillable) + ")");
strMessage += TimeLoggedMessage(intMinutesLogged) + ": " + strTitle + " " + "(Billable: " + TimeLoggedMessage(intMinutesBillable) + ";" + " " + "Non-Billable: " + TimeLoggedMessage(intMinutesNonBillable) + ")" + "\n";
}
await SendMessage(strChatURL, strMessage);
}
static string TimeLoggedMessage(int intMinutesLogged)
{
return intMinutesLogged / 60 + "h" + " " + intMinutesLogged % 60 + "m";
}
You could use LINQ for this: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderbydescending?view=net-6.0
You could create a simple class or anonymous type to hold the integer values you're summing up (total minutes, billable minutes, non-billable minutes). Then you could populate a collection of this type within the code you shared and afterwards call OrderByDescending on it. You could order based on any of the three integer values.

Linq to xml foreach not working

Hi i have problems getting my foreach to work. It only picks one node, in my simpel linq query. i actly dont no what the problem is, because i usede to be using Xmldocument and xmlnodelist. But i really want to learn do it using Linq, i cannot find anything on google why its not working, i allso tryede the expmales on these links.
http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples
Simple LINQ to XML is not working
https://forums.asp.net/t/1378244.aspx?Linq+to+XML+query+how+to+get+all+elements+but+excluding+some+
http://www.c-sharpcorner.com/UploadFile/de41d6/learning-linq-made-easy-linq-to-xml-tutorial-3/
Both these examples only return, one xml node.
XElement ele = XElement.Load(filePath);
String aXmlString = ele.ToString(SaveOptions.DisableFormatting) + ele.Value;
foreach (XElement xNode in ele.Descendants("lakeringsdel"))
{
//litTest.Text = xNode.Element("lakeringsMetode").Value;
strData = "<table style='width:100%;' >"
+ "<tr>"
+ "<th>Nr.</th>"
+ "<th>Arbejdsbeskrivelse/Omfang</th>"
+ "<th>Metode</th>"
+ "<th>Ae</th>"
+ "</tr>"
+ "<tr>"
+ "<td>" + xNode.Element("ledenr").Value + "</td>"
+ "<td>" + xNode.Element("lakeringsDel").Value + "</td> "
+ "<td>" + xNode.Element("lakeringsMetode").Value + "</td> "
+ "<td>" + xNode.Element("arbejdsEnheder").Value + "</td> "
+ "</tr>"
+
"</table>";
}
next example
var test = "";
var q = from c in ele.Descendants("lakeringsdel")
select c.Element("lakeringsDel").Value;
foreach (string item in q)
{
test = item;
}
My xml document
<lakRapportDetaljer>
<aePrTime>10</aePrTime>
<fabrikatModelTekst>FORD FOCUS (CEW)</fabrikatModelTekst>
<kilometerStand>28205</kilometerStand>
<lakArbejde>2745.0</lakArbejde>
<lakIaltTotal>3610.05</lakIaltTotal>
<lakIndex>134</lakIndex>
<lakMaterialer>865.05</lakMaterialer>
<lakTimepris>450.0</lakTimepris>
<lakeringsMetode>2-LAGS METALLIC</lakeringsMetode>
<lakeringsMetode>DØRGREB LEVERES LAKE</lakeringsMetode>
<lakeringsdel>
<arbejdsEnheder>10.0</arbejdsEnheder>
<lakeringsDel>KOFANGER H B</lakeringsDel>
<lakeringsMetode>REPARATION LAK.PLAST</lakeringsMetode>
<ledenr>2584</ledenr>
</lakeringsdel>
<lakeringsdel>
<arbejdsEnheder>15.0</arbejdsEnheder>
<lakeringsDel>BAGSKÆRM HØJRE</lakeringsDel>
<lakeringsMetode>REP.LAK. &lt;50%, METAL</lakeringsMetode>
<ledenr>3482</ledenr>
</lakeringsdel>
<lakeringsdel>
<arbejdsEnheder>5.0</arbejdsEnheder>
<lakeringsDel>SPECIALAFDÆKNING</lakeringsDel>
<lakeringsMetode>OVERFLADE LAKERING</lakeringsMetode>
<ledenr>1000</ledenr>
</lakeringsdel>
<miljoeLakMaterialerProcent>6</miljoeLakMaterialerProcent>
</lakRapportDetaljer>
I've tested your code,but I can't see any problem: it returns three nodes:
if you want to select all elements inside the 3 Xelement nodes "lakeringsdel", you could do :
var q = from c in ele.Descendants("lakeringsdel")
select c;
foreach (XElement item in q)
{
test = item.Value;
}
which gives :
or directly:
var q = ele.Descendants("lakeringsdel");
foreach (XElement item in q)
{
test = item.Value;
}
which gives the same result
or in 1 line:
ele.Descendants("lakeringsdel").ToList().ForEach(elem=>test =elem.Value);
but as said Reniuz in comment : you store only the last result in your "test" variable
if you want to store all results you could do:
List<string> test = new List<string>();
var q = from c in ele.Descendants("lakeringsdel")
select c.Element("lakeringsDel").Value;
foreach (string item in q)
{
test.Add(item);
}
or in 1 line:
test.AddRange(q);
In both examples you set the output strData and test every time you loop through your items. That results in handling nly the last item in your list.
Assuming you want to show a table with the results on your homepage you should change your first example to this
XElement ele = XElement.Load(filePath);
String aXmlString = ele.ToString(SaveOptions.DisableFormatting) + ele.Value;
strData = "<table style='width:100%;' >"
+ "<tr>"
+ "<th>Nr.</th>"
+ "<th>Arbejdsbeskrivelse/Omfang</th>"
+ "<th>Metode</th>"
+ "<th>Ae</th>"
+ "</tr>"
foreach (XElement xNode in ele.Descendants("lakeringsdel"))
{
//litTest.Text = xNode.Element("lakeringsMetode").Value;
strData += "<tr>"
+ "<td>" + xNode.Element("ledenr").Value + "</td>"
+ "<td>" + xNode.Element("lakeringsDel").Value + "</td> "
+ "<td>" + xNode.Element("lakeringsMetode").Value + "</td> "
+ "<td>" + xNode.Element("arbejdsEnheder").Value + "</td> "
+ "</tr>";
}
strData += "</table>";
I would recommend using a StringBuilder to build the string instead of +; whcih could look like this example.
StringBuilder strData = new StringBuilder("<table style='width:100%;'>");
strData.AppendLine("<tr>");
strData.AppendLine("<th>Nr.</th>");

C# removing duplicate HTML rows

I'm generating a HTML file using C#. Using objects and their properties, I'm adding rows to an HTML table . However, I'm seeing some duplicate rows which I don't want, even though the column values are correct. After doing some googling, I found a few HTML functions that people posted for accomplishing this, such as:
function removeDuplicateRows($table){
function getVisibleRowText($row){
return $row.find('td:visible').text().toLowerCase();
}
$table.find('tr').each(function(index, row){
var $row = $(row);
$row.nextAll('tr').each(function(index, next){
var $next = $(next);
if(getVisibleRowText($next) == getVisibleRowText($row))
$next.remove();
})
});
}
and
var seen = {};
$('table tr').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
However, I'm not sure how to utilize those in my c# code (possibly from my lack of HTML coding). When I tried using these and wrap each line with a sw.WriteLine, all I get is text of the function. So my question is, how can I remove duplicate HTML table rows in my code?
What I have:
StreamWriter sw = new StreamWriter(Path.Combine(cdsPublishRoot, "GeneralActive.htm"));
sw.WriteLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \" http://www.w3.org/TR/html4/strict.dtd \">");
sw.WriteLine("<html>");
sw.WriteLine("<head>");
sw.WriteLine("<meta http-equiv=\"Expires\" content=\"0\">");
sw.WriteLine("<title>GPCSE-CDS General</title>");
sw.WriteLine("</head>");
sw.WriteLine("<body onmousemove=\"MouseMove(event)\">");
sw.WriteLine("<script type=\"text/javascript\">");
sw.WriteLine("DoMainMenu(false,\"SDS\");");
sw.WriteLine("DoSdsSubMenu(true,\"ARCHIVE\");");
sw.WriteLine("</script>");
sw.WriteLine("<p class=Heading2><br>Active PCM General Requirement Specifications<br></p>");
sw.WriteLine("<table cellpadding=5 cellspacing=0 border=1 width=760 id=\"MainDataTable\">");
sw.WriteLine("<tr><th style=\"width:14%\" align=left class=SdsHeader>Spec.</th>");
sw.WriteLine("<th style=\"width:64%\" align=left class=SdsHeader>Specification Name</th>");
sw.WriteLine("<th style=\"width:7%\" align=left class=SdsHeader>Spec. Ver.</th>");
sw.WriteLine("<th style=\"width:7%\" align=left class=SdsHeader>Rev. Level</th>");
sw.WriteLine("<th style=\"width:8%\" align=left class=SdsHeader>Rev. Date</th></tr>");
foreach (string file in genActive.Distinct())
{
string[] genOldDirectories = file.Split(Path.DirectorySeparatorChar);
string path = Path.GetDirectoryName(file);
string[] files = Directory.GetFiles(path);
var acro = ArdApp.Data.GeneralSpecification.GeneralSpecVersion.DataLoad(genOldDirectories[2].ToString(), Convert.ToInt32(genOldDirectories[3][0].ToString()), Convert.ToInt32(genOldDirectories[3][2].ToString() + genOldDirectories[3][3].ToString()));
sw.WriteLine("<tr><td id=\"idTDMenuTitle0008\">");
sw.WriteLine("<div id=\"idMenuTitle0008\" class=\"clsMenuTitle\" onclick=\"DoMenu(this)\">");
sw.WriteLine(genOldDirectories[2] + "</div>");
sw.WriteLine("<div id=\"idMenu0008\" class=\"clsMenu\">");
sw.WriteLine("<ul class=\"clsMenuBullets\">");
foreach (var fileName in files)
{
string[] fileDirectory = fileName.Split(Path.DirectorySeparatorChar);
sw.WriteLine("<li class=\"clsMenuItem\"><a class=\"clsMenuItem\" href=" + StripToWebFriendlyName(file) + ">" + StripToWebFriendlyName(fileDirectory[5]) + "</a></li>");
}
sw.WriteLine("</ul>");
sw.WriteLine("</div></td>");
sw.WriteLine("<td class=BodyText>" + acro.AcronymName + "</td>");
sw.WriteLine("<td class=BodyText>" + Convert.ToInt32(genOldDirectories[3][0].ToString()) + "</td>");
sw.WriteLine("<td class=BodyText>" + genOldDirectories[3][2].ToString() + genOldDirectories[3][3] + "</td>");
sw.WriteLine("<td class=BodyText>" + acro.RevisionDate + "</td>");
sw.WriteLine("</tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</Body>");
sw.WriteLine("</HTML>");
sw.Close();
}
What I tried:
....
foreach (var fileName in files)
{
string[] fileDirectory = fileName.Split(Path.DirectorySeparatorChar);
sw.WriteLine("<li class=\"clsMenuItem\"><a class=\"clsMenuItem\" href=" + StripToWebFriendlyName(file) + ">" + StripToWebFriendlyName(fileDirectory[5]) + "</a></li>");
}
sw.WriteLine("</ul>");
sw.WriteLine("</div></td>");
sw.WriteLine("<td class=BodyText>" + acro.AcronymName + "</td>");
sw.WriteLine("<td class=BodyText>" + Convert.ToInt32(genOldDirectories[3][0].ToString()) + "</td>");
sw.WriteLine("<td class=BodyText>" + genOldDirectories[3][2].ToString() + genOldDirectories[3][3] + "</td>");
sw.WriteLine("<td class=BodyText>" + acro.RevisionDate + "</td>");
sw.WriteLine("</tr>");
}
sw.WriteLine("var seen = {};");
sw.WriteLine("$('table tr').each(function() {");
sw.WriteLine("var txt = $(this).text();");
sw.WriteLine("if (seen[txt])");
sw.WriteLine(" $(this).remove();");
sw.WriteLine("else");
sw.WriteLine("seen[txt] = true;");
sw.WriteLine("});");
Looks like your just reading in file info and just want to display distinct files in your HTML display?
If this is still a c# question, consider just reading in the file info (full file path info) into a List<string> and then you could use linq "Distinct" to get your non-duplicated items and then output in html as needed.

Why 'innerhtml' does not work properly for 'select' tag

I am trying to set the innerhtml of an html select tag but I cannot set this feature;therefor,I need to use the outerhtml feature.This way,not only is my code HARDCODE ,but also it is preposterous.I have already read 'InnerHTML IE 8 doesn't work properly? Resetting form',it did not help though.
I would really appreciate it if you tell me how to set the innerhtml feature of an html select tag.
My C# code:
public void SetDefaultValue(string ControlID, string ControlValue)
{
System.Windows.Forms.HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl = doc.GetElementById(ControlID);
string ListResult;
string ListInnerHTML = "";
ListInnerHTML += "<OPTION value = " + LstString + ">" + LstString + "</OPTION>";
ListResult = "<SELECT id = " + '"' + HTMLControl.Id + '"' + " type = " + '"' + HTMLControl.GetAttribute("type") + '"' + " title = " + '"' +
HTMLControl.GetAttribute("title") + '"' + " name = " + '"' + HTMLControl.Name + '"' + " value = " + '"' + HTMLControl.GetAttribute("value") +
'"' + " size = \"" + HTMLControl.GetAttribute("size") + '"' + HTMLControl.GetAttribute("multiple").ToString() + "\">" + ListInnerHTML + "</SELECT>";
HTMLControl.OuterHtml = ListResult;
}
or
string _lsthtml = _htmlel.OuterHtml;
string[] _parts = ControlValue.Split(new char[] { ',' });
string _lstinner = "";
foreach (string _lst in _parts)
_lstinner += "<option value=" + _lst + ">" + _lst + "</option>";
_lsthtml = _lsthtml.Insert(_lsthtml.IndexOf(">") + 1, _lstinner);
_htmlel.OuterHtml = _lsthtml;
This code works but I need something efficient and clean.
The ReturnControlType function returns the type of an html tag.
This is an official Internet Explorer bug:
BUG: Internet Explorer Fails to Set the innerHTML Property of the Select Object.
One workaround
You may try adding one of the following meta tags in your document's head:
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
or
<meta http-equiv="X-UA-Compatible" content="IE=10" />
You should also properly format the option tag's value attribute (enclose LstString in '):
ListInnerHTML += "<OPTION value='" + LstString + "'>" + LstString + "</OPTION>";
A more reliable solution
As the fixes above might be a workaround for your code, I would suggest to use a more reliable approach. Consider adding a reference to Microsoft.mshtml to your project and modifying your method like this:
// add this to the top of the file containing your class
using mshtml;
public void SetDefaultValue(string ControlID, string ControlValue)
{
System.Windows.Forms.HtmlDocument doc = webBrowser1.Document;
IHTMLDocument2 document = doc.DomDocument as IHTMLDocument2;
var sel = doc.GetElementById(ControlID);
HTMLSelectElement domSelect = (HTMLSelectElement)sel.DomElement;
domSelect.options.length = 0;
HTMLOptionElement option;
// here you can dynamically add the options to the select element
for (int i = 0; i < 10; i++)
{
option = (HTMLOptionElement)document.createElement("option");
option.text = String.Format("text{0}", i);
option.value = String.Format("value{0}", i);
domSelect.options.add(option, 0);
}
}
I really don't know why innerHTML is not working for you.
If it just dosen't you could try an alternative:
http://innerdom.sourceforge.net/
demo
In this thread it is sugested you use the items collection of a control
How to add items to dynamically created select (html) Control
refer to this page for a complete example:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlselect.items.aspx

JavaScript Google map marker bug

I'm currently working on an ASP.NET project where I'm using the Google Maps API to show a marker for every company that's registrated in the database.
Everything works just fine, but when I click on a marker the tooltip/dialogbox for the last company in my company list always shows up and not the actualy company mark that's been clicked on.
I can't really get my head around why it is always the last marker that shows up. Here's my updated code:
JavaScript.Text = #"<script type='text/javascript'>
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map'));
map.setCenter(new GLatLng(56.4, 10.57983), 9);
map.enableScrollWheelZoom();
}
}
</script> ";
foreach (MemberProfile m in relatedMembers)
{
XmlDocument doc = new XmlDocument();
string address = m.Address;
string zip = m.Zip;
string city = m.City;
string navn = m.Name;
string tlf = m.Phone;
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?address=" + zip + "+" + city + "+" + address + "+DK&sensor=true&key=ABQIAAAAEaY4JLb9fZFGMlDKuMUlWBRSvyGIkBO7X03pzlT7Z30EPXHR8BS0rXL_ShFm2gc79lZTw2Zak88wng");
XmlNode latNode = doc.SelectSingleNode("GeocodeResponse/result/geometry/location/lat/text()");
XmlNode lonNode = doc.SelectSingleNode("GeocodeResponse/result/geometry/location/lng/text()");
if (latNode != null && lonNode != null)
{
JSAddMarkers.Text += #"<script type='text/javascript'>
var marker = new GMarker(new GLatLng(" + latNode.Value + "," + lonNode.Value + ")); "
+ "var html = '<b>" + navn + "</b><br />" + address + "<br /> " + zip + " " + city + "<br />" + tlf + "'; " + "GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); });"
+ "map.addOverlay(marker);"
+ "</script>";
}
If any of you out there can spot the reason why, I would be happy to hear from you! Any help/hint is appreciated :-)
All the best,
Bo
try this
var point =new GLatLng(" + latNode.Value + "," + lonNode.Value + ");
var marker = createMarker(point, address,zip,city,navn);
map.addOverlay(marker);
function createMarker(point, address, zip,city, navn) {
var marker = new GMarker(point, customIcons[type]);
var html = "Address:<b style='padding-left:6px'>" + address+ "</b><br/>zip:<b style='padding-left:6px'>"+ zip+ "</b><br/>city:<b style='padding-left:6px'>"+ city+ "</b>";
GEvent.addListener(marker, 'mouseover', function() {
marker.openInfoWindowHtml(html);
});
GEvent.addListener(marker, "mouseout", function() {
marker.closeInfoWindow();
});
return marker;
}

Categories