Calin Tatar

C#.NET, VB.NET, SQL, WSS, MOSS
posts - 5, comments - 17, trackbacks - 0

My Links

News

Archives

Post Categories

C#.NET

Links

VB.NET

Automatically add Sharepoint subfolders when adding a new Folder Content Type

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

Print | posted on Sunday, June 07, 2009 4:48 AM | Filed Under [ UI Automation MOSS 2007 WSS 3.0 ]

Feedback

Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

what program is the code created in ?
7/20/2009 7:44 PM | Mike Brickles
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

Hi Mike,

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

Calin
7/21/2009 1:00 AM | Calin Tatar
Gravatar

# Mr

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.
10/6/2009 11:25 AM | Yan Herndon
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

I could paste the code here if you want.

Calin
10/8/2009 10:06 AM | Calin
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

Yes please paste the code here and if it is not to much trouble.
11/12/2009 8:05 AM | Shellzdoggie
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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
11/12/2009 11:49 AM | Calin Tatar
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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?
11/12/2009 1:50 PM | Shellzdoggie
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

Hi
Did you try to add folders in the root of the list?
Calin
11/13/2009 12:49 AM | Calin Tatar
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

I have deployed this on WSS and MOSS.

Calin
11/13/2009 12:51 AM | Calin Tatar
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

Sorry, did you add the event handler to the list (EventReceivers.Add()) in FeatureActivated ?

Calin
11/13/2009 12:58 AM | Calin Tatar
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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?
11/13/2009 10:17 AM | Shellzdoggie
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

<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
11/13/2009 10:20 AM | Shellzdoggie
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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
11/13/2009 11:02 PM | Calin Tatar
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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.
11/17/2009 6:06 AM | Shellzdoggie
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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
11/23/2009 8:13 AM | Chris Norris
Gravatar

# re: Automatically add Sharepoint subfolders when adding a new Folder Content Type

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

11/24/2009 11:15 AM | Calin Tatar
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: