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”]
Advertisements

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s