tags:

views:

594

answers:

6

I'm trying to run a console application that uses the SharePoint Object Model.

I'm getting the error Cannot open database "dbname" requested by the login. The login failed. Login failed for user 'DOMAIN\userid'.

Some place I have read that the user must have permission to the Content DB.

I can not find an article that explains what permissions to setup. I need this as ammunition to go to my Sys Admin guy to get the permission setup.

Where is there an article that explains that? I have searched google but with no luck.

A: 

Are you running the console application on the server itself? I assume so.

In this case it is likely to be a permissions issue with the account you are using (RDP?) on the server. The database error side of things can be misleading as you will need to be permissioned within SharePoint itself, which will then give permissions to the database.

I would get your sys admins to create a service account for you to use that can be granted the correct rights. (site collection administrator is often needed, but it depends on the code inside the console app. most do assume site collection admin rights though). you may get more mileage from looking at the application instructions (or if it is your own code just go for site collection admin)

Running a console app is a bit of a major though, so you may have better luck if you give the sysadmins the application to run and instructions... though I doubt you are running this on the prod box.

Nat
A: 

OK, here is the code I'm trying to run (simple but I'm moving forward with baby steps):

    private static void DisplayAllLists(string site, string webToOpen)
    {
        try
        {
            using ( SPSite siteCollection = new SPSite(site) )
            {
                try
                {
                    using (SPWeb web = siteCollection.OpenWeb(webToOpen))
                    {
                        SPListCollection lists = web.Lists;
                        foreach (SPList list in lists)
                        {
                            Console.WriteLine(string.Format("List Title: {0}", list.Title));
                        }
                    }
                }
                finally
                {
                    siteCollection.RootWeb.Dispose();
                    Console.ReadLine();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: "+ex.Message);
        }
    }

And here is my specific error (with the db name and user information obscured):

Exception: Cannot open database "..._WSS_0_Portal" requested by the login. The login failed.
Login failed for user 'DOMAIN\...'.

If I ask that the account I'm using be given Full Control, under the Policies for Web Application, should that work?

hobbyman
A: 

Hi, your user propably don't have permissions to access those lists or webs. You can run your code with elevated privilegies, but it can sometimes give you unexpected results. Example of how elevated privilegies is used can be found here

Or you can set user unser who's account console app runs as site collection administrator.

Your code updated to run with elevated privilegies can look like this:

private static void DisplayAllLists(string site, string webToOpen)
{
    try
    {
       SPSecurity.RunWithElevatedPrivileges(delegate() 
       {


          using ( SPSite siteCollection = new SPSite(site) )
          {
            try
            {
                using (SPWeb web = siteCollection.OpenWeb(webToOpen))
                {
                    SPListCollection lists = web.Lists;
                    foreach (SPList list in lists)
                    {
                        Console.WriteLine(string.Format("List Title: {0}", list.Title));
                    }
                }
             }
           }
           finally
            {
                siteCollection.RootWeb.Dispose();
                Console.ReadLine();
            }
        }

    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception: "+ex.Message);
    }
}

Note: This code was written from top of my head, so maybe something is missing..you will have to try it

drax
A: 

I added the RunWithElevatedPrivileges but I still get the same error.

hobbyman
A: 

RunWithElevatedPrivileges doesn't help because it just changes the thread user to the process identity - in a console application, this has no effect because they're the same. The "elevation" in an impersonated web context works because the process identity is the application pool account, which has db_owner on its content database.

If I ask that the account I'm using be given Full Control, under the Policies for Web Application, should that work?

Not according to Ishai Sagi: Object model code and stsadm fail with "Access Denied". In short, it seems db_owner permissions on the content database are required for a user to run object model code (including STSADM) without a web context.

dahlbyk
A: 

OK - I finally got my console application to work. Thanks for your help. I also had to use this article to get me going.

hobbyman