Wednesday, March 17, 2010 3:11 PMHow do I add a folder to a list (custom)?
I had tried using:
<ListInstance TemplateType="13011" Title="ListTitle" Description="" Url="Lists/ListTitle" FeatureId="GUID"> <Data> <Rows> <Row> <Field Name="ContentTypeId">0x012000GUID</Field> <Field Name="ID">1</Field> <Field Name="Title">Root</Field> </Row> </Rows> </Data> </ListInstance>I had added custom item content type (0x0100GUID) and custom folder content type (0x012000GUID). Once feature is activated, it will create the list and insert a folder to the list. From the sample xml code above, the folder can be added but the Title of the folder is something like _.0001. Title Column value is Root which is correct but the FileLeafRef is not correct.
Is there anywhere to add a folder by feature? or do i miss out any fields? Thanks.
Monday, March 22, 2010 12:06 PM
Just a passing thought:
Why dont you try attaching a FeatureReceiver class where you can place your code to add a custom folder to the list in the ItemAdded event.
Do respond if this suits you.
- Marked As Answer by Aaron Han - MSFT Friday, April 02, 2010 7:56 AM
Monday, March 22, 2010 6:13 PM
you need to add this to the Row element:
Monday, March 22, 2010 11:18 PM
By adding the folowing code, it will add a folder but the name of the folder will be .0001 not the value that i entered.
<Field Name="ContentTypeId">0x012000GUID</Field> <Field Name="FSObjType">1</Field> <Field Name="ID">1</Field> <Field Name="Title">Root</Field>
By creating a folder by FeatureReceiver class ItemAdded event, it will works. I just want to try if using xml can create a folder without write code.
Tuesday, March 23, 2010 4:07 AM
Try also setting this field:
<Field Name='LinkTitle'>folder title</Field>
Tuesday, March 23, 2010 8:35 AM
<Field Name='BaseName'>Folder name</Field>
Regards, Vikas Patel.
Tuesday, March 23, 2010 9:17 AM
Not working for LinkTitle as this field is a readonly.
<Field Name="ContentTypeId">0x012000GUID</Field> <Field Name="FSObjType">1</Field> <Field Name="ID">1</Field> <Field Name="Title">Root2</Field> <Field Name="BaseName">Root2</Field>
It throw "Specified cast is not valid."
Tuesday, March 23, 2010 10:53 AM
yes - there were some problems with provisioning of Folders into lists using list instance. But when you export list into stp template with "Include content" option, it exports folders in the following format (I extracted manifest.xml from stp file):
<?xml version="1.0" encoding="UTF-8" ?> <ListTemplate WebUrl="..."> <Details> ... </Details> <Files> <File Name="AllItems.aspx" Src="00000000.000"> <MetaInfo> ... </MetaInfo> </File> <Folder Name="Attachments"> <MetaInfo> .... </MetaInfo> </Folder> <Folder Name="Test"> <MetaInfo> ... </MetaInfo> <Folder Name="SubTest"> <MetaInfo> ... </MetaInfo> </Folder> </Folder> </Files> ... </ListTemplate>it allows nested folders as well. So you can try to export some list with folders into stp file and copy some <Folder> tag into your ListTemplate.xml file (not ListInstance.xml). Or you can use approach with stp files provisioning. See http://stackoverflow.com/questions/1532642/create-feature-to-upload-site-template-file-stp-in-moss for details
Tuesday, March 23, 2010 12:32 PM
sorry, i quickly tested tested that on my SP 2010 box and it works there, but it doesn't on SP 2007
this is because this code executes ok on 2007:
SPListItem item = list.Items.Add(); item["BaseName"] = "fld12345"; item["FSObjType"] = 1; item.Update();
and it creates indeed a folder, but this doesn't:
SPListItem item = list.Items.Add(); item["BaseName"] = "fld12345"; item["FSObjType"] = "1"; item.Update();
and throws the invalid cast exception because it tries to cast the string value directly to SPFileSystemObjectType enumeration (this is fixed in SP 2010)
so it seems that the ListInstance feature element passes all field values as strings so it can be tricked to set the FSObjType field which is needed in this case.
Tuesday, March 23, 2010 1:55 PM
I checked the stp manifest xml file, there is also <Data> tag under the <List ...> tag. <ListTemplate> written in element.xml cannot set any child elements as written in msdn.
Found out this website that say create a folder via <listinstance> will not work in custom list. http://www.notesfor.net/post/2009/02/16/Deploy-a-Custom-SPList-with-folders-from-onetxml.aspx
Wednesday, March 31, 2010 6:47 AM
So you mean to say this approach of creating folders does not work with custom list?
Are you still looking for alternatives for achieving this or are you taking the FeatureReceiver approach now?
Wednesday, March 31, 2010 10:59 PMCreating folder in elements.xml will only work for document library but not custom list. Since there is no alternatives, i will take the featurereceiver approach. Thanks.