![]() |
Enterprise Systems »
SharePoint Server »
General
Intermediate
License: The Code Project Open License (CPOL)
Creating a Sub directory Breaks SharePoint Virtual Site MakeupBy Johan FourieCreating a sub directory using MapPath("testdir") breaks SharePoint virtual site makeup |
C#, Windows, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
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.
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.
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!
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Jul 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Johan Fourie Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |