Archive

Archive for the ‘Uncategorized’ Category

Be very, very careful when you use SPSite.OpenWeb()!

September 27, 2009 Leave a comment

I recently came across what can only be described as a shocking and dangerous bug in the SharePoint API.

According to the MSDN documentation the SPSite.OpenWeb() method:

“Returns the site that is associated with the URL that is used in an SPSite constructor.”

Below is an example of how it is very commonly used:

using (SPSite site = new SPSite("http://www.myserver.com/parentSite/childSite"))
{
using (SPWeb web = site.OpenWeb())
{
//Do your stuff
}
}

Going by the MSDN documentation the code above should return the web site located at http://www.myserver.com/parentSite/childSite. This is further confirmed by the following statement:

“When used in conjunction with an SPSite constructor, the OpenWeb method returns the lowest-level site specified by the URL that is passed as parameter for the constructor.”

This is all true if the site located at the specified url exists. But what if it doesnt exist?

Consider a scenario where an SPWeb with the url http://www.myserver.com/parentSite exists but an SPWeb with the url http://www.myserver.com/parentSite/childSite does not exist. In this scenario you would expect the code above to throw an exception, however, it does no such thing instead it ends up opening a completely different SPWeb. In this specific scenario it returns an SPWeb with the url http://www.myserver.com/parentSite. If an SPWeb didnt exist at this url as well then it would have returned the RootWeb! (i.e. http://www.myserver.com if it existed)

In other words you would end up opening and performing actions on a totally different SPWeb! As you can imagine this can have very dangerous consequences as I only very recently found!

Categories: Uncategorized

Meeting Workspace: How to programatically add tabs

April 3, 2009 1 comment

When you add a page via the UI it adds it as a WebPartPage to a hidden list called “Workspace Pages”. It also does something internally to create the Tab because simply progammatically adding the WebPartPage to the hidden list doesnt seem to work.

After spending a lot of time investigating this issue I finally found a solution. The solution is listed below:
 

string newPage = string.Empty;
Type cmType = typeof(SPMeeting);
SPMeeting mtg = (SPMeeting)
Activator.CreateInstance(cmType,
BindingFlags.NonPublic | BindingFlags.Instance,
null, null, System.Globalization.CultureInfo.CurrentCulture,
null);
Type t = mtg.GetType();
t.InvokeMember(
"m_Web", BindingFlags.NonPublic
|
BindingFlags.Instance | BindingFlags.SetField,
null, mtg, new Object[] { web });
mtg.AddPage(
"JunkYard", 1, out newPage);

The SPMeeting class has a method “AddPage”. This class has no public constructors therefore you cannot instantiate it the normal way. Once you instantiate it you need to set, via reflection, m_Web which is a private member and holds the SPWeb object. The final step is to call the AddPage method passing it the name of the page you want to add, an instanceid (not sure what this is), and an out string parameter.

Follow

Get every new post delivered to your Inbox.