Geeks With Blogs
Calin Tatar C#.NET, VB.NET, WPF, SILVERLIGHT, SQL, WSS, MOSS

Last week I was asked on one of my CodeProject article, about how to implement a Sharepoint feature that will automatically add subfolders to a parent folder in a document library.

To add subfolders when a new Folder(or derived Content Type from Folder) was created, we have to write a feature, that will handle the ItemAdded event. When this event gets fired, we can get the current folder item and add subfolders to it.

The following feature adds subfolders( the ones from folders array) when a new Project Folder is created.

SubFoldersCreatorFeature

Basically, what the above method is doing is:

- we get the current document library that we are working on.

- test if the current Item is derived from our Content Type; with this we can restrict the feature to work only on some of the folder content types

- by getting the url of the current folder, we can add a new folder on the current SPDocumentLibrary.

Enjoy!

Calin Tatar

Posted on Sunday, June 7, 2009 4:48 AM UI Automation , MOSS 2007 , WSS 3.0 | Back to top


Comments on this post: Automatically add Sharepoint subfolders when adding a new Folder Content Type

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
what program is the code created in ?
Left by Mike Brickles on Jul 20, 2009 7:44 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Mike,

It was Visual Studio 2005, class library project, referencing Sharepoint API, deployed as SP feature.

Calin
Left by Calin Tatar on Jul 21, 2009 1:00 AM

# Mr
Requesting Gravatar...
I really want to thank you for the code.
However since its a #$%^& screenshot I would have to retype it all. So I shall continue my googles and come back if I fail, prepared for the typing.
Left by Yan Herndon on Oct 06, 2009 11:25 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
I could paste the code here if you want.

Calin
Left by Calin on Oct 08, 2009 10:06 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Yes please paste the code here and if it is not to much trouble.
Left by Shellzdoggie on Nov 12, 2009 8:05 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Here is the code:


public class SubfoldersCreatorFeature: SPItemEventReceiver
{
private string[] folders = new string[] {"Bing",
"Logs",
"Docs"};

public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);

SPWeb web = properties.OpenWeb();

SPDocumentLibrary prjsLib = (SPDocumentLibrary)web.Lists[properties.ListId];

if (properties.ListItem.ContentType.Name == "Project Folder"
&& properties.ListItem.Folder.ParentFolder.ToString() == prjsLib.RootFolder.ToString())
{
String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
SPFolder libFolder = prjsLib.RootFolder.SubFolders[properties.ListItem.Name];

string newFolderUrl = (web.Url + "/" + libFolder.ToString());

foreach (string folder in folders)
{
SPListItem newFolder = prjsLib.Items.Add(newFolderUrl, SPFileSystemObjectType.Folder, folder);
newFolder.Update();
}
}
}
}


Calin
Left by Calin Tatar on Nov 12, 2009 11:49 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hey Thanks allot for the code. Unfortunately I can seen to get it to work on my MOSS 2007 site everything went ok I added the feature signed and add the dll all went well but when I create a folder from content type Project Folder or create a new folder called Project Folder no sub folders? I would really like to get this to work is there something different for MOSS did you do this on WSS or MOSS?
Left by Shellzdoggie on Nov 12, 2009 1:50 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi
Did you try to add folders in the root of the list?
Calin
Left by Calin Tatar on Nov 13, 2009 12:49 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
I have deployed this on WSS and MOSS.

Calin
Left by Calin Tatar on Nov 13, 2009 12:51 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Sorry, did you add the event handler to the list (EventReceivers.Add()) in FeatureActivated ?

Calin
Left by Calin Tatar on Nov 13, 2009 12:58 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Yep I created the features and elemants xml and deployed them that all went well Not creal on what you mean
"did you add the event handler to the list (EventReceivers.Add()) in FeatureActivated ?" I created the project made the class file put the dll in the GAC made the Feature folder in the 12 hive added my XML's the deployed with stsadm they feature shows up in my portal but when I make a document library and add the Project folder conten type I created and then add the Project folders folder from the new menu it creats the sub folder put no sub folders in it. The only thing I may have messed up on is my element.xml Iam going to start over on another box and try. does this element xml look right?
Left by Shellzdoggie on Nov 13, 2009 10:17 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>Project_Folder</Name>
<Type>ItemAdded</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>Project_Folder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eda7738f1f7be2ac</Assembly>
<Class>Project_Folder.SubFoldersCreatorFeature</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>

My project DLL is Project_Folder.DLL and the class file I created is SubFoldersCreatorFeature
Left by Shellzdoggie on Nov 13, 2009 10:20 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
There are 2 ways to install a feature: by using the API with C# (that's why I referred to EventReceivers.Add()) or with xml's.
Your Elements.xml looks good.

Calin
Left by Calin Tatar on Nov 13, 2009 11:02 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Calin,
Thanks for all the assistance I very much appreciated it I finally got it to work. Thank you now I just need to figure out the next step how to have it create some template docs inside the folders.
Thanks again for Sharing your code and Helping out.
Left by Shellzdoggie on Nov 17, 2009 6:06 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi, i like this.... I would like to do it but need a little assistance in getting started.... could you tell me..... how to set this up in Visual Studio 2008 by steps? I tried to do this in designer but cant be done.

