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)
      {
          try
          {
              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];
              }
              else
              {
                  _lblErrMsg.Text = "Could not find the custom site template.";
              }

              if (siteTemplate != null)
              {

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

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

                  newWeb.Update();

                  siteCreated = true;

                  webSite.AllowUnsafeUpdates = false;

              }

              webSite.Close();

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

          return siteCreated;
      }
[tweetmeme only_single=”false”]

How to check if a project template exists

If you are creating sites based off custom project templates through code you should if the project template exists.  If you don’t and you try then SharePoint will through an error.

Here is a simple method that will return true or false if your project template exists.  You pass in the name of the project template you are looking for.

//Check if a site template already exists
        private static bool templateExists(string title)
        {
            using (SPWeb webSite = SPContext.Current.Web)
            {
                SPWebTemplateCollection collection = webSite.GetAvailableWebTemplates(Convert.ToUInt32(1033));

                foreach (SPWebTemplate list in collection)
                {
                    if (list.Title.ToLower() == title.ToLower())
                    {
                        return true;
                    }
                }
            }
            return false;
        }
[tweetmeme only_single=”false”]

How to create sub sites in SharePoint 2007

Here is some code to create a subsite in SharePoint.
The important part to remember is to set AllUnsafeUpdates (and unset), otherwise it won’t work.

try
{
    SPWeb webSite = SPContext.Current.Web;

    webSite.AllowUnsafeUpdates = true;

        //Add subsite site
        webSite.Webs.Add(<WEBURL>, <TITLE>, <DESCRIPTION>, LOCALE_ID_ENGLISH, <SITE TEMPLATE>, true, false);

    webSite.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
    throw ex;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

SharePoint Error tab in navigation

If you notice your navigation has gone away and you have a tab called “Error:”, you have a problem.  You may also notice that authenticated users can’t navigate your site.

This is caused when you have setup the Site Collection output cache with “Page Layouts can use a different page output cache profile”.

Here is an another article explaining this problem in more detail.

http://www.sharepoint-tips.com/2007/09/wrokaround-error-in-navigation-when.html

Performance Point Server, approval’s don’t work

When you create an approval workflow in Performance Point, it appears to work.  Unfortunately it doesn’t.  When a user submits an excel template, to an approval it actually save the data to the database.  You can the record in your fact table to see what happens.

This is too bad that doesn’t work.  This one of those easy pieces of functionality. that sells the whole product..