Esempio n. 1
0
        /// <summary>
        /// Display all active Web Parts to the Web Part grid for the specified Web Application
        /// </summary>
        private void DisplayActiveWebParts(SPWebApplication webApp)
        {
            try
            {
                // Process search criteria
                bool   includeSubSites           = wp_type_subsites.Checked;
                bool   includeSharePointWebParts = wp_type_SharePoint2007.Checked;
                bool   includeCustomWebParts     = wp_type_custom.Checked;
                string urlFilter     = wp_url_search.Text == "<Site Collection / Site>" ? string.Empty : wp_url_search.Text.Trim();
                string keywordFilter = wp_webpart_search.Text == "<Web Part Search>" ? string.Empty : wp_webpart_search.Text.Trim().ToLower();

                try
                {
                    // Validate URL filter format
                    if (urlFilter != string.Empty)
                    {
                        new Uri(urlFilter);
                    }
                }
                catch (UriFormatException)
                {
                    MessageBox.Show("Please enter a valid URL filter", "URL Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (urlFilter == string.Empty || includeSubSites)
                {
                    DialogResult dialogResult = MessageBox.Show("Your search criteria may generate a large number of results, do you want to continue?", "Search Criteria Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (dialogResult != DialogResult.Yes)
                    {
                        return;
                    }
                }

                collectionLabel.Text = "Total Web Parts: Loading...Please Wait";
                this.Invalidate();
                this.Update();

                WebPartUtilities        webpartUtilities = new WebPartUtilities();
                List <WebPartToDisplay> allWebParts      = new List <WebPartToDisplay>();
                if (urlFilter != string.Empty)
                {
                    // Apply a URL filter
                    using (SPSite site = new SPSite(urlFilter))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            allWebParts = webpartUtilities.GetAllWebPartsInWeb(web, includeSubSites, includeSharePointWebParts, includeCustomWebParts);
                        }
                    }
                }
                else
                {
                    // No URL filter, go through each site collection
                    foreach (SPSite site in webApp.Sites)
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            allWebParts.AddRange(webpartUtilities.GetAllWebPartsInWeb(web, includeSubSites, includeSharePointWebParts, includeCustomWebParts));
                        }
                        site.Dispose();
                    }
                }

                if (!webPartGrid.Columns.Contains("Zone"))
                {
                    webPartGrid.Columns.Add("Zone", "Zone");
                }
                if (!webPartGrid.Columns.Contains("WebPartTitle"))
                {
                    webPartGrid.Columns.Add("WebPartTitle", "Title");
                }
                if (!webPartGrid.Columns.Contains("Description"))
                {
                    webPartGrid.Columns.Add("Description", "Description");
                }

                foreach (WebPartToDisplay webpartToDisplay in allWebParts)
                {
                    if (keywordFilter != string.Empty)
                    {
                        if (!webpartToDisplay.Title.ToLower().Contains(keywordFilter) &&
                            !webpartToDisplay.Type.ToLower().Contains(keywordFilter))
                        {
                            continue;
                        }
                    }

                    int             rowIndex = webPartGrid.Rows.Add();
                    DataGridViewRow row      = webPartGrid.Rows[rowIndex];
                    row.Cells["webURL"].Value       = webpartToDisplay.PageUrl;
                    row.Cells["WebPart"].Value      = webpartToDisplay.Type;
                    row.Cells["webType"].Value      = webpartToDisplay.Category;
                    row.Cells["WebScope"].Value     = webpartToDisplay.Visible ? "Visible" : "Hidden";
                    row.Cells["Description"].Value  = webpartToDisplay.Description;
                    row.Cells["Zone"].Value         = webpartToDisplay.Zone;
                    row.Cells["WebPartTitle"].Value = webpartToDisplay.Title;
                }

                webPartGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                collectionLabel.Text = "Total Web Parts: " + (webPartGrid.Rows.Count - 1);
            }
            catch (Exception ex)
            {
                collectionLabel.Text = "Total Web Parts: 0";
                if (DEBUG_MODE.Checked)
                {
                    MessageBox.Show("Error in DisplayActiveWebParts:\n" + ex.ToString());
                }
            }
        }