简体   繁体   中英

How to query a specific view in sharepoint via powershell

So i've been trying to query a specific view for a list i have in sharepoint. The view is called "Pending" but im not sure where to apply the view within the code.

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
$list = $Context.Web.Lists.GetByTitle($listTitle)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery() 
$items = $list.GetItems($qry)
$Context.Load($items)
$Context.ExecuteQuery()
return $items}

It currently just takes in every entry within the list, regardless of what view they are in but i want it to only query the entries that are "Pending".

You will want to extend the function with an extra parameter for the name of the view.

I cannot test this myself, but this might work for you:

function Get-ListItemsFromView {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory = $true)]
        [Microsoft.SharePoint.Client.ClientContext]$Context, 

        [Parameter(Position = 1, Mandatory = $true)]
        [string]$ViewName,

        [Parameter(Position = 2, Mandatory = $true)]
        [string]$ListTitle
    )


    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $Context.Load($list)
    $Context.ExecuteQuery()

    $view = $list.Views.GetByTitle($ViewName)
    $Context.Load($view)
    $Context.ExecuteQuery()

    $qry = New-Object Microsoft.SharePoint.Client.CamlQuery
    $qry.ViewXml = $view.ViewQuery

    $items = $list.GetItems($qry)
    $Context.Load($items)
    $Context.ExecuteQuery()

    return $items
}

Views are not a data type in SharePoint. SharePoint knows only List and Library objects. Views are just ASP.NET Web Forms pages created by SharePoint. Filtering the Approval Status column in the list must be what you are looking for.

function Get-PendingListItems
{
    [CmdletBinding()]
    [OutputType([Microsoft.SharePoint.Client.ListItemCollection])]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$false,
                   Position=0)]
        [Microsoft.SharePoint.Client.ClientContext]
        $Context,

        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$false,
                   Position=1)]
        [String]
        $ListTitle
    )
    Process
    {
        # Fail Fast
        try {
            $list = $Context.Web.Lists.GetByTitle($ListTitle)
        } catch {
            Write-Error "The list $ListTitle does not exist."
            return $null
        }

        $query = New-Object Microsoft.SharePoint.SPQuery;

        # You can select the fields you would like to view. For more information look at: https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ms470490(v=office.14)
        # $query.ViewFields = '<ViewFields><FieldRef Name="ID"></FieldRef><FieldRef Name="Title"></FieldRef></ViewFields>';

        # 2 means pending. For More information look at: https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spmoderationstatustype?view=sharepoint-server
        $query.Query = '<Where><Eq><FieldRef Name="_ModerationStatus" /><Value Type="ModStat">2</Value></Eq></Where>';

        $items = $list.GetItems($query)
        $Context.Load($items)
        $Context.ExecuteQuery()

        return $items
    }
    End
    {
        $Context.Dispose()
    }
}

Here we have created a CAML query that filters only items with Approval Status as Pending . The number 2 means Pending , as you can see in the docs . You can filter the properties to return but I have commented out the line since it is not mandatory. You can have a look at that on the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM