Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I currently have code in place which uploads files to a sites Shared Documents folder on my development machine - i.e. http: // mydesktop/mysubsite1. My machine (mydesktop) has a single site collection with 2 subsites - mysubsite1 and mysubsite2. This takes place from a Windows Service using the SharePoint API.

However, when I deploy the service to the test system, I am getting errors. The only obvious difference is the test system uses Alternate Mappings - i.e. sub.somedomain.com. This is on a different domain to my machine.

Code

using (SPSite siteCollection = new SPSite("http://sub.mysharepoint.com/mysubsite1"))
{
    using (SPWeb spWeb = siteCollection.OpenWeb())
    {
        SPList spList = spWeb.GetList("Shared Documents");
        SPFolder folder = SharePointHelper.GetFolder(reportsFolder, spList.RootFolder.SubFolders);

        foreach (var file in files)
        {   
            // upload file here
        }
    }
}

I initially had the following exception:

System.Data.SqlClient.SqlException: Cannot open database "WSS_Content_RTR" requested by the login. The login failed.
Login failed for user 'MyDomain\SP_Admin'.

Which I fixed by adding the user SP_Admin access to the SQL database.

This now throws the following exception:

Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMException (0x80131904): <nativehr>0x80131904</nativehr><nativestack></nativestack>
   at Microsoft.SharePoint.Library.SPRequestInternalClass.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)
   at Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)

I thought I'd write a test harness to figure what was going on (NOTE - I've tried this with http://sub.mysharepoint.com and http : // sub.mysharepoint.com/mysubsite1):

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string siteName = @"http://sub.mysharepoint.com";

            Console.WriteLine("Connecting to " + siteName);

            using (SPSite sc = new SPSite(siteName))
            {
                Console.WriteLine("Connected");
                string woo = sc.HostName;

                Console.WriteLine("HostName:" + sc.HostName);
                Console.WriteLine("ID:" + sc.ID);
                Console.WriteLine("Url:" + sc.Url);
                Console.WriteLine("Port:" + sc.Port);
                Console.WriteLine("Opening Web ");

                using (SPWeb spWeb = sc.OpenWeb())
                {
                    Console.WriteLine("Web Null? " + (spWeb == null).ToString());
                    Console.WriteLine("Web URL =  " + spWeb.Url);
                    //Console.WriteLine("Name = " + spWeb.Name);

                    Console.WriteLine("List Count = " + spWeb.Lists.Count);
                    foreach (SPList list in spWeb.Lists)
                    {
                        Console.WriteLine("List " + list.Title);
                    }

                    Console.WriteLine("Getting Shared Documents");
                    SPList spList = spWeb.GetList("Shared Documents");
                    Console.WriteLine("Got it!");               }
            }

            Console.WriteLine("Press Enter");
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.WriteLine("Press a key to continue");
            Console.ReadLine();
        }
    }
}

Which outputs the following:

Console App

I've now added some NLogging to the application. This is the full stack trace:

