Exemplo n.º 1
0
        /// <summary>
        /// Delete webparts.
        /// </summary>
        /// <param name="properties"></param>
        private static void DeleteWebParts(SPFeatureReceiverProperties properties)
        {
            try
            {
                //System.Diagnostics.Debugger.Launch(); you need to use stsadm for launch debugger

                if (properties.Definition.Properties["WebPartsToDelete"] != null
                    && !String.IsNullOrEmpty(properties.Definition.Properties["WebPartsToDelete"].Value))
                {
                    using (SPWeb web = properties.GetWeb())
                    {
                        SPList list = web.GetCatalog(SPListTemplateType.WebPartCatalog);
                        if (list != null)
                        {
                            string[] wpToDelete = properties.Definition.Properties["WebPartsToDelete"].Value.Split(',');
                            IEnumerable<SPListItem> wpToDeleteList = list.Items.OfType<SPListItem>().Where(it => wpToDelete.Contains(it.Name));

                            if (wpToDeleteList.Count() > 0)
                            {
                                foreach (SPListItem toDelete in wpToDeleteList)
                                {
                                    list.Items.DeleteItemById(toDelete.ID);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Receivers.WebPartsReceiver.DeleteWebParts problem", ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Set Default MasterPage.
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="activation">if deactivation : set the original master / if activation : set the custom master.</param>
        private static void SetDefaultMasterPage(SPFeatureReceiverProperties properties, bool activation)
        {
            try
            {
                //System.Diagnostics.Debugger.Launch();

                using (SPWeb web = properties.GetWeb())
                {
                    string urlCustomMaster = "/_catalogs/masterpage/BaseAppMasterPage.master";
                    string urlDefaultMaster = "/_catalogs/masterpage/default.master";

                    string masterName = string.Empty;
                    if (activation)
                    {
                        masterName = urlCustomMaster;

                        Uri masterUri = new Uri(web.Url + masterName);

                        web.MasterUrl = masterUri.AbsolutePath;
                        web.CustomMasterUrl = masterUri.AbsolutePath;

                        web.Update();

                    }
                    else
                    {
                        masterName = urlDefaultMaster;

                        Uri masterUri = new Uri(web.Url + masterName);

                        web.MasterUrl = masterUri.AbsolutePath;
                        web.CustomMasterUrl = masterUri.AbsolutePath;

                        web.Update();

                        // if necessary remove the custom master page.
                        SPFolder catalogsFolder = web.Folders["_catalogs"];
                        if (catalogsFolder != null)
                        {
                            SPFolder masterpageFolder = catalogsFolder.SubFolders["masterpage"];
                            if (masterpageFolder != null)
                            {
                                SPFile custMPFile = masterpageFolder.Files["BaseAppMasterPage.master"];
                                if (custMPFile != null)
                                {
                                    custMPFile.Delete();
                                }
                            }
                        }

                        web.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Receivers.MasterPageReceiver.SetDefaultMasterPage problem", ex);
            }
        }
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;

            try
            {
                web = properties.GetWeb();
                // _Url = web.Site.Url;
                CustomList.CreateList(web);
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add default items to list.
        /// </summary>
        /// <param name="properties"></param>
        private static void AddListItems(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPWeb web = properties.GetWeb())
                {
                    // add default item to the BaseList
                    SPList baseList = web.Lists.TryGetList("BaseList");
                    if (baseList != null)
                    {
                        SPListItem item = baseList.Items.Add();
                        item["Title"] = "Sample 1";
                        item["Content"] = "Sample 1 Content";
                        item.Update();

                        SPListItem item2 = baseList.Items.Add();
                        item2["Title"] = "Sample 2";
                        item2["Content"] = "Sample 2 Content";
                        item2.Update();

                        SPListItem item3 = baseList.Items.Add();
                        item3["Title"] = "Sample 3";
                        item3["Content"] = "Sample 3 Content";
                        item3.Update();

                        SPListItem item4 = baseList.Items.Add();
                        item4["Title"] = "Sample 4";
                        item4["Content"] = "Sample 4 Content";
                        item4.Update();

                        SPListItem item5 = baseList.Items.Add();
                        item5["Title"] = "Sample 5";
                        item5["Content"] = "Sample 5 Content";
                        item5.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Receivers.ListsReceiver.AddListItems problem", ex);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Delete lists.
 /// </summary>
 /// <param name="properties"></param>
 private static void DeleteLists(SPFeatureReceiverProperties properties)
 {
     try
     {
         if (properties.Definition.Properties["ListsToDelete"] != null
             && !String.IsNullOrEmpty(properties.Definition.Properties["ListsToDelete"].Value))
         {
             using (SPWeb web = properties.GetWeb())
             {
                 // loop on all lists name to delete
                 // <Feature>
                 //  ...
                 //  <Properties>
                 //    <Property Key="ListsToDelete" Value="NameOfListToDelete"/>
                 //  </Properties>
                 // </Feature>
                 foreach (string filename in properties.Definition.Properties["ListsToDelete"].Value.Split(','))
                 {
                     SPList list = web.Lists.TryGetList(filename);
                     if (list != null)
                     {
                         list.Delete();
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Receivers.ListsReceiver.DeleteLists problem", ex);
     }
 }