2

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?

1
  • Could you please try to rephrase your question? It is difficult to understand what you are trying to accomplish. Oct 2, 2012 at 19:29

3 Answers 3

3

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()
1
  • This should be the correct answer, you can indeed query a SharePoint List View.
    – Paul Leigh
    Dec 3, 2013 at 13:51
0

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
  • 1
    This should really be a comment. Oct 2, 2012 at 19:28
0

@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);
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.