Tobberik

I'm a little bit confused about the PortalUrl member of SPWeb. If I have the following code in a webpart placed on a page in a MOSS 2007 site other than the /QMS site used below, it turns out that the PortalUrl member of QMSWeb is null.

portalSite = Microsoft.SharePoint.WebControls.SPControl.GetContextSite(Context);

QMSWeb = portalSite.OpenWeb("/QMS");

I've tried some other constructs too with the same result. Can anyone explain how its supposed to work



Re: SharePoint - Development and Programming How does SPWeb.PortalUrl work?

eddy2705

You stay in the same context of your webpart. I use this:

string portalName;

SPSite portal = SPControl.GetContextSite(this.Context);

portalName = "http://" + portal.HostName + ":" + portal.Port.ToString();

using (SPSite site = new SPSite(portalName))

{

using (SPWeb refWeb = site.OpenWeb("/QMS"))

{

try

{

}

catch (Exception e)

{

log.Error(e.Message + "\n" + e.StackTrace);

}

}

}





Re: SharePoint - Development and Programming How does SPWeb.PortalUrl work?

Tobberik

Thanks for helping me out here.I've tried your solution in a small test webpart, but this time portalUrl end up as an empty string.

Code Snippet

protected override void Render(HtmlTextWriter writer)

{

string portalName;

SPSite portal = SPControl.GetContextSite(this.Context);

portalName = "http://" + portal.HostName + ":" + portal.Port.ToString();

using (SPSite site = new SPSite(portalName))

{

using (SPWeb refWeb = site.OpenWeb("/QMS"))

{

try

{

writer.WriteLine("Portalname:" + portal);

writer.WriteLine("refWeb.portalUrl:" + refWeb.PortalUrl);

writer.WriteLine("site.portalurl:" + site.PortalUrl);

}

}

catch (Exception e)

{

writer.WriteLine(e.Message + "\n" + e.StackTrace);

}

}

}

I've been searching around a bit more to see if there are any special considerations or some scenarios there it's not supposed to work, but either it's too simple and just works, or else it's not very much used.





Re: SharePoint - Development and Programming How does SPWeb.PortalUrl work?

eddy2705

wich one...refweb.PortalUrl or site.PortalUrl





Re: SharePoint - Development and Programming How does SPWeb.PortalUrl work?

Tobberik

Both actually.



Re: SharePoint - Development and Programming How does SPWeb.PortalUrl work?

eddy2705

and the portal

SPSite portal = SPControl.GetContextSite(this.Context);

does it have a value


if it does, then "/QMS" does not exist. Are you sure it is not "/sites/QMS"


try to print everything, and see what gives a value, so

portal, portal.HostName, etc.

This should give you clues what is null what isnt.


Sharepoint is such a pain in the *ss sometimes.