Introduction
This small article is about a SharePoint caveat that I encountered while developing a WebPart. The cause of the problem was the command:
Directory.CreateDirectory(Server.MapPath("testdir"))
This indirectly breaks SharePoint and results in an HTTP 404 error.
The Steps to Reproduce
Step 1: Using VS 2007, create a WebPart with the following code:
protected override void CreateChildControls()
{
base.CreateChildControls();
Directory.CreateDirectory(Context.Server.MapPath("testdir"));
Label label = new Label();
label.Text = "Dropzone created";
this.Controls.Add(label);
}
Also add the code...
[assembly: System.Security.AllowPartiallyTrustedCallers()]
... to the AssemblyInfo.cs file.
Step 2: Deploy the WebPart to your SharePoint server. I won't attempt to document the deployment steps because it will take the focus away from the specific topic of this article.
Step 3: Change the trust level of SharePoint from minimal to full. This is not recommended in general, but I only want to show a quick way to allow the WebPart to create a folder.
From:
<trust level="WSS_Minimal" originUrl="" />
To:
<trust level="Full" originUrl="" />
Step 4: Go to YourSite > Web Part Gallery and add the new WebPart. This step may not be necessary for some deployment methods.
Step 5: Go to YourSite > Site Settings > Sites and Workspaces and create a new site for this test.
Step 6: Edit the default page, http://yourserver:###/TestMapPath/Pages/default.aspx, of the new site and add your WebPart to this page. Exit edit mode.
Outcome
Now whenever you view this page and WebPart, the following folder will be created:
\\yourserver\c$\Inetpub\wwwroot\wss\VirtualDirectories\###\TestMapPath\Pages\testdir
Great, you think. The WebPart worked as expected. It did work as expected, so far. But wait, there is more.
Step 7: Now restart the site. You can easily do this by modifying and saving the \\yourserver\c$\Inetpub\wwwroot\wss\VirtualDirectories\###\web.config file.
Step 8: Now try to visit the page http://yourserver:###/TestMapPath/Pages/default.aspx again.
The site is broken!
What happened?
When the WebPart created the directory, it created a physical folder that conflicts with the virtual content of the TestMapPath
site. When we accessed the page (after the restart), IIS was looking for the page in the physical folder, and could not find it there. IIS was not able to pass the request down the chain to the SharePoint service and retrieve the virtual content of the page.
History
- Version 1 dated 22 July 2008
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.