Accessing Document Library by Subsite URL and List Name Fails with SPException

I am attempting to access a document library in a nested site http://sharepoint.abc.com/XXX/YYY/Test. XXX is a subsite of the root site collection (http://sharepoint.abc.com/) and YYY is a subsite of XXX.  When I get an SPException (FAILED hr detected error code -2146232060 ("FAILED hr detected (hr = 0x80131904)") at SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists[g_docLibName] in the following code block:

                string g_siteUrl = "http://sharepoint.abc.com/XXX/YYY";
                string g_docLibName = "Test";

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(g_siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists[g_docLibName];
                            SPFileCollection files = documentLibrary.RootFolder.Files;
                            SPFile newFile = files.Add(documentLibrary.RootFolder.Url + "/" + filename, file_bytes, true);

...

Therefore I attempted to open the subsite(web) another way but recieve the same SPException:

                string g_siteUrl = "http://sharepoint.abc.com/";
                string g_webURL = "XXX/YYY";
                string g_docLibName = "Test";

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(g_siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb(g_webURL))
                        {
                            web.AllowUnsafeUpdates = true;
                            SPDocumentLibrary documentLibrary = (SPDocumentLibrary)web.Lists[g_docLibName];
                            SPFileCollection files = documentLibrary.RootFolder.Files;
                            SPFile newFile = files.Add(documentLibrary.RootFolder.Url + "/" + filename, file_bytes, true);

...

When I step through the code SPSite is returning the root site collection(SPSite URL=http://sharepoint.abc.com/) and all properties state "threw an exception of type Microsoft.SharePoint.SPException". The code is wrapped in a try but the catch is only triggered once the SPDocumentLibrary is instantiated.

Can someone please let me know what I am doing wrong here this should be a simple task. I have seen documented this way repeatedly. The path http://sharepoint.abc.com/XXX/YYY/Test/Forms/AllItems.aspx is a valid Document Library and is accessble. Any suggestions are appreciated. Thank you.

 
  • Edited by JonesEJ20 Wednesday, October 02, 2013 12:32 AM Wrong
October 2nd, 2013 3:30am

Try changing the g_webUrl value to this "/XXX/YYY".  The initial slash will make the web url relative to the site, so it should find the YYY web without problem.

HTH

Stuart

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 6:13pm

0x80131904 indicates a SQL error

Isn't the Site URL too long? Are all parameters correct? Is the site accessible through the browser? Has the application pool account enough permissions?

October 3rd, 2013 6:17pm

Hello,

Could you try AllWebs[] method to detect subsite within site collection:

using (SPSite site = new SPSite(g_siteUrl))
{
 using (SPWeb web = site.AllWebs["YYY"])
{
}}

Free Windows Admin Tool Kit Click here and download it now
October 14th, 2013 6:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics