views:

1752

answers:

3

This may seem a bit trivial, but I have not been able to figure it out. I am opening up a SPSite and then trying to open up a SPWeb under that SPSite. This is working fine on the VPC, which has the same Site Collection/Site hierarchy, but on production, I get an exception telling me that the URL is invalid when I try the SPSite.OpenWeb(webUrl);. I have verified that the URL’s are correct.

The Code:

  try
        {
            SPSite scheduleSiteCol = new SPSite(branchScheduleURL);
            lblError.Text += Environment.NewLine + "Site Collection URL: " + scheduleSiteCol.Url;
            SPWeb scheduleWeb = scheduleSiteCol.OpenWeb(branchScheduleURL.Replace(scheduleSiteCol.Url, "")); //<--- Throws error on this line
            SPList scheduleList = scheduleWeb.GetList(branchScheduleURL + "/lists/" + SPContext.Current.List.Title);
            return scheduleList.GetItemById(int.Parse(testID));
        }
        catch (System.Exception ex)
        {
            lblError.Text += Environment.NewLine + ex.ToString();
            return null;
        }

Note:
branchScheduleURL is actually the whole URL that includes the URL of the Web as well.

The output + exception:

Site Collection URL: https://ourSite.com/mocc
System.ArgumentException: Invalid URL: /internal/scheduletool. at Microsoft.SharePoint.SPSite.OpenWeb(String strUrl, Boolean requireExactUrl) at Microsoft.SharePoint.SPSite.OpenWeb(String strUrl) at MOCCBranchScheduleListWeb.MOCCBranchScheduleListV3.GetConflictListItem(String branchScheduleURL, String testID)System.NullReferenceException: Object reference not set to an instance of an object. at MOCCBranchScheduleListWeb.MOCCBranchScheduleListV3.CheckForConflicts(String[] cfcFlags1, DateTime startTime, DateTime endTime, String[] cfcFlags2)

Note:
https://ourSite.com/mocc/internal/scheduletool is the SPWeb I am trying to open.

Am I missing something obvious? Any help would be greatly appreciated.

Thanks.

+2  A: 

Looks at the examples table at the bottom of this page.

Try not sending any parameters into the OpenWeb() method (2nd row).

Eugene Katz
Thanks! I did not realize that the constructor for the SPSite remembered the original URL passed in. I thought it just stripped the url down to the site collection and disregarded the rest.
AdamBT
+1  A: 

Try getting the SPWeb object for "Internal" first. then get the SubWeb SPWebCollection for that and object. From that, try to get the SPWeb object for "ScheduleTool" using the GetSubwebsForCurrentUser() Method.

ashwnacharya
A: 

It says your Site Collection URL is /mocc, thus your SPWeb underneath would be something like /mocc/internal/scheduletool. So do something like

string webServerRelativeUrl = site.ServerRelativeUrl + "/internal/scheduletool"

Peter Seale