2013-07-11 10:40:26.0546 TestHarness.Program.TestConnection: Port to 80 
2013-07-11 10:40:26.0546 TestHarness.Program.TestConnection: Connecting to  
2013-07-11 10:40:26.0546 TestHarness.Program.TestConnection: Opening Web 
2013-07-11 10:40:26.0546 TestHarness.Program.TestConnection: Web Opened 
2013-07-11 10:40:26.1326 TestHarness.Program.TestConnection: Something happened Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMException (0x80131904): <nativehr>0x80131904</nativehr><nativestack></nativestack>
   at Microsoft.SharePoint.Library.SPRequestInternalClass.OpenWeb(String bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& pbstrDescription, String& pbstrTitleResourceId, String& pbstrDescriptionResourceId, Guid& pguidID, String& pbstrRequestAccessEmail, UInt32& pwebVersion, Guid& pguidScopeId, UInt32& pnAuthorID, UInt32& pnLanguage, UInt32& pnLocale, UInt16& pnTimeZone, Boolean& bTime24, Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& pbShowWeeks, Int16& pnFirstWeekOfYear, UInt32& pnFirstDayOfWeek, Int16& pnWorkDays, Int16& pnWorkDayStartHour, Int16& pnWorkDayEndHour, Int16& pnMeetingCount, Int32& plFlags, Boolean& bConnectedToPortal, String& pbstrPortalUrl, String& pbstrPortalName, Int32& plWebTemplateId, Int16& pnProvisionConfig, String& pbstrDefaultTheme, String& pbstrDefaultThemeCSSUrl, String& pbstrThemedCssFolderUrl, String& pbstrAlternateCSSUrl, String& pbstrCustomizedCssFileList, String& pbstrCustomJSUrl, String& pbstrAlternateHeaderUrl, String& pbstrMasterUrl, String& pbstrCustomMasterUrl, String& pbstrSiteLogoUrl, String& pbstrSiteLogoDescription, Object& pvarUser, Boolean& pvarIsAuditor, UInt64& ppermMask, Boolean& bUserIsSiteAdmin, Boolean& bHasUniquePerm, Guid& pguidUserInfoListID, Guid& pguidUniqueNavParent, Int32& plSiteFlags, DateTime& pdtLastContentChange, DateTime& pdtLastSecurityChange, String& pbstrWelcomePage, Boolean& pbOverwriteMUICultures, Boolean& pbMUIEnabled, String& pbstrAlternateMUICultures, Int32& puiVersion, Int16& pnClientTag)
   at Microsoft.SharePoint.Library.SPRequest.OpenWeb(String bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& pbstrDescription, String& pbstrTitleResourceId, String& pbstrDescriptionResourceId, Guid& pguidID, String& pbstrRequestAccessEmail, UInt32& pwebVersion, Guid& pguidScopeId, UInt32& pnAuthorID, UInt32& pnLanguage, UInt32& pnLocale, UInt16& pnTimeZone, Boolean& bTime24, Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& pbShowWeeks, Int16& pnFirstWeekOfYear, UInt32& pnFirstDayOfWeek, Int16& pnWorkDays, Int16& pnWorkDayStartHour, Int16& pnWorkDayEndHour, Int16& pnMeetingCount, Int32& plFlags, Boolean& bConnectedToPortal, String& pbstrPortalUrl, String& pbstrPortalName, Int32& plWebTemplateId, Int16& pnProvisionConfig, String& pbstrDefaultTheme, String& pbstrDefaultThemeCSSUrl, String& pbstrThemedCssFolderUrl, String& pbstrAlternateCSSUrl, String& pbstrCustomizedCssFileList, String& pbstrCustomJSUrl, String& pbstrAlternateHeaderUrl, String& pbstrMasterUrl, String& pbstrCustomMasterUrl, String& pbstrSiteLogoUrl, String& pbstrSiteLogoDescription, Object& pvarUser, Boolean& pvarIsAuditor, UInt64& ppermMask, Boolean& bUserIsSiteAdmin, Boolean& bHasUniquePerm, Guid& pguidUserInfoListID, Guid& pguidUniqueNavParent, Int32& plSiteFlags, DateTime& pdtLastContentChange, DateTime& pdtLastSecurityChange, String& pbstrWelcomePage, Boolean& pbOverwriteMUICultures, Boolean& pbMUIEnabled, String& pbstrAlternateMUICultures, Int32& puiVersion, Int16& pnClientTag)
   --- End of inner exception stack trace ---
   at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
   at Microsoft.SharePoint.Library.SPRequest.OpenWeb(String bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& pbstrDescription, String& pbstrTitleResourceId, String& pbstrDescriptionResourceId, Guid& pguidID, String& pbstrRequestAccessEmail, UInt32& pwebVersion, Guid& pguidScopeId, UInt32& pnAuthorID, UInt32& pnLanguage, UInt32& pnLocale, UInt16& pnTimeZone, Boolean& bTime24, Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& pbShowWeeks, Int16& pnFirstWeekOfYear, UInt32& pnFirstDayOfWeek, Int16& pnWorkDays, Int16& pnWorkDayStartHour, Int16& pnWorkDayEndHour, Int16& pnMeetingCount, Int32& plFlags, Boolean& bConnectedToPortal, String& pbstrPortalUrl, String& pbstrPortalName, Int32& plWebTemplateId, Int16& pnProvisionConfig, String& pbstrDefaultTheme, String& pbstrDefaultThemeCSSUrl, String& pbstrThemedCssFolderUrl, String& pbstrAlternateCSSUrl, String& pbstrCustomizedCssFileList, String& pbstrCustomJSUrl, String& pbstrAlternateHeaderUrl, String& pbstrMasterUrl, String& pbstrCustomMasterUrl, String& pbstrSiteLogoUrl, String& pbstrSiteLogoDescription, Object& pvarUser, Boolean& pvarIsAuditor, UInt64& ppermMask, Boolean& bUserIsSiteAdmin, Boolean& bHasUniquePerm, Guid& pguidUserInfoListID, Guid& pguidUniqueNavParent, Int32& plSiteFlags, DateTime& pdtLastContentChange, DateTime& pdtLastSecurityChange, String& pbstrWelcomePage, Boolean& pbOverwriteMUICultures, Boolean& pbMUIEnabled, String& pbstrAlternateMUICultures, Int32& puiVersion, Int16& pnClientTag)
   at Microsoft.SharePoint.SPWeb.InitWeb()
   at Microsoft.SharePoint.SPWeb.get_Name()
   at TestHarness.Program.TestConnection()

