Saturday, December 17, 2011

ListWSP

Step1: Create An Empty Sharepoint Project
Step2: Create an FolderNamed As Layouts,create a folder inside that Layouts and Paste all .stp files of Lists
Step3: Add an EventReceiver Class in Feature

Paste the following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {           
            SPWeb spWeb =(SPWeb)properties.Feature.Parent;
            string path = @"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\DRAMISCUSTOMLIST\";
            Hashtable CustomList = new Hashtable();
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists)
            {
                FileInfo[] rgFiles = di.GetFiles("*.stp");
                foreach (FileInfo fi in rgFiles)
                {
                    CustomList.Add(fi.Name, path);
                }
                if (CustomList.Count > 0)
                {
                    foreach (DictionaryEntry item in CustomList)
                    {
                        UploadTemplate(spWeb.Site, spWeb, item.Value.ToString() + item.Key.ToString(), item.Key.ToString(), "description");
                    }
                }
            }
        }

        public static void UploadTemplate(SPSite spSite, SPWeb spWeb, string CustomListStpLocalPath, string CustomLisName, string description)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (spSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        spSite.AllowUnsafeUpdates = true;
                        using (spWeb = spSite.OpenWeb())
                        {
                            spWeb.AllowUnsafeUpdates = true;
                            //get the template gallery
                            SPDocumentLibrary list = (SPDocumentLibrary)spWeb.Lists["List Template Gallery"];
                            //get the folder under which the site tempaltes reside
                            SPFolder myFolder = list.RootFolder;
                            //get the byte data of the site tempalte from the disk
                            byte[] data = File.ReadAllBytes(CustomListStpLocalPath);
                            //add the file to the folder                       
                            SPFile file = myFolder.Files.Add(CustomLisName, data, true);
                            //update the changes
                            file.Update();
                            spWeb.Dispose();
                        }
                    }
                    spSite.AllowUnsafeUpdates = false; spWeb.AllowUnsafeUpdates = false;
                });
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.GetType().Name);
            }
        }
Step4: Deploy the project to the site.

No comments:

Post a Comment

Creating Provider hosted app (sharepoint online) with local hosted IIS

The Pre-requires are as follows. 1. Office 365 Subscription 2. Visual Studio 2015 (Professional/Community/Enterprise Edition) With t...