Bil Simser over at the Fear and Loathing blog has put together a very good tutorial on how to use Subversion properly, including branching and revisions.
He has pictures, and goes very very slowly which is helpful for “one repository, one code base” people like myself.
It’s crystal clear, and if you use Subversion, will actually make it easier for you to use.
To do so, is quite simple in concept, just identify the display mode, and chuck an “if” statement in there. However, it did take me a while to find the right code and I thought it was worth posting about, especially because it can help you give users a more professional & polished interface.
First, where I looked for the display mode. There’s a WebPartManager class which exists, and has a display mode, but it’s tricky to find an instance.
If while inside CreateChildControls or OnPreRender you try:
WebPartDisplayMode currentMode = this.WebPartManager.DisplayMode;
you will get a null pointer exception, because this.WebPartManager == null. I don’t understand why, but it does.
What you need to do is execute the static GetCurrentWebPartManager function on your current page object (found this from a post by Rich from DevAndDesign.com).
WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(this.Page);
this.Controls.Add(new Literal(“browse mode”));
this.Controls.Add(new Literal(“not browse mode”));
This relies on the boolean AllowPageDesign property. If true, the page is in edit mode (that includes the edit, connect, design & catalog modes). It’s definitely a bit silly that this.WebPartManager doesn’t work, especially because it’s such a simple property to implement. Maybe somebody can fill me in on the history?
If you’ve had this problem, then two things to make you feel better:
This post, meanders a bit, so if you want the solution, jump to the bottom.
Still here? Sucker.
So, what are the prerequisites to causing this problem:
I think that’s it.
The web part will display correctly anywhere in your root web, but nowhere else. I’m calling it a “web” here because it maps to how Sharepoint’s API. If it helps for you to think about it as a single “Site” in WSS2.0 fashion, go nuts. When you visit subsites, you see the following error:
“Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator.”
How helpful, especially if you’re an administrator or developer. As if they’d have a magic wand to fix the problem. The error might as well say:
“You’re screwed. Unless you’re a clever developer, give up, swear at Sharepoint and call tech support. Not many people at your company are going to be able to help you.”
Which is silly, because once we come to the cause, the error is completely within the realms of detection. To the developer who wrote that line of code… 10 minutes on the naughty step.
The problem is seemingly unresolveable. If you try to add the dataview directly to a page in a subsite, it works fine, but master page? Sharepoint tells you to go whistle.
The cause? Sharepoint doesn’t know which “web” the list comes from.
The Dataview web part can take parameters, including, parameters which tell pages where to find a list. In Sharepoint Designer, when you edit a master page, it fails to insert a parameter telling pages the list lives in the root.
Bad Sharepoint Designer! Bad!
The problem compounded because taking a dataview created on a subsite and dumping the code into the masterpage doesn’t work either (which makes it very tricky to solve).
To figure this out, I took the following steps in Sharepoint Designer:
A side-by-side comparison revealed several things…
First, the datasource control (a child of the dataview web part) is referred to with a different namespace in the master page than in the page itself. This is why you can’t copy dataview code from a regular page into a master page. Maybe there’s a good reason for having done this, but I think the masterpage programmer forgot to speak to the regular page programmer. However, that isn’t the root cause, but something to be aware of.
Second, THERE’S ANOTHER PARAMETER. OMG!!! Froth, swear, stomp, swear, slam your keyboard. Feel better?
Knowing a parameter exists and knowing the syntax are two different things. Before finding the actual code, I was pretty confident the problem was down to subsites not knowing where the list was.
The worst thing is that the url of the web is actually part of the dataview web part, down at the very bottom of the code. Why doesn’t this flow through to the datasource? Naughty step!
It’s simple enough to copy the parameter from the subsite dataview into the masterpage dataview.
<WebPartPages:DataFormParameter Name=”WebURL” ParameterKey=”WebURL” PropertyName=”ParameterValues” DefaultValue=”/”/>
I don’t believe the approach is necessary if you’re referring to a list in a subsite (the parameter should be inserted automatically).
Hope that helps!