It would appear anything I try to do on spWeb errors. Any thoughts on these errors?

Is there a "correct"/best practice way of uploading files to a library? Baring in mind I do create sub folders in the Shared Documents along the way.

share|improve this question
    
Just a hint, an SPWeb object will not be null in non-existing. It will have .Exists set to false –  Robert Lindgren Jul 11 '13 at 10:26
    
It just throws SPException when I call .Exists!!! –  Andez Jul 11 '13 at 14:48
    
Sorry, missed that you use client dll –  Robert Lindgren Jul 12 '13 at 5:40
add comment

2 Answers

From the error message it looks like your are having issues with the contentdatabase. First try to run you program with the farm account to make your it is not some kind of permission problem. You allready this the ShellAdmin thing so you should be fine. The offical way for SP2010 and above would be to run the Add-SPSHellAdmin cmdlet:

http://technet.microsoft.com/en-US/library/ff607596(v=office.14).aspx

Next go into Central Admin (for SP2010 and SP2013) under "Upgarde and Migration" -> "Review database status" and check if your database is ok. If it need an upgrade you must first an Upgrade-SPContentDabase or the big hammer PSCONFIG.

http://support.microsoft.com/kb/841216/en

For SharePoint 2007 your screwed ;-) Try stsadm -o localupgradestatus.

share|improve this answer
    
Thanks for that. It was a security permission issue. I've detailed the answer to fix below. –  Andez Jul 22 '13 at 9:49
add comment
up vote 0 down vote accepted

It was a security issue. I revisited the ULS SharePoint logs to see the following error:

SqlError: 'The EXECUTE permission was denied on the object 'proc_GetTpWebMetaDataAndListMetaData', database 'WSS_Content_RTR', schema 'dbo'.'    Source: '.Net SqlClient Data Provider' Number: 229 State: 5 Class: 14 Procedure: 'proc_GetTpWebMetaDataAndListMetaData' LineNumber: 1 Server: 'XXX'
SqlError: 'The EXECUTE permission was denied on the object 'proc_GetTpWebMetaDataAndListMetaData', database 'WSS_Content_RTR', schema 'dbo'.'    Source: '.Net SqlClient Data Provider' Number: 229 State: 5 Class: 14 Procedure: 'proc_GetTpWebMetaDataAndListMetaData' LineNumber: 1 Server: 'XXX'

Although I'd granted the user SP_Admin access to the Content Database, it did not have the permissions in place to execute the stored procedures. So I ran the following SQL:

CREATE ROLE db_executor
GRANT EXECUTE TO db_executor

Then assigned the db_executor role to the SP_Admin user.

The test harness now works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.