Why this sitecore query is not returning any items despite the fact that item with specified field value exists? - sitecore

I'm trying to query one item's descendants using following query :
And not getting any items despite the fact that item with specified field value exists?
This query in query analyzer for some reason works:
select * from /sitecore/content/itema/News/descendant::*[#NewsId='235271']
Any help in correcting my code to get back item I'm searching for?
string sitecoreQuery = "descendant::*[#NewsId='" + item.Id.ToString() + "']";
Item[] newsItems = parent.Axes.SelectItems(sitecoreQuery);

Updated based on your comment:
The query looks ok, but you could also try using the shortcut for descendants: //*. I have just tested this locally and it returned me expected items.
string sitecoreQuery = "//*[#NewsId='" + item.Id.ToString() + "']";
Item[] newsItems = parent.Axes.SelectItems(sitecoreQuery);
Also double check that item.Id.ToString() is returning the Id you are expecting.
Hope that helps

You can try the following:
Item[] newsItems = Sitecore.Context.Database.SelectItems(string.Format("/sitecore/content/itema/News//*[#NewsId='{0}']", NewsId));
That should get you the filtered items

Related

Sitecore how to get a Droplist or Droplink selected item's both Id and Value?

var item = Sitecore.Context.Database.GetItem('xxx');
Now how can I get both its id and value? I've tried use item.Fields[""] , but only got an empty string, not sure why.
What Sitecore field type you are using "Droplink" or "DropList".
Please see if below solution helps:
How to access the selected item in a Droplist in Code Behind?

Oracle Apex: Auto select if shuttle list contains only a single item

I have a shuttle list that, depending on the selection of a previous form, may have between one and seven items. Is there any way for me to automatically move the entry to the right hand pane if there is only a single selection, just for the sake of long term optimization and user friendliness?
You can add a default Value to your shuttle item
SELECT YOUR_COLUMN_ID
FROM T0000_YOUR_TABLE
WHERE FILTER_COLUMN_ID = :P_FILTER_ITEM_PREV_FORM
GROUP BY YOUR_COLUMN_ID
HAVING COUNT(*) = 1
If there are more than 1 entries for your filter item the statement will return nothing, if there is exactly 1 entry the entry will be returned and set for your shuttle item
I like sim0n's answer. But here's another that uses JavaScript (you can choose which is best for your use case).
Create a Dynamic Action. Set Name to Page loaded and Event to Page Load.
Select the Action created by default. Set Action to Execute JavaScript Code, then enter the following code in Code.
var itemId = 'P1_ITEM_NAME';
var $opts = $('#' + itemId + ' select:eq(0) > option');
if ($opts.length === 1) {
$s(itemId, $opts.val());
}
Don't forget to change the name of the item to match the one on your page.
When the page loads, the JavaScript will check to see how many options are in the select element on the left. If there's just one, it will set the value of the item using the value to the value of the single option.

How to insert programmatically a value in a new row of a QtableView

I'm using a QtableView to display and edit data from a QsqlTableModel.
Everything's fine : data from a postgreSQL table is displayed, and user can edit it and save modifications.
I want to add a row when uses click on a button. I use the insertRecord method from my QslTableModel.
The row is correctly added in the QTableView.
My problem is :
I want to insert a value from a query in the first cell of this new row. (to automatically populate an unique identifier).
This is my code :
def ajoutlgn(self):
query_idNewLine = QtSql.QSqlQuery(self.db)
if query_idNewLine.exec_('SELECT sp_idsuivi+1 FROM bdsuivis.t_suivprev_tablo ORDER BY sp_idsuivi DESC LIMIT 1'):
while query_idNewLine.next():
identifiant = query_idNewLine.value(0)
#print identifiant
record = QtSql.QSqlRecord()
record.setValue(1,identifiant)
self.model.insertRecord(self.model.rowCount(), record)
The value is not inserted in the new row (but if I enter a value by hand, it works perfectly).
Nevertheless, the query is OK (as I can see with the « print identifiant » line, which returns the expected integer).
Do you see what I'm doing wrong ?
Are there other methods to insert programmatically a value in a QTableView cell?
Or do I have to use a QitemDelegate ?
Thanks for advance.
Finally I found the solution :
This line creates a record, but not a record for my QsqlTableModel
record = QtSql.QSqlRecord()
I replaced it with this one, and it works perfectly :
record = self.model.record()

CAML Query to get only non duplicates

My first goal is to get all items that has a status of 'Pending' in my sharepoint list. My code so far is like this :
using (var clientContext = new ClientContext(spSite.Trim()))
{
clientContext.Credentials = GetNetworkCredential();
var approvalLists = clientContext.Web.Lists.GetByTitle(approvalLibraryName);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='IsApproved'/><Value Type='Choice'>Pending</Value>" +
"</Eq>" +
"</Where>" +
"</Query>" +
"</View>";
ListItemCollection approvalListItem = approvalLists.GetItems(query);
clientContext.Load(approvalListItem);
clientContext.ExecuteQuery();
}
It is working but I then realize that a particular item can be inserted more than 1 in that list. So for example for item request_100, it can have one row for pending and another row for approved. So I need to only get those non duplicates items with status 'Pending'. Is it possible to have group by then fetch only those who have count = 1? Because I'm thinking if I could just load all items then manipulate it in using linq list. Or do you guys have another suggestion for this?
My first thought was indeed also using linq on the ItemCollection which should be working just fine.
But just a thought, if you select only the 'pending' rows, you won't get duplicates will you? Or can there be more than one item like 'request_100' with statusses 'Pending'?
Check this question which is alike to yours: https://sharepoint.stackexchange.com/questions/43262/how-to-use-groupby-in-caml-query

calculated fields in TestTrack, how to add the field to itself?

i need to to add a text calculated field in TestTrack, so the field always add itself and then anoter field.
so the field will always contain the previous entry and the new one.
how can i do it ?, without the testtrack claiming its a recursive formula
the only way to dit is to do a count of the event (adding data to the field)
and than run a for each of the ocurrnces with a concatenate of the data with the string itself (old value).
here is the example that works for me :
var TicketCount=Item.Events.count("update ticket");
var ticketsStr ='';
for(ticketIndex = 0;ticketIndex < TicketCount;ticketIndex++)
{
ticketsStr = ticketsStr + Item.Events.at(ticketIndex,"update ticket").fieldValue("Customer Name");
if(ticketIndex < TicketCount-1)
ticketsStr = ticketsStr + ",";
}
result = ticketsStr;
You are correct that a TestTrack calculated field cannot reference itself in the formula. Even if it could reference itself, consider the following formula for "update ticket":
Item.fieldValue("update ticket")+Item.fieldValue("Type")
In this scenario, the Type value would always be appended but there would be no check to see if the Type value is already in the list. Every time an item is edited the "update ticket" field value would be recalculated and the Type value would be appended again whether it has changed or not.
The solution proposed by Tal solves this problem by looping through the other fields and re-building the value. Additionally if a Customer Name value is modified or deleted, the field value will be correctly calculated.