I want to query and existing sharepoint view. Is this possible? I did not find any tutorial to guide for querying a view.How cant it be done? Kindly tell me is this possible or not and if yes than how?
3 Answers
I believe this is what you are looking for
Sorry for the powershell snippet, but it will be easy to test and easy to convert for usage in other api's
# Add SharePoint PowerShell Snapin
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$web = Get-SPWeb "http://url"
$listName = "List Name"
$list = $web.Lists[$listName]
$viewName = "View name"
$view = $list.Views[$viewName]
## Or get by view id
#$viewId = "A6932332-765A-4776-A1DF-18016DA1D645"
#$view = $list.GetView($viewId)
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope=""Recursive"""
$query.ViewXml = '<View><Query>' + $view.Query + '</Query></View>'
$items = $list.GetItems($query)
$items.Count
$web.Dispose()
-
This should be the correct answer, you can indeed query a SharePoint List View. Dec 3, 2013 at 13:51
You cannot query a list view.
A Sharepoint list view - can be create by definitions or by the web UI.
You dont "query" views, you use them to display list information.
You need to be more clearer with what you are asking.
regards, KS
-
1
@Eric Helitz's accepted answer totally gets the job done.
Here's my SOM version:
using (SPSite siteColl = new SPSite("http://site_collection_URL"))
{
using (SPWeb site = siteColl.OpenWeb())
{
SPList list = site.Lists["list_name"];
SPView view = list.Views["view_name"];
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
query.ViewXml = "<View><Query>" + view.Query + "</Query></View>";
SPListItemCollection results = list.GetItems(query);
}
}