Get all Personal Views from a SharePoint List
-
28-09-2020 - |
Question
I understand that I can impersonate a user with a user's token to get their personal view, but is there a less brute way of getting personal views?
Is there an account I'm over-looking that has "super access" that would be able to return all personal views? Or is iterating through each user and impersonating the only option here?
Solution
You can use SPSecurity.RunWithElevatedPrivileges method for super access.
Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.
static void GetViews(SPSite site)
{
SPWeb spweb = site.OpenWeb();
foreach(SPUser oUser in spweb.AllUsers)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
SPSite oElevSite = new SPSite(site.ID, oUser.UserToken);
SPWeb oElevWeb = oElevSite.RootWeb;
SPList splist = oElevWeb.Lists["CustomStatusList"];
SPViewCollection views = splist.Views;
foreach (SPView view in views)
{
if (view.PersonalView)
{
Console.WriteLine(oUser.Name +":"+ view.Title);
}
}
oElevWeb.Dispose();
oElevSite.Dispose();
}
catch
{
}
});
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange