Thursday, October 22, 2009

Creating Custom List from List Template through feature in sharepoint

Elements.xml file:


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="KM" Url="_catalogs/lt" RootWebOnly="TRUE">
    <File Url="Knowledge Management.stp" Type="GhostableInLibrary" >
      <Property Name="Name" Value="Knowledge Management"/>
      <Property Name="Title" Value="Knowledge Management"/>
    </File>
    <File Url="Team Details.stp" Type="GhostableInLibrary" >
      <Property Name="Name" Value="Team Details"/>
      <Property Name="Title" Value="Team Details"/>
    </File>
    <File Url="Team Leave.stp" Type="GhostableInLibrary" >
      <Property Name="Name" Value="Team Leave"/>
      <Property Name="Title" Value="Team Leave"/>
    </File>
    <File Url="Training Plan.stp" Type="GhostableInLibrary" >
      <Property Name="Name" Value="Training Plan"/>
      <Property Name="Title" Value="Training Plan"/>
    </File>
  </Module>
</Elements>



Feature Activation code:


 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //System.Diagnostics.Debugger.Launch();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(properties.Feature.Definition.RootDirectory + "/ListInstance.xml");
            XmlElement xmlElement = xmlDocument.DocumentElement;
            XmlNodeList node = xmlElement.ChildNodes;
            using (SPWeb web = (SPWeb)properties.Feature.Parent)
                {
                    //SPWeb web = site.OpenWeb();
                    foreach (XmlNode node1 in node)
                    {
                        SPListTemplate template;
                        SPListTemplateCollection templateList = web.Site.GetCustomListTemplates(web);
                        if (templateList != null && templateList.Count > 0)
                        {
                            template = templateList[node1.Attributes[0].Value];
                        }
                        else
                        {
                            throw new Exception(string.Format("No custom lists found for {0}", web));
                        }

                        if (template != null)
                        {
                            web.Lists.Add(node1.Attributes[0].Value, node1.Attributes[0].Value, template);
                            //web.Lists.Add("Teams TSL", "Teams TSL", template);
                            //web.Lists.Add("Teams Toezicht", "Teams Toezicht", template);
                            web.Update();
                            SPList list=web.Lists[node1.Attributes[0].Value];
                            list.OnQuickLaunch=true;
                            list.Update();
                        }
                        else
                        {
                            throw new Exception("No template to add");
                        }
                    }
                }
           
            //throw new NotImplementedException();
        }

No comments:

Post a Comment