Tuesday, August 30, 2011

Using CAML to get items based on DateTime

Most of the Times we work on retrieve items from List or Document Library based on different Columns.This article is to target the datetime column.

Scenario 1: I want to get items which are greater than or equal to Today's date
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today /></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The attribute "Today" helps in defining the date to be Current Date
Scenario 2: We Want to get result from say 5 days from Today
<Query><Where><Geq><FieldRef Name="EndDate" /><Value IncludeTimeValue="TRUE" Type="DateTime"><Today  Offset="5"/></Value></Geq></Where><OrderBy><FieldRef Name="StartDate" Ascending="False" /></OrderBy></Query>

The Attribute "Offset" defines the No.of days we want to go back




Tuesday, August 16, 2011

Calling List webservice using Jquery

Most of the times we generally use object model to query SharePoint List.But not all development environments gives you an option of custom code.So JQuery along with Content Editor WebPart.
Here is the scenario I had a case of using dataview webpart which needs to be filtered basing on criteria, as we can create a parameter based on query string.In this case I am intrested in the filter rather than how to create DVWP.So coming to the coding part
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<getlistitems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listname>AppStatusList</listName> \
<viewfields> \
<viewfields> \
<fieldref Name='Title' /> \
</ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";

$.ajax({
url: "http://balaapd004/dept/cs/test/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});

$('#ddlSelect').change(function() {
//var selectedValue=
$('#txtResult').val($('#ddlSelect option:selected').text());
});
});

function processResult(xData, status) {
var listItems="";

$(xData.responseXML).find("z\\:row").each(function() {
listItems+= "<option value='" + $(this).attr("ows_ID") + "'>" + $(this).attr("ows_Title") + "</option>";
$("#ddlSelect").html(listItems);

});
}
</script>

<select id="ddlSelect">
</select>
<input type="text" id="txtResult" />

Monday, August 1, 2011

Extension Method to iterate against any control on a Page

We all know that in order to iterate against any control on page a method we generally go for is
Page.FindControl.
But this is a bit tedious process when we associate the same page with Master Page which adds one more foreach loop on the top and the best vs bad part is it gives you a bit of debugging time.

I think the best way of doing this is using the Extensions method which comes with c# 3.0

public static class myExtensions
        {        
            public static IEnumerable<T> AllControls<T>(this Control ctrl) where T : Control
            {
                bool find = ctlr is T;
                if (find)
                {
                    yield return ctrl as T;
                }
                foreach (var child in ctrl.Controls.Cast<Control>())
                {
                    foreach (var item in AllControls<T>(child))
                    {
                        yield return item;
                    }
                }
            }
        }

This gives you an ease in different ways as it can used along with Lambda expressions like this
Page.AllControls<CheckBox>().Where(c => c.Checked);

or simply to iterate on the control you want to find
 var chkBox = Page.AllControls<CheckBox>();

foreach (CheckBox i in chkBox)
       {
    //Your code
       }