0

I have a Custom list view web part on a SharePoint Page. The List has a custom date field "Exam Date".

I need to filter the list items based on current month, week and day. I don't want to use the calendar view or date filter.

I am looking to build a Custom web part for the solution. i.e. A drop down with the month /view or day values.

Can I set the filter criteria of the List View programmatically. i.e.

<View Name="{A8487F05-FCE5-45B5-8CB0-A47181FB968B}" MobileView="TRUE" Type="HTML" DisplayName="Marketing" Url="/SchoolExamGCSE/Lists/GCSEResults/Exam.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png">
                <Query>
                    <Where>
                        <And>
                            <Geq>
                                <FieldRef Name="ExamStartDate"/>
                                <Value Type="DateTime">{ExamStartDate}</Value>
                            </Geq>
                            <Leq>
                                <FieldRef Name="ExamEndDate"/>
                                <Value Type="DateTime">{ExamEndDate}</Value>
                            </Leq>
                        </And>
                    </Where>
                </Query>

I did try the Date Filter and connect it to the web part but it only works if the date filter value is set . I do not want to show the date filter . I did set the date filter value programmatically but it required me do postback i.e.

setTimeout(__doPostBack(\'ctl00$m$g_13ee3dc5_7ede_4e1d_985f_647b1cb1317f$DateFilterPicker$DateFilterPickerDate\',\'\')";, 0);

Or

Can I just set the yiew programmatically like

SPWeb webSite = SPContext.Current.Site.RootWeb;
myListQuery.List = webSite.Lists["Exam"];

SPQuery query = new SPQuery(myListQuery.List.Views[3]);
query.ViewFields = "<FieldRef Name=\"ExamStartDate\"/><FieldRef Name=\"Body\"/>";
query.Query = "<Query><Where><Leq><FieldRef Name='ExamStartDate' /><Value IncludeTimeValue='TRUE' Type='DateTime'>CurrentExamStartDate</Value></Leq></Where></Query>";

myListQuery.Query = query;

without having to set and pass the parameter

0

On view's page (datefilter.aspx) add the webpart with html-form with your dropdown filter. It has to generate query string such as:

/Lists/List/datefilter.aspx?StartDate=2018-05-10&EndDate=2018-05-30

Open this page in SharePoint Designer and add parameters to listview web part:

<ParameterBinding Name="StartDate" Location="QueryString(StartDate)" />
<ParameterBinding Name="EndDateDate" Location="QueryString(EndDate)" />

Then modify CAML query for this view with your params:

<Where>
  <And>
    <Geq>
      <FieldRef Name="ExamDate"/>
      <Value Type="DateTime">{StartDate}</Value>
    </Geq>
    <Leq>
      <FieldRef Name="ExamDate"/>
      <Value Type="DateTime">{EndDate}</Value>
    </Leq>
  </And>
</Where>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.