Calin Tatar

C#.NET, VB.NET, WPF, SILVERLIGHT, SQL, WSS, MOSS
posts - 5, comments - 27, 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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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
Gravatar

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

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.
2/22/2010 5:18 AM | Bethany
Gravatar

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

Hi Bethany,
do you have Visual Studio installed on the server?

Calin
2/23/2010 10:48 PM | Calin
Gravatar

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

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
3/26/2010 4:03 PM | Bethany
Gravatar

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

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
4/23/2010 8:56 AM | Khalid
Gravatar

# Rg: How to Automatically add sharepoint sub foler when aading new folder with list's item Title and Code

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
12/21/2010 7:05 PM | Renu Sharma
Gravatar

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

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;
12/22/2010 7:25 AM | Calin
Gravatar

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

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!!!!

}
2/7/2011 3:07 PM | Bill
Gravatar

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

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
3/2/2011 9:50 PM | Calin Tatar
Gravatar

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

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
2/3/2012 2:24 PM | Alind Gupta
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: