Archive for December, 2012

SharePoint: Create Dynamic SPQuery

Scenario:

We need to develop a webpart that searches a list in SharePoint. The search criteria are based upon user input. A simplified image of search criteria is as under:


Resolution:

After googling, we found a great solution:
http://sharepointcamlhelper.codeplex.com/

Suppose we have two checkboxes and two radio button, then our query should be like this:

SPQuery oQuery = new SPQuery();
CAMLManager mgr = new CAMLManager();
mgr.QueryGroups.Add(new QueryGroup("ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
if (chkSelectGroup.Checked == true)
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, ddlSelectGroup.SelectedItem.Text));
}
if (chkSubject.Checked == true)
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Contains, txtSubject.Text));
}
if (rbAnnByEmail.SelectedIndex != -1)
{
if (rbAnnByEmail.SelectedItem.Text == "Yes")
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
}
if (rbAnnByEmail.SelectedItem.Text == "No")
{
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, "ColumnName", Types.FieldTypes.Text, Types.QueryTypes.Eq, "ColumnValue"));
}
}
mgr.OrderBy.Add(new OrderBy("ColumnName", false));
string camlQuery = mgr.GetFullCAML();
oQuery.Query = camlQuery;

Issue:
One thing that doesn’t seems to be working is “ViewFields”. We have added following lines to restrict our query to specific fields, but it doesn’t seems to be working.

cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));
cmgr.ViewFields.Add(new ViewField("ColumnName"));


Update:
If we add following code, ViewFields seems to work

spQuery.ViewFieldsOnly = true;
spQuery.ViewFields = cmgr.GetViewFields().Replace("", "").Replace("", "");

Advertisements

Comments (1)

SharePoint:InputFormTextBox: Validation Issue after postback

Scenario:

We have placed a “SharePoint:InputFormTextBox” text control on our webpart and wanted to make it a “Required Field”. We added an ASP “RequiredFieldValidator” which works fine when we click submit.

We have other controls on the form as well. If an event is occurred and any other controls postback, then, the validation on “SharePoint:InputFormTextBox” fails to work if we try to submit.

Resolution:

We look into the issue and tried to resolve by adding implementing different solutions but nothing was working. In the end, found a reply on MS SharePoint forum that was buried in other replies. Suggested setting the initial property of “RequiredFieldValidator”.

Turned out the when webpart postback, somehow, “<DIV></DIV>” is added in “SharePoint:InputFormTextBox”. You can check it by editing the control “SharePoint:InputFormTextBox” after postback from some other control.

We set

RequiredFieldValidator3.InitialValue = “<DIV></DIV>”

in every control server-side code that postback.

Source: http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/e9fbbd43-bef9-48fc-9a9f-0011514798e7/ Look at the last reply by “cschoenfeldt“.

Leave a Comment