This works as local administrator only, but I'm trying to figure out how I can make it work with NT AUTHORITY\SYSTEM
.
It starts spitting out group members but fails with access denied on a specific group on a site collection.
I've tried adding NT AUTHORITY\SYSTEM
to local administrators and WSS_ADMIN_WPG groups and to the SharePoint Farm Administrators group and the site collection admins group for the site giving the access denied error.
Update 2/25/2014: Tried granting db_owner to the content DB but that didn't work either.
USE [SharePoint - 33220]
GO
EXEC dbo.sp_addrolemember N'db_owner', N'NT AUTHORITY\SYSTEM'
GO
Here's the error -
Unhandled Exception: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS DENIED)) at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex) at Microsoft.SharePoint.Library.SPRequest.GetUsersDataAsSafeArray(String bstrUrl, UInt32 dwUsersScope, String bstrVal ue, UInt32 dwValue, UInt32& pdwColCount, UInt32& pdwRowCount, Object& pvarDataSet) at Microsoft.SharePoint.SPUserCollection.InitUsers(Boolean fCustomUsers, String[] strIdentifiers) at Microsoft.SharePoint.SPUserCollection.InitUsers() at Microsoft.SharePoint.SPUserCollection.Undirty() at Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator() at ConsoleApplication1.Program.c__DisplayClass2.b__0() at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state) at Microsoft.SharePoint.SPSecurity.c__DisplayClass4.b__2() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at ConsoleApplication1.Program.Main(String[] args)
Here's the code -
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SPWebServiceCollection spWebServiceCollection = new SPWebServiceCollection(SPFarm.Local);
foreach (SPWebService spWebService in spWebServiceCollection)
{
foreach (SPWebApplication webApplication in spWebService.WebApplications)
{
foreach (SPSite spSite in webApplication.Sites)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb spWeb = spSite.OpenWeb();
Console.WriteLine(spWeb.Title);
foreach (SPGroup group in spWeb.Groups)
{
Console.WriteLine(group.Name);
foreach (SPUser user in group.Users)
{
Console.WriteLine(user.LoginName);
}
}
});
}
}
}
}
}
}