Last active
December 17, 2015 10:59
-
-
Save AmitKB/5599327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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