Get next date in lucene index - search

I am storing a listing of events in a lucene.net index, which has a start date field. I would like to add a button for the user to click to see the next event (based on the start date field) relative to the event they are viewing.
I'm currently using a ConstantScoreRangeQuery search to do a search of events between two dates, but not sure how I can get the next date in the index.

heres a simple way to do it using a TermEnum
RAMDirectory ramDir = new RAMDirectory();
IndexWriter iw = new IndexWriter(ramDir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT));
Document d = new Document();
Field date = new Field("date", "", Field.Store.YES, Field.Index.ANALYZED);
d.Add(date);
DateTime first = DateTime.Now.Subtract(TimeSpan.FromDays(5));
DateTime second = DateTime.Now.Subtract(TimeSpan.FromDays(4));
DateTime third = DateTime.Now.Subtract(TimeSpan.FromDays(3));
DateTime fourth = DateTime.Now.Subtract(TimeSpan.FromDays(2));
DateTime fifth = DateTime.Now.Subtract(TimeSpan.FromDays(1));
date.SetValue(DateTools.DateToString(first, DateTools.Resolution.MINUTE));
iw.AddDocument(d);
date.SetValue(DateTools.DateToString(second, DateTools.Resolution.MINUTE));
iw.AddDocument(d);
date.SetValue(DateTools.DateToString(third, DateTools.Resolution.MINUTE));
iw.AddDocument(d);
date.SetValue(DateTools.DateToString(fourth, DateTools.Resolution.MINUTE));
iw.AddDocument(d);
date.SetValue(DateTools.DateToString(fifth, DateTools.Resolution.MINUTE));
iw.AddDocument(d);
iw.Commit();
IndexReader ir = iw.GetReader();
var termEnum = ir.Terms(new Term("date", DateTools.DateToString(second, DateTools.Resolution.MINUTE)));
if (termEnum.Next())
{
Console.WriteLine(DateTools.StringToDate(termEnum.Term().Text()));
Console.WriteLine(DateTools.StringToDate(termEnum.Term().Text()).Day == third.Day);
}
Console.Read();

Related

split date month and year in different fields

I have a date stored in my database in this format:
2013-01-08T17:16:36.000Z
I have searched for it a lot but it shows how to get this date format but what I am looking for is want to separate this like:
a=yyyy,b=dd and c=mm
And the rest is not required because I want to show date, month and year all this three in different fields.
I want to store the date in one variable, month in another variable and year in another one.
I tried this https://jsfiddle.net/ccywo7a9/ and actually works but i want this using split in node.js
This works
var Date='2013-01-08T17:16:36.000Z';
var b=Date.slice(0,10);
var c=b.split('-');
var e=c[Symbol.iterator]();
console.log(e.next().value);
console.log(e.next().value);
console.log(e.next().value);
and gives the value in three different variables.
Output:
2013
01
08
Try this method.. it's convert Mongo DB date and Time format...
String dateParts[] = "2013-01-08T17:16:36.000Z".split("T");
String split_date = dateParts[0];
String sp_time = dateParts[1];
System.out.println("sp_date----"+split_date);
String dateParts2[] = sp_time.split("Z");
String split_time = dateParts2[0];
System.out.println("sp_time----"+split_time);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss.SSS");
SimpleDateFormat cnv = new SimpleDateFormat("MMM dd hh:mm a");
Date modified_date = null;
try {
modified_date = format.parse(split_date+split_time);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("DATE----------------------"+cnv.format(modified_date));
Result :Jun 06 10:58 AM

How to remove time from date and time

In DOB i am getting date and time but i need only date
foreach (DataRow row in ProfileDt.Rows)
{
UserProfileData.FirstName = row["FirstName"].ToString();
UserProfileData.LastName = row["LastName"].ToString();
UserProfileData.Email = row["Email"].ToString();
UserProfileData.Address = row["HouseNo"].ToString();
UserProfileData.City = row["CityName"].ToString();
UserProfileData.date = row["DOB"].ToString();
}
If your database is giving you a string then you'll need to parse it to a date then back to a string with the desired date format.
string dateFormat = "MM/DD/YYYY HH:mm:ss" // REPLACE WITH THE CORRECT FORMAT
UserProfileData.date = DateTime.ParseExact(row["DOB"].ToString(), dateFormat ,
System.Globalization.CultureInfo.InvariantCulture).ToString("MM/DD/YYYY");
If your database is giving you a datetime directly (which is the better solution) then it becomes even easier.
UserProfileData.date = ((DateTime)row["DOB"]).ToString("MM/DD/YYYY");
Convert your string to DateTime type and use Date property, something like yourDate.Date
You can use DateTime.Parse or DateTime.ParseExact for parsing string to DateTime
It also make sense to return DateTime type directly from database so you do not need any conversions...

Issue with Updating an already updated Sharepoint recurring calendar item

I am trying to update a recurring calendar list instance, when i update the first time it all works fine but when i fetch the updated item and try to update it, than it does not get updated.Here is the code i have done to update a recurring item
//fetch List item, in my case it will be exact one item per day
list = web.Lists["Calendar"];
SPQuery query1 = new SPQuery();query1.Query = string.Concat(
//Today"<Where><DateRangesOverlap>","<FieldRef Name='EventDate' />","<FieldRef Name='EndDate' />","<FieldRef Name='RecurrenceID' />","<Value type='DateTime'>","<Today/>","</Value>","</DateRangesOverlap>",
"</Where>");
query1.ExpandRecurrence = true;
query1.CalendarDate = DateTime.Parse(txtCalendarDate.Text);
//as only one item is fetched updated the fetched item
SPListItem Newitem = list.Items.Add();
//The guid of the recurrence item id
string uid_old = Convert.ToString(items[0]["UID"]);
Newitem["UID"] = new Guid(uid_old);
Newitem["Title"] = ttl;
Newitem["Location"] = ttl;
//Override the EventDate and EndDate
Newitem["EventDate"] = startdate;//Same date as item fetched but different time
Newitem["EndDate"] = Enddate;//Same date as item fetched but different time
// recurrence item’s ID
int masterID;
if (Convert.ToString(items[0]["MasterSeriesItemID"]) == "" || Convert.ToString(items[0]["MasterSeriesItemID"]) == "0")
masterID = items[0].ID;
else
masterID = Convert.ToInt32(items[0]["MasterSeriesItemID"]);
Newitem["MasterSeriesItemID"] = masterID;
//Take recurrence item’s EventDate as RecurrenceID
Newitem["RecurrenceID"] = Convert.ToDateTime(items[0]["EventDate"].ToString());
Newitem["fRecurrence"] = 1;
Newitem["fAllDayEvent"] = 0;
Newitem["TimeZone"] = items[0]["TimeZone"].ToString();// What ever is the time zone.
//4 for override the recurrence item
Newitem["EventType"] = 4;
Newitem["RecurrenceData"] = Convert.ToString(items[0]["RecurrenceData"]);
Newitem.Update();
list.Update();
Can anyone help me figure out why the update is not working the second time i try to update? The updated entry is fetched properly, but it does not update it. The 1st time updated entry remains as it is and can be seen in Current Events in All Events another entry gets created but it is not reflected in the recurring item and during the same day if i try to fetch the entry than the 1st time updated entry is fetched.
Please refer image:
http://social.msdn.microsoft.com/Forums/getfile/185919
Please Help ME~

How to update a list item date/time field, programmatically with formated datatime

The date format is like this.
0001-01-01T00:00:00 -> not updating.
2012-05-21T00:00:00 -> updating properly
Following code I used.
SPSite site = new SPSite("http://server");
SPWeb web = site.OpenWeb();
SPWeb subsite = site.OpenWeb();
SPList englist = subsite.Lists["list"];
SPListItem item1 = englist.Items.Add();
item1["Title"] = "title";
item1["date"] = "0001-01-01T00:00:00";//0001-01-01T00:00:00 -> not updating. 2012-05-21T00:00:00
item1.Update();
Console.WriteLine("Added");
What is the reason the date time value "0001-01-01T00:00:00" not updating.
I find this might be ths issue( date range limitation)
Sharepoint using MSSQL for storing values of the fields, so DateTime field mapped to datetime sql type which have range from January 1, 1753, through December 31, 9999. That is the reason why can not store such small values in database.

Update Item has Recurrence in SharePoint

I had an event list. I created a new item that has recurrence daily ( Start Time : 1/5/2010 12 : 00 AM and End Time : 5/30/2010 12:00 AM). I want to delete the item which has Start Time : 5/12/2010 12:00 AM but my application throwed exception.
My code as below :
DateTime eventDate = DateTime.Parse(list.Fields.GetFieldByInternalName("EventDate").GetFieldValueAsHtml(DateTime.Parse(this.DateTimeOfItem).ToUniversalTime()));
SPQuery pQuery = new SPQuery();
pQuery.ExpandRecurrence = true;
pQuery.CalendarDate = eventDate.AddDays(-1);
pQuery.Query = string.Format("<OrderBy><FieldRef Name=\"EventDate\"/></OrderBy><Where><And><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Week /></Value></DateRangesOverlap><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">{0}</Value></Eq></And></Where>", this.ID);
SPListItemCollection itemColl = list.GetItems(pQuery);
int index = 0;
while (index < itemColl.Count)
{
SPListItem item = itemColl[index];
if (DateTime.Parse(item["EventDate"].ToString()).CompareTo(eventDate) == 0)
{
web.AllowUnsafeUpdates = true;
item["UID"] = Guid.NewGuid().ToString();
item["EventType"] = 3;
item["RecurrenceID"] = eventDate;
item["MasterSeriesItemID"] = this.ID;
item["XMLTZone"] = null;
item["RecurrenceData"] = "Every 1 day(s)";
item.Update();
list.Update();
web.AllowUnsafeUpdates = false;
break;
}
index++;
}
I do not know why I can not update this item. Please help me.
Thanks
PD.
To delete an instance of a recurring event in SharePoint you have to actually add a NEW record and mark it as deleted.
To understand recurring events in SharePoint and keep your sanity you need to write a little utility to output the whole list (every field) to learn how they work. The CAML query is expanding out the recurring event into 'fake' instances and these can't be updated.
When you setup a recurring event you only add 1 record into the list no matter how many instances it has. This is the 'master record' and has the recurrence pattern in the RecurrenceDate field.
When you add an exception (e.g. either the instance on "1/5/2010 12 : 00 AM" has been deleted or moved to another date) then this is another NEW record.
In this exception record you have the following fields of interest
MasterSeriesItemID - The ID of the master recurrance record
EventType - 3 for a modified instance, 4 for a deleted instance
RecurrenceID - The datetime of the instance that this execption replaces
It refernces the orgional recurrance record
This is about the best reference around for Recurring Events.
Understanding the SharePoint calendar and how to export it to iCal format
Be aware that in SharePoint 2007 there are some pretty crazy bugs converting to/from UTC to site time in the RecurranceID field when you've got 'All Day Events' (either the master record or the exception)
Yes Ryan is correct. You need to add a new record to delete an instance of a recurring series. For detailed info check out this URL : http://sharepointtechie.blogspot.com/2010/08/deleting-individual-events-from.html

Resources