SPCalendarView control, same events are displayed each month in a VisualWebPart UserControl.ascx.cs file
Question
I am creating a custom SharePoint Calendar to pull from multiple SharePoint Calendar Lists.
In the markup I have the following:
<SharePoint:SPCalendarView ID="EventsCalendar2" width="100%" runat="server"></SharePoint:SPCalendarView>
I bind it like this in page load after getting the site and list myItems
is a SPListItemCollection
:
EventsCalendar2.DataSource = MakeSchedule(myItems);
EventsCalendar2.DataBind();
The MakeSchedule(myItems)
is the following:
private SPCalendarItemCollection MakeSchedule(SPListItemCollection myItems)
{
SPCalendarItemCollection items = new SPCalendarItemCollection();
for (int i = 0; i < myItems.Count; i++)
{
SPListItem item = myItems[i];
DateTime StartTime = Convert.ToDateTime(item["EventDate"]);
DateTime EndTime = Convert.ToDateTime(item["EndDate"]);
string Description = "";
if (item["Description"] != null)
Description = item["Description"].ToString();
string Location = "";
if (item["Location"] != null)
Location = item["Location"].ToString();
bool Recurrance = false;
if (item["fRecurrence"] != null)
Recurrance = (bool)item["fRecurrence"];
bool AllDayEvent = false;
if (item["fAllDayEvent"] != null)
AllDayEvent = (bool)item["fAllDayEvent"];
items.Add(
new SPCalendarItem()
{
ItemID = item["ID"].ToString(),
StartDate = StartTime,
EndDate = EndTime,
hasEndDate = true,
Title = item["Title"].ToString(),
Location = Location,
Description = Description,
IsAllDayEvent = AllDayEvent,
IsRecurrence = Recurrance,
CalendarType = (int)SPCalendarType.Gregorian
}
);
}
return items;
}
NOW I can't figure out why the events are being displayed in all of the months when they should only be displayed in the months specified by StartDate
& EndDate
. Any ideas on what I am doing wrong??
Could it be that the SPCalendarView
control cannot be used in a VisualWebPart?
La solution 2
had to set the SPCalendarView
's V4Rendering
property to false
to get it working (EnableV4Rendering="false"
). Of course this isn't the best solution as this removes some functionality added onto the Calendar from SharePoint 2007.
Other things I have come across in my research was that this is definitely a VisualWebPart
issue in particular the UserControl. I will edit this answer with the link.
A solution offered by others is updating the SharePoint server with the December2011 cumulative update. I will try that tomorrow, but for now that is the temporary solution.
http://www.sharepointdevelopment.me/2012/03/harnessing-the-spcalendarview/
&& Yes the HotFix I applied fixed it, here is the link: http://support.microsoft.com/kb/2596998
Autres conseils
for starters, you are calculating start and end dates differently
DateTime StartTime = Convert.ToDateTime(item["EventDate"]);
DateTime EndTime = Convert.ToDateTime(item["EndDate"] ).Date;
You should also use Convert.ToBoolean()
rather than casting it as x = (bool) object
. I've seen the latter fail in strange ways and if that happens to be the case here, then you could be ending up with Recurrance
being true when it shouldn't be.