How to create a new site based on a custom site template

Here is a method to create a sub site based on a custom site template.  You need to pass in your new site name and the name of the site template.  It will check the available site templates for the one you specified (so you must deploy the site template first).  If it’s not there an error gets displayed.  I put a label on my web part to display the error _lblErrMsg.  You will need that control (or you can remove it and handle errors whatever way you want.

private bool CreateSubSite(string newSiteName, string ProjectsiteTemplateName)
              SPWeb webSite = SPContext.Current.Web;

              webSite.AllowUnsafeUpdates = true;

              //Get all the site templates
              SPWebTemplateCollection Templates = webSite.GetAvailableWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));

              //Get the specific project template
              SPWebTemplate siteTemplate = null;

              //Verify the custom site template exists
              if (templateExists(Templates, ProjectsiteTemplateName))
                  siteTemplate = Templates[ProjectsiteTemplateName];
                  _lblErrMsg.Text = "Could not find the custom site template.";

              if (siteTemplate != null)

                  SPWeb newWeb; 
                  newWeb = SPContext.GetContext(HttpContext.Current).Web.Webs.Add(
                              "Project Site",
                              LOCALE_ID_ENGLISH, siteTemplate,
                              true, false);

                  //Inherit navigation from parent site
                  newWeb.Navigation.UseShared = true;


                  siteCreated = true;

                  webSite.AllowUnsafeUpdates = false;



          catch (Exception ex)
              this._lblErrMsg.Text = ex.Message.ToString();

          return siteCreated;
[tweetmeme only_single=”false”]

How to add a content editor web part to a SharePoint page

Here is some code to add a content editor web part to a page. So if you are creating a page in code (like during feature activation), then you put a content editor web part on the page.

Setting the InnerText (contentXMLElement.InnerText = “”;) will set the text in the content editor, incase you want something to be there by default.  User HTML.

using (SPWeb webSite = SPContext.Current.Site.OpenWeb(SiteToOpen))
using (SPLimitedWebPartManager mgr = webSite.GetFile(“default.aspx”).GetLimitedWebPartManager(PersonalizationScope.Shared))
if (mgr != null)
# region AddNewLink

ContentEditorWebPart cewp = new ContentEditorWebPart();
cewp.AllowClose = false;
cewp.AllowEdit = false;
cewp.AllowHide = false;
cewp.AllowMinimize = false;
cewp.ID = “ContentEditorWP”;
cewp.Title = “Content Editor Web Part”;
cewp.ChromeType = PartChromeType.None;

//Add content to the content editor web part
XmlDocument addNewXMLDoc = new XmlDocument();
XmlElement contentXMLElement = addNewXMLDoc.CreateElement(“Root”);
contentXMLElement.InnerText = “”;
cewp.Content = contentXMLElement;
cewp.Content.InnerText = contentXMLElement.InnerText;

// add the web part.
// first argument: web part object
// second argument: zone
// third argument: index (location within the zone)
mgr.AddWebPart(cewp,”left”, 0);

# endregion


Tips to speed up Firefox

Who wants to slooowly surf the web.
Firefox 3.x works great out of the box, but here are some configuration changes to speed it up (browsing and startup).

1) Turn on TraceMonkey (Javascript complier).

Type about:config in the Address bar and press Enter.
In the filter textbox, type
Double-click on the value to turn it on (true)

Here is the link where I found this information.



2) Increase you piplines (increases the number of concurrent requests to the server)

Type about:config in the Address bar and press Enter.
Type network.http.pipelining and network.http.proxy.pipelining in the Filter field.
Under the Filter field the preferences should appear . Click on them and set them to
Type network.http.pipelining.maxrequests in the Filter field. Under the Filter field the preference should appear. Click on it and set the value to 10 or to whatever value to wish (anyway no more than 30). 


   Here is the link where I found this information.



[tweetmeme only_single=”false”]

How to add a row click event to a gridview

So I ran across the problem of I need a web page to do something if a user clicked a row in a grid.
It wasn’t going to work for the user to click a button, the whole row had to be clickable.

Adding a row data bound event solved the problem.
I also change the background color on mouse over (and mouse out).

When the user clicks the row they get redirected to a page (with a query string parm so I know what row they clicked on).


   1: protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)

   2: {

   3:     if (e.Row.RowType == DataControlRowType.DataRow)

   4:     {

   5:         e.Row.Attributes.Add("onmouseover", "'#ceedfc'");

   6:         e.Row.Attributes.Add("onmouseout", "''");

   7:         e.Row.Attributes.Add("style", "cursor:pointer;");

   8:         e.Row.Attributes.Add("onclick", "location='detail.aspx?id=" + e.Row.Cells[0].Text + "'");

   9:     }

  10: }

Need to run jQuery and Mootools side by side

You can run both jQuery and Mootools libraries on the same site, but there is a trick.

Add the link jQuery.noConflict(); to that last line in your jQuery js libary (jquery-1.3.2.min.js)

This will change the namespace that jQuery uses from $ to jQuery.  This way jQuery and Mootools won’t step all over each other.

You do have to change all your jQuery functions to use jQuery instead of $.

I assume this works also if you trying to integrate jQuery with the ASP.NET Ajax libraries, but I have not tested it in that scenario.

Here is a link explaining in more detail.

Eliminate the Modal popup flicker

One problem with Ajax modal popup is that when the page first loads up whatever is on the popup panel will flicker on the screen.
The slower your pc or connection is the more pronounced the flicker.

After doing some research, I found the answer on this blog (it has an example).  It turns out to be really simple.  Just add style=”display:none” on your pop panel.

Poof, no more flicker.

Browser Compatibility Check for IE versions

When doing web development you rarely have a one browser to support.  Having to test with multiple browsers is hard and even more difficult with different versions of the same browsers.  Now there is a solution for different versions of IE.  I used to keep different VM’s with
different versions of IE.  That was quite a pain.

There is now a tool that will allow you to view you web pages in different versions of IE (5.5, 6, 7, and 8).