/// <summary> /// Adds new web parts to the default home page. /// </summary> /// private void AddWebParts(SPWeb CurrentWeb) { try { /* Retrieve an instance of the default.aspx as a SPFile object. */ this._homePage = CurrentWeb.GetFile("default.aspx"); /* Retrieve an instance of the SPLimitedWebPartManager object. */ SPLimitedWebPartManager wpm = this._homePage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); /* Create an instance ofthe AsyncWebPart1 object. */ this._asyncWP = new AsyncWP1(); this._asyncWPExists = false; /* Loop through the web part manager to see if the web part exists. */ foreach (WebPart webPart in wpm.WebParts) { if (webPart.GetType().FullName == this._asyncWP.GetType().FullName) { _asyncWPExists = true; break; } } /* Add the web part to the page if not found on the page. */ if (this._asyncWPExists == false) { this._asyncWP.Title = "Asynchronous Web Part Example (SPDudes)"; this._asyncWP.ToolTip = "Asynchronous Web Part Example (SPDudes)"; wpm.AddWebPart(this._asyncWP, "left", 0); } } catch (Exception ex) { throw; } }
/// <summary> /// Removes web parts that exists on the home page. /// </summary> private void RemoveWebparts(SPWeb CurrentWeb) { try { /* Retrieve an instance of the default.aspx page. */ this._homePage = CurrentWeb.GetFile("default.aspx"); /* Retrieve an instance of the web part manager on the home page. */ SPLimitedWebPartManager wpm = this._homePage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); /* Location the AsyncWP1 web part. */ this._asyncWPExists = false; this._asyncWP = new AsyncWP1(); foreach (WebPart webPart in wpm.WebParts) { if (webPart.GetType().FullName == this._asyncWP.GetType().FullName) { this._asyncWPExists = true; break; } } if (_asyncWPExists) { wpm.DeleteWebPart(this._asyncWP); } } catch (Exception ex) { throw; } }