views:

965

answers:

3

Here is my code snippet:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    SPSolution newSolution = SPFarm.Local.Solutions.Add(@fullPath);                            
});

The stacktrace and innerexception give no further clues. The Exception.Source says Microsoft.SharePoint.

SPFarm.Local.CurrentUserIsAdministrator() returns TRUE for the userid.

The userid is in the Farm Administrators group.

Any ideas?

EDIT

I have changed my code to the following and still get the Access Denied error:

private void AddSolution()
{
   SPSolution newSolution = SPFarm.Local.Solutions.Add(@fullPath);
}

SPSecurity.CodeToRunElevated elevatedAddSolution = new SPSecurity.CodeToRunElevated(AddSolution);
SPSecurity.RunWithElevatedPrivileges(elevatedAddSolution);
+1  A: 
Charlie
What is meant by your first question?
hobbyman
I meant basically trying what you have done above, although it appears not to have worked.
Charlie
Are you running this on a local web service as previously discussed?
Charlie
I haven't gotten that far yet. Right now I have it in an ASPX page in _layouts. It worked a couple of weeks ago but now it does not.
hobbyman
+1  A: 

I do believe that the issue you are having is due to the fact that you are using the static member to access the SPFarm object. I think that it is similar to running the SPcontext static class which will still run under the security context of the logged on user and not under the elevated privledges context (which is the local application pool identity).

Try this instead inside your delegate:

SPFarm spFarm = SPWebService.AdministrationService.Farm;
SPSolution newSolution = spFarm.Solutions.Add(@fullPath);

EDIT: Since the above didn't help then your issue probably has to do with database permissions to the config database. The RunWithElevatedPriviliges will run under the application pool's identity that the code is running under. Adding a solution to your farm affects the configuration database so your application pool identity will need access to the config database. As a test try adding the app pool identity to the config db and give it dbo permissions. If that fixes the issue then you will need to find the minimum amount of permissions that each of your app pool accounts will need to add solutions (do not leave as dbo)

webwires
Made that change but still get the same error.
hobbyman
I'm showing my laziness and ignorance. How do I find the name of the CONFIG DB?
hobbyman
Registry: HKLM\Software\Microsoft\Shared Tools\Web Server Extensions\12.0\Secure\ConfigDb
webwires
+1  A: 

Your main problem might just be that you are not DBO of a sharepoint database (_Config if I'm not wrong). Adding a solution to a farm is something that require more rights than just access to the farm.

Be sure that the user running this is Farm Administrator and DBO of the proper database.

If you still have problem... try running

stsadm -o addsolution -filename "myWsp.wsp"

If you have the proper right, it will give you the proper error.

Maxim
That is what the problem is. I run stsadm, it gets and error, I look in the event logs and it says my userid does not have access to the config database.
hobbyman