Simple SPQuery issue - sharepoint-2010

I cant get this simple SPQuery working. It does not return any items. Although my document library has 5 items with Modified date as 22/08/2013 12:46
Please help me, where I am getting it wrong ?
string SPquery = #"<Where><Geq><FieldRef Name='Modified'/><Value Type='DateTime'>22/08/2013</Value></Geq></Where>";
SPQuery oQuery = new SPQuery();
oQuery.ViewAttributes = "Scope=\"Recursive\"";
oQuery.Query = SPquery;
var query = documentLibraryAsList.GetItems(oQuery);
foreach (SPListItem spListIem in query)
{
string s = "Sure";
}

Seems you're using a wrong DateTime format, it should be yyyy-MM-dd. Try that
string SPquery = #"<Where><Geq><FieldRef Name='Modified'/><Value Type='DateTime'>2013-08-22</Value></Geq></Where>";

Related

How to not add to the list, when the return of a LINQ IEnumerable is empty?

I do have the following VB.NET code:
Dim list = directoryQuery.Select(
Function(d) New With {
.dir = d.FullName,
.acl = GetFileSystemAccessRule(d).Select(
Function(a) New With {.if = a.Reference.ToString()}
)
}
)
End Sub
Sometimes the return of GetFileSystemAccessRule(d).Select is Return Enumerable.Empty(Of FileSystemAccessRule)(). In that case, I would like to neither add .directory nor .acl to that list. I want to skip it.
So I tought about the options to remove afterwards the empty items.
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Enumerable.Empty(list)).ToList()
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Nothing).ToList()
But unfortunately all of them failed. What do I wrong?
I think this would be the way to go:
list = list.Where(Function(a) a.acl.Any())
or something closely resembling that (I'm not very well versed in VB.NET syntax).

Creating a MoreLikeThis query yields empty queries and terms

I'm trying to get similar Document of another one . I'm using the Lucene.Net MoreLikeThis-Class to achieve this. For this i seperate my Documents in multiple Fields - Title and Content. Now creating the actual query results in an empty query without interesting Terms.
My could looks like this:
var queries = new List<Query>();
foreach(var docField in docFields)
var similarSearch = new MoreLikeThis(indexReader);
similarSearch.SetFieldNames(docField.fieldName);
similarSearch.Analyzer = new GermanAnalyzer(Version.LUCENE_30, new HashSet<string>(StopWords));
similarSearch.MinDocFreq = 1;
similarSearch.MinTermFreq = 1;
similarSearch.MinWordLen = 1;
similarSearch.Boost = true;
similarSearch.BoostFactor = boostFactor;
using(var reader = new StringReader(docField.Content)){
var searchQuery = similarSearch.Like(reader);
// debugging purpose
var queryString = searchQuery.ToString(); // empty
var terms = similarSearch.RetrieveInterestingTerms(reader); // also empty
queries.Add(searchQuery);
}
var booleanQuery = new BooleanQuery();
foreach(var moreLikeThisQuery in queries)
{
booleanQuery.Add(moreLikeThisQuery, Occur.SHOULD);
}
var topDocs = indexSearcher.Search(booleanQuery, maxNumberOfResults); // and of course no results obtained
So the question is:
Why there are no Terms / why there is no query generated?
I hope important thing's be seen, if not please help me to make my first question better :)
I got it to work.
The problem was, that i worked on the false directory.
I have different Solutions for creating the index and creating the queries and had a missmatch with the index-location.
So the generall solution would be:
Is your Querygenerating-Class fully initialized? (MinDocFreq, MinTermFreq, MinWordLen, has a Analyzer, set the fieldNames)
Is your used IndexReader correctly initialized?

Convert a Linq result to a datatable

I am trying to convert a Linq result in to a datatable
I have a linq that is created from a dataset of many tables. It returns results, but I need to get the results in to a new datatable.
Examples I have seen say I sould be able to use .CopyToDataTable But for some reason this doesn't work?
I have noticed that I can to .ToArray perhaps I can then turn the array in to a datatable? Seems line an unnecessary step?
Here is my query: (it works)
Dim R2 = From Inq In DS.Tables!CNLocalInquiry.AsEnumerable()
Join Cust In DS.Tables!CustomerID.AsEnumerable() On Inq.Field(Of Integer)("CNLocalInquiry_Id") Equals Cust.Field(Of Integer)("CNLocalInquiry_Id")
Select New With {.date = Inq.Field(Of String)("date"),
.CName = Cust.Field(Of String)("CustomerNumber"),
.Name = Cust.Field(Of String)("name")}
Dim MemberInq as new datatable
MemberInq = R2.CopyToDataTable() <-- this doesn't work
This is what my query returns:
(this is the easy to code way... this will not be performant for large datasets)
public static class ToolsEx
{
public static DataTable ToDataTable<T>(this IEnumerable<T> items)
{
var t = typeof(T);
var dt = new DataTable(t.Name);
var props = t.GetProperties()
.Select(p => new { N = p.Name, Getter = p.GetGetMethod() })
.Where(p => p.Getter != null)
.ToList();
props.ForEach(p => dt.Columns.Add(p.N));
foreach (var item in items)
dt.Rows.Add(props.Select(p => p.Getter.Invoke(item, null)).ToArray());
return dt;
}
}
I've saved this as an extension method and it's always worked perfectly:
https://msdn.microsoft.com/en-us/library/bb669096.aspx
Examples here:
https://msdn.microsoft.com/en-us/library/bb386921.aspx
Hope that does the trick!

How can I update document without losing fields?

CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "id1");
doc1.addField("name", "doc1");
doc1.addField("price", new Float(10));
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "id1");
doc2.addField("name", "doc2");
server.add(doc1);
server.add(doc2);
server.commit();
SolrQuery query = new SolrQuery();
query.setQuery("id:id1");
query.addSortField("price", SolrQuery.ORDER.desc);
QueryResponse rsp = server.query(query);
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while(iter.hasNext()){
SolrDocument doc = iter.next();
Collection fieldNames = doc.getFieldNames();
Iterator<String> fieldIter = fieldNames.iterator();
StringBuffer content = new StringBuffer("");
while(fieldIter.hasNext()){
String field = fieldIter.next();
content.append(field+":"+doc.get(field)).append(" ");
//System.out.println(field);
}
System.out.println(content);
}
The question is that I want to get the result "id:id1 name:doc2 price:10.0", but the output is "id:id1 name:doc2"...
So I want to know if I want to get the result as "id:id1 name:doc2 price:10.0", how can I modify my programming?
As you are adding the documents with same id. You are basically adding a same document twice.
Solr will update/overwrite the document. updated is basically delete and add.
As the second document you added with the same id does not have the price field, it won't be added and you wont find it the index.
you would need to have all the fields changed and unchanged when you are adding back the document.
doc2.addField("price", new Float(10)); // should add it back to the document