chris
Left by Chris Norris on Nov 23, 2009 8:13 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Chris,
First, you need to install WSS 3.0 extension for VS2008:

http://www.microsoft.com/downloads/details.aspx?familyid=7BF65B28-06E2-4E87-9BAD-086E32185E68&displaylang=en

After that, create a solution in VS:
-select Sharepoint as for the project type, and Empty.
Right-click on the newly create project, and under Add, select New Item, and select Event Receiver.

Calin

Left by Calin Tatar on Nov 24, 2009 11:15 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
I am a power user, not a programmer or administrator, and would like to have subfolders automatically appear whenever I create a new folder as this article tells us how to do. However, I have never created a feature before. Would you mind writing out step by step instructions about how to actually put this code in SharePoint to make this work? I would appreciate any help! Thanks.
Left by Bethany on Feb 22, 2010 5:18 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Bethany,
do you have Visual Studio installed on the server?

Calin
Left by Calin on Feb 23, 2010 10:48 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Calin:

I am working at a government site and do not have access to the IT department. I am a site owner and have been told I might be able to create this folder/sub folder structure using jquery. Do you know how I might be able to do this?

Bethany
Left by Bethany on Mar 26, 2010 4:03 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi. This work like a charm.. Thanks . I have a question. If i want to create dynamic Subfolder in a subfolder and i don't now the path. how can i get the dynamic path fo the subfolder and run code
Left by Khalid on Apr 23, 2010 8:56 AM

# Rg: How to Automatically add sharepoint sub foler when aading new folder with list's item Title and Code
Requesting Gravatar...
Hi Calin,

after using above coding I have successfully done mt task. thankx much.
can you help me out in my another task and my another task is - Automatically add sharepoint sub foler when aading new folder with list's item Title and Code
Like My Custome List Name - Department
List item Titlename - Human Resource
List item Code - Hr
when i create folder in document library, automatically create sub folder with title & code of list's item..

How to do this..
Please give me some solution like above code

Thanks
Renu
Left by Renu Sharma on Dec 21, 2010 7:05 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Renu,

You just have to check if the current item's title equals "Human Resource".

eg:

if (!properties.ListItem.Title.Equals("Human Resource"))
return;
Left by Calin on Dec 22, 2010 7:25 AM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
I am new to sharepoint. It still is not working for me. I just want to verify that my understanding is correct.

I have a doc library called formcreate.

Maybe I am understanding this wrong can you please let me know.

public class EventReceiver1 : SPItemEventReceiver
{
//folder array to create
private string[] folders = new string[] {"Documents",
"SOW",
"Cases",
"Opportunities",
"Archive"};


public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);

SPWeb web = properties.OpenWeb();
//Defining the document library
SPDocumentLibrary docLib =(SPDocumentLibrary)web.Lists[properties.ListId];

//if a new folder is created the library automatically adds the folders
if (properties.ListItem.ContentType.Name == "formcreate"
&& properties.ListItem.Folder.ParentFolder.ToString() == docLib.RootFolder.ToString())
{
String url = properties.ListItem.ParentList.RootFolder.ServerRelativeUrl.ToString();
SPFolder libFolder = docLib.RootFolder.SubFolders[properties.ListItem.Name];

string NewFolderUrl = (web.Url + "/" + libFolder.ToString());

foreach (string folder in folders)
{
SPListItem newFolder = docLib.Items.Add(NewFolderUrl, SPFileSystemObjectType.Folder, folder);
newFolder.Update();
}
}

}


Then I run debug and it brings up my sharepoint I go to my form and try to add a new folder. After I do there are no subfolders inside of it. Thanks in advance for any help!!!!

}
Left by Bill on Feb 07, 2011 3:07 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Bill,

You need to attach the event receiver to the document list, either by using a feature.xml or by using the server object model: SPDocumentLibrary.EventReceivers.Add()

Calin
Left by Calin Tatar on Mar 02, 2011 9:50 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
Hi Calin, will this code work for Sharepoint Foundatoin 2010?

When I add a new Event Receive it asks me for the following:
1) What Type of event received do you want?
2) What item should be the event source
3) Handle the following events

Could you be kind enough to let me know what items I should select for all these three to implement your code?

Thanks a lot,
Alind
Left by Alind Gupta on Feb 03, 2012 2:24 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
if (properties.ListItem.ContentType.Name == "Folder") in this place iam getting a Nullreference exception. Object reference is not set to an instance of an object ?.. what would be mistake.

Thank you.
Left by Dinesh on Apr 03, 2013 3:05 PM

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type
Requesting Gravatar...
will this code work in SharePoint 2013
Left by lorga on Aug 27, 2014 7:57 PM

Your comment:
 (will show your gravatar)


Copyright © CalinTatar | Powered by: GeeksWithBlogs.net