2
votes

Have a fun issue with sharepoint calendar view filtering. That code works fine:

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Site.ID))
                {
                    using (SPWeb site = siteCollection.OpenWeb())
                    {
                        site.AllowUnsafeUpdates = true;
                        SPView view = site.Lists["My Calendar"].Views["Calendar"];
                        view.Query = @"<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>ololo</Value></Eq></Where>";
                        view.Update();
                        site.AllowUnsafeUpdates = false;
                    }
                }
            });

But when I change query to

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Site.ID))
                {
                    using (SPWeb site = siteCollection.OpenWeb())
                    {
                        site.AllowUnsafeUpdates = true;
                        SPView view = site.Lists["My Calendar"].Views["Calendar"];
                        view.Query = @"<Where><Or><Eq><FieldRef Name='Title'/><Value Type='Text'>ololo</Value></Eq><Eq><FieldRef Name='Title'/><Value Type='Text'>trololo</Value></Eq></Or></Where>";
                        view.Update();
                        site.AllowUnsafeUpdates = false;
                    }
                }
            });

I get exception:

Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: Input string was not in a correct format.] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10161091 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +207 System.Convert.ToInt32(String value, IFormatProvider provider) +55 Microsoft.SharePoint.Utilities.SPUtility.CreateSystemDateTimeFromXmlDataDateTimeFormat(String strDT, Boolean fPreserveMilliseconds) +174 Microsoft.SharePoint.ApplicationPages.Calendar.SafeFieldAccessor.GetDateTimeFieldValue(SPItem item, String fieldName) +227 Microsoft.SharePoint.ApplicationPages.Calendar.CalendarItemRetriever.b_0(SPItem item) +24 System.Linq.WhereListIterator1.MoveNext() +288 System.Linq.<ExceptIterator>d__921.MoveNext() +322 System.Linq.Buffer1..ctor(IEnumerable1 source) +548 System.Linq.d_0.MoveNext() +164 Microsoft.SharePoint.ApplicationPages.Calendar.CalendarItemRetriever.ConvertItemType(IEnumerable1 items) +578 Microsoft.SharePoint.ApplicationPages.Calendar.DefaultCalendarListAccessor.Retrieve(String selectedDate, String scope, Dictionary2 entityInfo) +18 Microsoft.SharePoint.ApplicationPages.Calendar.CalendarService.CreateStartupResponse(ICalendarAccessor accessor, Dictionary`2 parameters, String viewType, String selectedDate) +249 Microsoft.SharePoint.ApplicationPages.WebControls.AjaxCalendarView.CreateStartupData(String viewType, String selectedDate) +462 Microsoft.SharePoint.ApplicationPages.WebControls.AjaxCalendarView.CreateBodyOnLoadScript(SPWeb web) +306 Microsoft.SharePoint.ApplicationPages.WebControls.AjaxCalendarView.OnPreRender(EventArgs e) +425 System.Web.UI.Control.PreRenderRecursiveInternal() +108 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Control.PreRenderRecursiveInternal() +224 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

Does anybody know how fix that problem?

2
A bit OT but why are you using RunWithElevatedPermissions? Its likely that you shouldn't be (unless you are deliberately bypassing security trimming) and even if you are - its not really relevant for this question so its confusing the code snippet.Ryan

2 Answers

1
votes

The problem is solved. "DateRangesOverlap" should be added to the calendar query.

view.Query = @"<Where><And><DateRangesOverlap>
    <FieldRef Name='EventDate' />
    <FieldRef Name='EndDate' />
    <FieldRef Name='RecurrenceID' />
    <Value Type='DateTime'><Month /></Value>
</DateRangesOverlap>
<Or><Eq><FieldRef Name='Title' />
<Value Type='Text'>orlolo</Value></Eq>
<Eq><FieldRef Name='Title' />
<Value Type='Text'>trololo</Value></Eq>
</Or></And></Where>";
0
votes

Are you sure the item with the Title "trololo" isn't "trolling" your page and breaking it (probably due to invalid data)?

See if you can manually create that view in the UI, then write a console app to go in there and print out what it generates for the Query. I just did and the query was syntactically identical to your example.