SP2010 FullTetSqlQuery no results, worked on SP2007

This code worked in our WCF service with SP 2007. In our new server on SP2010 this doesn't work. I do not know if the code needs to change or if there's something different about the configuration
Is this code wrong? Are there other configuration things I need to do? Search service is started.
StringBuilder queryText = new StringBuilder();
queryText.Append("SELECT PreferredName ");
queryText.Append("FROM SCOPE() ");
queryText.Append("WHERE \"scope\" = 'People' ");
Microsoft.Office.Server.Search.Query.FullTextSqlQuery query = new Microsoft.Office.Server.Search.Query.FullTextSqlQuery(SPContext.Current.Site);
query.QueryText = queryText.ToString();
query.ResultTypes = Microsoft.Office.Server.Search.Query.ResultType.RelevantResults;
query.RowLimit = 50;
Microsoft.Office.Server.Search.Query.ResultTableCollection results = query.Execute();
if ((int)ResultType.RelevantResults != 0 && results.Exists(ResultType.RelevantResults))
{
ResultTable tblResult = results[ResultType.RelevantResults];
DataTable relResultsTbl = new DataTable();
relResultsTbl.TableName = "Relevant Results";
DataSet ds = new DataSet("resultsset");
ds.Tables.Add(relResultsTbl);
ds.Load(tblResult, LoadOption.OverwriteChanges, relResultsTbl);
foreach (DataRow dataRow in ds.Tables[0].Rows)
{
accountNamesFromSearch.Add(dataRow["AccountName"] as string);
}
}
Quickly See if you can get the desired result in this MOSS Search tool good way to debug these kind of cases, rather than breaking our heads.