Feb 16

Did you ever try to create folders in a list from onet.xml?

Sure it looks easy as Jussi Palo shows it:

<Row>

  <Field Name='ContentTypeId'>0x0120</Field>

  <Field Name='FSObjType'>1</Field>

  <Field Name='Title'>folder title</Field>

  <Field Name='LinkTitle'>folder title</Field>

</Row>

 

But this only works if you use a SPList inheriting from a Document Library. In my project, we use Custom Lists and that tricks does not work anymore, no matter what. So in order to create some folders while creating a new site, we will write some code in a site feature receiver.

global view of the feature hierarchy

Here is the global view of our feature. It creates a list containing some animation movies.
In a real application, that kind of feature can be used to deploy a referential list that can hold only folders and manage some hierarchical data.

 

Here is our data. To keep things simple, we use an Xml containing our folders.  This is easy to modify for a non developer too.

 

data.xml

<?xml version="1.0" encoding="utf-8" ?>

<Folders>

  <Folder Title="Anime">

    <Folder Title="Pixar">

      <Folder Title="The Incredibles" />

      <Folder Title="Finding Nemo" />

      <Folder Title="Ratatouille" />

    </Folder>

    </Folder>

      <Folder Title="Studio Ghibli">

      <Folder Title="Princess Mononoke" />

      <Folder Title="Grave of the Fireflies" />

      <Folder Title="Spirited Away" />

    </Folder>

</Folders>



animeList.xml

<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

    <ListInstance

      Id="WssAnimes" Title="Animes"

      Description="Anime List Description"

      TemplateType="100" Url="Lists/Animes"

      FeatureId="00BFEA71-DE22-43B2-A848-C05709900100">

    </ListInstance>

</Elements>

 

And finally here is our feature receiver:
Remarks the use of properties.Definition.RootDirectory that returns something like : C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\FolderListFeature.

sealed class SiteFeature : SPFeatureReceiver

{

       public override void FeatureActivated(SPFeatureReceiverProperties properties)

       {

             SPSite site = properties.Feature.Parent as SPSite;

 

             using (SPWeb rootWeb = site.RootWeb)

             {

                    SPList list = rootWeb.GetList(SPUrlUtility.CombineUrl(rootWeb.Url, "Lists/Animes"));

                    ProvisionListsWithXmlData(

                           properties.Definition.RootDirectory + @"\ListData\data.xml", list);

             }

       }

 

       public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

       {

       }

 

       public override void FeatureInstalled(SPFeatureReceiverProperties properties)

       {

       }

 

       public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

       {

       }

 

       //____________________________________________________________________

       //

 

       /// <summary>

       /// Read a xml file and build an hierarchy of folders.

       /// </summary>

       private void ProvisionListsWithXmlData(string dataFile, SPList list)

       {

              XmlDocument document = new XmlDocument();

              document.Load(dataFile);

 

              XmlNode root = document.DocumentElement;

 

              string parentUrl = list.RootFolder.ServerRelativeUrl;

              SetupFolders(root.ChildNodes, list, parentUrl);

       }

 

       private static void SetupFolders(XmlNodeList nodes, SPList list, String parentUrl)

       {

              foreach (XmlNode node in nodes)

              {

                    if (node.NodeType != XmlNodeType.Element ||

                           !node.Name.Equals("Folder", StringComparison.InvariantCultureIgnoreCase)) continue;

 

                    SPListItem fold = list.Items.Add(parentUrl, SPFileSystemObjectType.Folder);

                    string folderTitle = node.Attributes["Title"].Value;

                    fold[SPBuiltInFieldId.Title] = folderTitle;

                    fold.Update();

 

                     if (node.HasChildNodes)

                           SetupFolders(node.ChildNodes, list, parentUrl + '/' + folderTitle);

              }

       }

}

 

It’s sad Microsoft does not provide a native way to create folders directly in onet.xml…

Tags:

Comments

Pinoy Teen BigBrother

Posted on Thursday, 28 May 2009 03:16


I think it was the great post you wanted,thanks for it,I totally agree about what you said.


web design

Posted on Monday, 20 July 2009 08:22

How can add a child element in a xml document?

Olivier

Posted on Monday, 20 July 2009 10:20

web design> to add a new Folder node with a Title attribute:

XmlDocument doc = new XmlDocument();
XmlElement elm = doc.CreateElement("Folder");
doc.DocumentElement.AppendChild(elm);

XmlAttribute titleAttr = doc.CreateAttribute("Title");
titleAttr.Value = "Pixar";
elm.Attributes.Append(titleAttr);

Comments are closed