Friday, March 30, 2012

Programmatically uploading List templates to List template gallery from "File System" and creating SharePoint List based on uploaded Templates:


One of my client wants to automate the template upload process in List template gallery and create list based on that. We had all the list template stored on local file system .My job is to upload them on list template gallery  only if they are not there  and create the list on some destination web using those template.


 This can be also useful if you want to re-create any list from one site collection to another site collection on different farm. Below is my console apps that will do this job:


static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter site collecton URL:"); // Prompt                
                string siteURL = Console.ReadLine();
                Console.WriteLine("Enter Folder location of template files:"); // Prompt                
                string folderLocation = Console.ReadLine();
                SPSite osite = new SPSite(siteURL);                
                SPWeb rootWeb = osite.RootWeb;
                SPWeb referenceWeb = osite.OpenWeb("/reference");
                string strSTPFileName = string.Empty;
                string strSTPFileNameWithoutExt = string.Empty;
                SPDocumentLibrary lstListTemplateGallery = (SPDocumentLibrary)rootWeb.Lists["List Template Gallery"];
                SPFolder rootFolder = lstListTemplateGallery.RootFolder;


                string[] fileCollection = Directory.GetFiles(folderLocation);
                foreach (string strfile in fileCollection)
                {
                    int lstIndexofSlash = strfile.LastIndexOf("\\");
                    strSTPFileName = strfile.Substring(lstIndexofSlash + 1);
                    strSTPFileNameWithoutExt = strSTPFileName.Substring(0, strSTPFileName.IndexOf("."));
                    SPFile newFile = rootWeb.GetFile(rootFolder.Url + "/" + strSTPFileName);
                    if (!newFile.Exists)
                    {
                        FileStream fileStream = new FileStream(strfile, FileMode.Open);
                        SPFile spfile = rootFolder.Files.Add(strSTPFileName, fileStream, true);
                        spfile.Item["TemplateTitle"] = strSTPFileNameWithoutExt;
                        spfile.Item["Description"] = strSTPFileNameWithoutExt;
                        spfile.Item.Update();
                        Console.WriteLine(strSTPFileName + " is added successfully to list template gallery");
                    }
                    else
                    {
                        Console.WriteLine(strSTPFileName + " already exists on list template gallery");
                    }


                    SPListTemplateCollection listtempcol = rootWeb.Site.GetCustomListTemplates(rootWeb);
                    if (referenceWeb.Lists.TryGetList(strSTPFileNameWithoutExt) == null)
                    {
                        referenceWeb.Lists.Add(strSTPFileNameWithoutExt, "", listtempcol[strSTPFileNameWithoutExt]);
                        SPList olist = referenceWeb.Lists[strSTPFileNameWithoutExt];
                        olist.Update();
                        Console.WriteLine("List: " + strSTPFileNameWithoutExt + " successfully added on web," + referenceWeb.Title);
                    }
                    else
                    {
                        Console.WriteLine("List: " + strSTPFileNameWithoutExt + " already exists on web," + referenceWeb.Title);
                    }
                }
                rootWeb.Dispose();
                referenceWeb.Dispose();
                osite.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:",ex); // Prompt      
            }
            
        }

Tag: Sharepoint, List Templates.

Happy Coding :)

1 comment:

Unknown said...

Wow, thank you for this. I could not for the life of me figure out how to add a web part that checks for and if not found adds a list from an STP file. I'm still working on a "in use by another process" exception, but I'm sure that will clear up.
-Tom