Click here to Skip to main content
15,894,955 members
Articles / Programming Languages / C#

Creating a Sub directory Breaks SharePoint Virtual Site Makeup

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Jul 2008CPOL2 min read 23.3K   60   8  
Creating a sub directory using MapPath("testdir") breaks SharePoint virtual site makeup

Introduction

This small article is about a SharePoint caveat that I encountered while developing a WebPart. The cause of the problem was the command:

C#
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:

C#
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...

C#
[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:

XML
<trust level="WSS_Minimal" originUrl="" />

To:

XML
<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.

Captured_on_22_Jul_2008_11_55_53_1691.jpg

Step 5: Go to YourSite > Site Settings > Sites and Workspaces and create a new site for this test.

Captured_on_24_Jul_2008_11_59_21_7011.jpg

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.

Captured_on_22_Jul_2008_12_13_22_5461.jpg

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --