My career in technology

This is part three of my exploration of a solution I recently completed, using an event handler to programmatically create and configure a site based on form data provided by users and a site template created and managed by a SharePoint power user.

When we left off from part two, we had just gotten to this point:

        private void CheckRequirements()
        {
            // A lot of code validating the need to create the site and assign values to variables for use by the rest of the code

            //Hey look, we need to create a site!
            return true;
        }


(Links to the other parts in the series are at the bottom of the post!)

Having decided that a site does need to be created, the code returns “true” to

                //If it is passes all checks, run applicable commands
                if (CheckRequirements())
                {
                    DoAllSiteCreationSteps();
                }
        private void DoAllSiteCreationSteps()
        {
            //Make sure this runs with appropriate privileges to actually run the commands
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //ToDo #1: Create the site
                DoCreateSite();

To Do #1: Create the site. Deceptively simple…

        private void DoCreateSite()
        {
            //create the site
            LOBSite.AllWebs.Add(siteUrl, siteName, siteDescription, 1033, siteTemplate, true, false);
        }

That is it. That is the entire code needed to actually create the site from the template.

It helps that the variables were previously defined! Here is what the MS documentation tells us about SPWebCollection.Add():

public SPWeb Add (
    string strWebUrl, //A string that contains the new Web site URL
    string strTitle, //A string that contains the title
    string strDescription, //A string that contains the description
    uint nLCID, //A 32-bit unsigned integer that specifies the locale ID
    string strWebTemplate, //A string that contains the name of the site definition configuration or site template
    bool useUniquePermissions, //true to create a subsite that does not inherit permissions from another site; otherwise, false
    bool bConvertIfThere //true to convert an existing folder of the same name to a SharePoint site. false to throw an exception that indicates that a URL path with the specified site name already exists
    )

Note that the siteTemplate variable was previously set to “SiteTemplateFileName.stp”, the name of the template file stored in the Site Template Gallery.

Aaaaand, that it! Thanks for coming to the show! Hope you had a lovely time…

What?!?! What do you mean there is still more to do?

Had I been working with a site definition, that would pretty much be the end of the show. Unfortunately, or fortunately, depending on your point of view, if you remember, this project requires that the power user be able to modify the contents of the site based on changing business needs. And the whole point was to make sure that new sites don’t require a lot of manual configuration after they have been created.

Hence the decision to go with a site template.

One of the sites in the hierarchy is a configured but otherwise empty site from which the template is derived. When business needs require a change in the site contents, the power user goes into the site and makes the needed changes, creates a new template from the site, and posts the template to the Site Template Gallery (http://SharePoint/_catalogs/wt/Forms/Common.aspx) – after testing to make sure the new template does not cause problems with the event handler, of course.

The problem with this is that many pieces of the site require full URLs. That means that, without modification, new sites built from the template point back to contents of the site the template was created from. Very unhelpful.

And it means, of course, that the show must go on….

On the seventh day, He took some time off. We need to take the time off a little sooner.

                //make sure the site has finished baking before continuing
                for (int i = 0; i < 30; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                }

Apparently SharePoint releases the thread before it has finished populating the content database. I was getting inconsistent errors when testing the code, but when I stepped through the code everything worked fine. Apparently, when I stepped through the code I was slow enough to keep the errors from happening. When I added the delay, the occasional errors went away.

Next time: ToDo #2: Create the site user groups!

I’d love your feedback. Is there something you think I could be doing better? Are there questions about the code I haven’t answered? Am I full of it? Let me know! Feel free to use these code samples in accordance with my usage policy.

Check out Part One here (The Setup).
Check out Part Two here (The Decision).
You are reading Part Three.
Check out Part Four here (…They Will Come).
Check out Part Five here (Bring ‘Em All In).
Check out Part Six here (Ride the CAML!).
Check out Part Seven here (Pointing The Way).
Check out Part Eight here (Taking Part).
Check out Part Nine here (Deconfigured).
Part Ten (The Log Blog) is coming!

More posts about SharePoint.

Advertisements

Leave a Reply

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

%d bloggers like this: