Get SharePoint List's Personal Views using PowerShell [duplicate]
-
14-12-2020 - |
Question
I'm trying to get all names of the personal views created by users for a list.
When I'm using the code
$views = $list.views
foreach($view in $views){
Write-Host $view.Title
}
It only lists the Public views so there is no point in using the condition $view.PersonalView.
Is there any way I can list all the views, including personal and public ones and then filter for only the personal ones?
Solution
I have never tried what @gautam-sheth has suggested... It may work although I assume you are running as an admin anyway?
Anyway what does work for me is to get the SPSite object with a user token as in the following function:
function Get-SPSiteWithToken {
param (
[string]$spSite,
[string]$spweb,
[Microsoft.SharePoint.SPUserToken]$userToken,
[string]$spList
)
$site = New-Object Microsoft.SharePoint.SPSite($spSite, $userToken)
$web = $site.OpenWeb($spWeb)
$list = $web.Lists.TryGetList($spList)
if ($list) {
foreach ($view in $list.views) {
if ($view.PersonalView) {
Write-Host $view.Title
#Do some stuff here
}
}
}
}
You then need to call it from something like:
$web = Get-SPWeb http://sharepoint/site/web
foreach ($user in $web.Users) {
if (!$user.IsDomainGroup) {
Get-SPSitewithToken "http://sharepoint/sites/sitecollection" "/relativeUrl/to/web" $user.UserToken "TargetListName"
}
}
OTHER TIPS
Try using Run with elevated priviledges as below:
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
$url = "https://sitecollectionurl"
$web = Get-SPWeb -identity $url
$list = $web.Lists["CustomList"]
$viewCollection = $list.Views
foreach($view in $viewCollection)
{
if($view.PersonalView)
{
Write-host "Personal View " + $view.Title
}
else
{
Write-host "Public View " + $view.Title
}
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange