Skip to content

Instantly share code, notes, and snippets.

@AmitKB
Last active December 17, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmitKB/5599327 to your computer and use it in GitHub Desktop.
Save AmitKB/5599327 to your computer and use it in GitHub Desktop.
private SPListItemCollection GetItemsUsingView()
{
// Get view
var list = SPContext.Current.Web.Lists.TryGetList("[MyList]");
var view = list.Views["[MyView]"];
#region Get "Where" filter expression from list view
// Get the filter expression from view
string filterCaml = string.Empty;
// Regex pattern to extract the filter
string pattern = @"<Where>\s*<.*</Where>";
// Regex options: Ignore Case & Singleline
var rxOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline;
var rx = new Regex(pattern,rxOptions);
// find
var match = rx.Match(view.Query);
if (match != null && match.Success)
{
filterCaml = match.Value;
}
#endregion
// Check if filter expression found
SPListItemCollection items;
if (filterCaml == string.Empty)
{
// No filter expression is defined in view
// so get all the items
items = TargetList.Items;
}
else
{
// Filter expression is defined.
// Use this filter express to get the
// the items
var query = new SPQuery();
query.Query = filterCaml;
items = TargetList.GetItems(query);
}
// return items
return items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment