TPowellBAH

I know the question seems to answer itself, but I figured it wouldn't hurt to ask...

If I've created a top-level site, and for various navigation reasons, created 6 or 7 sub-sites under it, and I then want to "templatize" the whole set (top + sub-sites), so that when a new site is created off the template, they get an empty top+sub-sites. How can I do it

I'm using MOSS 2007, and don't see where the standard "Save site as template" option allows you to include sub-sites. I haven't used stsadm.exe, but in looking over the documentation, I don't see where it does quite what I want, either. Is there a third-party tool that can do this I'm also using SharePoint Designer, but that doesn't seem to offer anything additional in this regard...

Any help is appreciated!

Thanks,

Todd



Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

Euan Gamble

Hi Todd,

I would strongly recommend using stsadm. Its very handy. To template your site and all subsites you could just do the following command:

stsadm.exe -o export -url http://<yourpath> -filename <filename>.dat

and then when your importing or creating those new sites in a new location run:

stsadm.exe -o import -url http://<newpath> -filename <filename>.dat

This will include the site and subsites.

Regards,

Euan Gamble





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Thanks, Euan, for the quick reply and the command line calls. These would get the "top" site and all subsites, but what I'm really looking for is an empty new set of sites, like the templating mechanism provides (either include content or not). Unless I'm missing something in the stsadm.exe options, there is no provision for not including the list/library content in the exported sites.

As a bit more discussion...

I've seen many web posts that recommend using sub-sites as a way of organizing information -- you can have the sub-sites listed across the tabs in the Top Link Bar, and each sub-site can present its own customized Quick Launch menu. These are great capabilities, and I believe the top+sub-site architecture is very valuable.

However, if you are working on a development MOSS 2007 server, and you finally get your "application" (top site + sub-sites) configured and working as you want, you'll want to move it to the production server. Of course, you've entered test data in your development "application's" lists/libraries, and you don't want that information going into production. Normally, the "Save site as template" can be used to exclude the embedded data, but that function doesn't grab the sub-sites.

The issue for me is even a little more complicated because I'm hoping to have many of these "applications" on the SharePoint server -- one for each business area -- all of which will have the same list/library/sub-site structure, but will have distinct data. Again, the "template" mechanism would be ideal, if it included the sub-sites.

If the stsadm's export/import functionality can't exclude the content, I'm afraid I'll be left with two options:

1) use the export/import to make a copy of the development "application" in another site, manually remove all the data from it, and then use the export/import against it to have a clean data set; or

2) radically change my approach and move away from sub-sites, embedding all the lists/libraries into a single site, and creating my own navigation mechanisms within the site. I'd like to avoid this because we'd be talking about a rather customized site, and I've already seen how much of a headache it is to upgrade such a site when a new version of SharePoint is released. The more "out-of-box" use of SharePoint, the better...

Thanks,

Todd





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

lisaa

Have you considered using SharePoint Designer to do a publish I've used it before to move .aspx pages from one site to another. List data and web parts are NOT moved though. You'd have to add your web parts back to your page.

I also tried using the SharePoint Designer Backup and Restore for a site. However, mine was a site collection and couldn't get the restore to work - either the site collection wasn't created so the restore failed OR I created the site collection and had to pick a template but then the restore failed because the template was picked.





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

dink337

Start by saving each site (top level and each "different" subsite) as a template.

Next, by using the "createSite" method from the sdk, create a simple app that will allow you to create a top level site based on a specified template (top level site's not subsite's template).

Add in the functionality to loop through the site creation code as many times as there are subsites, but each time pass the new parent site's url (in this case it will be the newly created top level site) to the createSite method (which will now make each of the successive runs of the code create a "subsite") and passing in the template of each subsite.

Additionally you'll have to add code that will update the parent site's top level navigation bar and quicklaunch bar to add navigation to the subsites.

We've had to implement something similar due to the shortsighted-ness of the "save site as template" concept. Not sure why they didnt think that folks wouldn't want to save a full collection of sites (top level and associated subsites) as a template, but then I suppose thats's what the idea of "site definitions" are for...although I'm not sure just how you could solve this issue through that approach either.

At any rate, there is no built-in method to save site+subsite(s) as a site template other than developing your own "coded" solution...which really isn't too bad (it's not too much code to make it work).

-Dink





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

lisaa wrote:

Have you considered using SharePoint Designer to do a publish I've used it before to move .aspx pages from one site to another. List data and web parts are NOT moved though. You'd have to add your web parts back to your page.

Thanks, Lisaa. Because I need to provide an easy, on-the-fly site creation mechanism that, hopefully, won't involve a developer with Designer, I don't think the "publish" option will work.





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Thanks, Dink.

I guess I'm now weighing the effort of creating this "Site+" utility you outline, vs. changing my approach and putting everything in a single site. In a real sense, all of this information really should be in a single site -- the underlying sites really aren't stand-alone. However, because of the navigational functionality available by making the sub-sites, I'm not sure of the best tactic. There's a part of me that says go for the functionality and damn the framework MS wants everyone to work under. Then again, I may be asking for more headaches when it comes time for SharePoint 2009( )...

For the Site+ utility, do you know the methods to call to:

1) update the Top Link Bar of the parent site with each added sub-site; and

2) update the sub-sites so they inherit the parent's navigation

I'll also look into the site definitions a bit -- there might be something there...

Thanks again!

Todd





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

dink337

Todd,

Use the "Navigation.UseShared = true" property of the SPWeb object (the subsite you create) to make the subsite inherit it's parent site's navigation . Something like this:

Code Snippet

SPWebCollection subSites = topLevelSiteWeb.Webs;

SPWeb newSubWeb = subSites.Add(siteUrl, siteTitle, siteDescription, Convert.ToUInt32(1033), "STS#0", true, false);

newSubWeb.Navigation.UseShared = true;

Then use "SPNavigationNodeCollection" and "SPNavigationNode" to update the parent site's links:

Code Snippet

//quicklaunch links on left column of page

SPNavigationNodeCollection quickLaunchLinks = topLevelSiteWeb.Navigation.QuickLaunch;

SPNavigationNode newLink = new SPNavigationNode(siteTitle, siteUrl, false);

quickLaunchLinks[3].Children.AddAsLast(newLink); //Adds link under the "Sites" category

topLevelSiteWeb.Update();

//top nav bar

SPNavigationNodeCollection topNavLinks = topLevelSiteWeb.Navigation.TopNavigationBar;

SPNavigationNode newTopLink = new SPNavigationNode(siteTitle, siteUrl, false);

topNavLinks.AddAsLast(newTopLink);

topLevelSiteWeb.Update();

This should get you going in the right direction.

- Dink





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Thanks, I'll give it a try!



Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Hello Dink,

I've got most of this working really well -- THANKS!!!!

The last part I'm wrestling with has to do with the population of the Top Link Bar as I'm adding the subsites. Normally, when you create a subsite through the SharePoint interface, you are asked in the newsbweb.aspx page if you want the new site to use the parent's top link bar. When you indicate "Yes", you get a subsite that has the same top link entries, AND, if you click on the "tab" for the newly added subsite, it gets colored like a "tab", in that it's background color is changed to match the site's scheme so it appears "active".

This is the part I'm struggling with... When I add the top link nav nodes with the code you provided, I end up with the proper tabs along the top, but when clicked on, they don't change their background color (they do change color briefly when you hover over the tab with the mouse, but that's it). So, while they show up, and they actually work to take the user to the default.aspx page of the subsite, they don't look "active".

BTW, this also is the case when you go to one of the lists/libraries under one of the subsites -- the corresponding "tab" for the subsite doesn't look active. I know that when I added the sites manually through the newsbweb.aspx page, the tabs behaved properly.

Any ideas I've looked around the object model, but don't see anything obvious...

Thanks!!

Todd





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

dink337

Todd,

This behavior has to do with how the link is treated for the new subsite. When you call the method to add the link to the top nav bar, it takes three options in the SPNavigationNode (title, link, and is the link external). When you choose "false" for the third option, it gets treated as a relative link and basically just refreshes the top link bar when you follow the link, which is why it doesn't become highlighted. When you choose "true" for that option, it actually creates a new top link bar when the page renders after following the link...as a side-result of this, the newly created nav bar that appears on the page of the first link you follow will now have relative links for each of the other links that show up on the nav bar (aside from the inherited one for the site's top-level home page). Confused yet Smile

Basically what happens is this, if you choose "false" the tabs will not highlight when you follow them since the inherited top nav bar just refreshes.

If you choose "true", the following example will illustrate what will occur:

Top level site named "mysite".

Programmatically create two subsites, one as a "Blog", the other as a "Wiki" choosing the "true" option (making them external URL's).

First subsite "mysite/blog", second subsite "mysite/wiki".

On the toplevel site, the topnavbar will now have three tabs ("Home", "Blog", and "Wiki").

On the home page, the tab "Home" will be highlighted, and hovering over each of the other two tabs will reveal that their respective URL's are as follows:

http://<server>/mysite/blog

http://<server>/mysite/wiki

If you then click (to follow) the "Blog" tab, you will go to its home page (http://<server>/mysite/blog/default.aspx) and you'll notice the tab is actually highlighted.

Now, again hovering over each of the tabs to reveal their respective URL's, you see a distinct difference:

http://<server>/mysite/blog/blog

http://<server>/mysite/blog/wiki

If we instead (from the home page of the top level site) click (to follow) the "Wiki" tab, you will go to its home page (http://<server>/mysite/wiki/Wiki%20Pages/Home.aspx), but right off you'll notice another difference, the tab for the "Wiki" page is not highlighted - instead, the tab for the "Blog" page is highlighted (I surmised that this may be because of the order in which I programmatically created the two sites...I suspect that if I had the "Wiki" page created first the behavior would switch between the two - the wiki tab would highlight fine, but then the blog tab would not upon following it from the home page).

Hovering over the tabs now will reveal again an issue with the relative URL's to the site you're in:

http://<server>/mysite/wiki/blog

http://<server>/mysite/wiki/wiki

So, as a result the URL's for each relative site wont be correct because of how it treats them on their top nav bars. If we instead just use "False" in the option when creating the sites (as I layed out in the example code I provided you), the URL's will all be correct, but we lose the functionality that visibly displays on the top nav bar as to which page we're on.

Somehow, there is an inconsistancy between creating sites through the UI of the site and through code. There's obviously something being left out in the object model, but I havent found it yet, which is again why I always just choose the "false" option...I'm more concerned with the URL's being correct than the functionality of the tab being highlighted.

So...after thing long-winded explanation, to answer your actual question Smile, no...I'm not sure how to make the tabs function correctly.

I'm still looking though because we face the exact same thing in our application, and I'll figure it out eventually...just not yet.

- Dink





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Thanks for the quick (and very thorough ) response! Here's a question for you...

In your example, when you call the function to add the SPNavigationNodes, and you specify "true", what are you putting in for the "link" arguement Are you putting in a relative path (like "wiki" and "blog"), or are you putting in a full URL (like http://<server>/mysite/blog)

I would think the full URL, along with the "true" for external, would allow the software to get the links correct. Of course, it probably makes the site completely un-portable, as I guess it won't replace the "<server>/mysite" with whatever the new instance is...

In any event, it's just helpful to know that I'm not the only one with the problem and it isn't a matter of not understanding the technology.

Thanks again!

- Todd





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

dink337

Todd,

I've tested with using combinations of relative/false, relative/true, absolute/false, and absolute/true. Unfortunately, none of them seem to produce the results we would want.

relative/false: tabs appear with correct url but do not highlight upon visiting the page.

relative/true: tabs appear with incorrect url (detailed in previous post example).

absolute/false: errors appeared stating that the page could not be found to link to so no tabs created.

absolute/true: tabs appear with correct url but do not highlight upon visiting the page.

Both the "relative/false" and "absolute/true" options appeared to do the same thing, but using "absolute/true" added the additional side-effect of keeping the tabs for the newly created subsites on the top nav bar if I go in later and delete the sites...whereas if I instead create them as "relative" url's there is some linking in the backgound that takes place that adds the required logic to delete the tabs if I go in later and delete the subsites.

So again, there does appear to be a few goofy things with the object model in regards to programmatic site creation that still need to be ironed out...perhaps when they issue SP1 for MOSS2007/WSS 3.0 they may take this issue in account.

A possible workaround may be to not inherit the parents navigation when creating subsites, and instead manually create the links (assuming the number of subsites is managable for such an action)...for us this wouldn't work due to the creation of several hundred sites at a time, but it could for those with a smaller number.

- Dink





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

Dink,

Well, I've been able to get the tabs to show as "active" the way you'd expect after building the suite of sites programmatically...

I'm not sure your list of options (relative/false, absolute/true, etc.) listed the way I ended up doing it, and I'm not sure it's practical for all sites, but here it is.

When I created my top site (Home), I had been relying on the programmatic method of using the SPNavigationNode object and the AddAsLast() method (as you suggested), after adding the subsite to the Home site. This caused the odd non-active tab display. Instead, if I pre-created, as part of the Home site, the necessary tabs to the subsites (assuming they'd eventually be there) with paths that started way up at the top of the site hierarchy, and went down to the subsite's default.aspx page, everything worked!

An example:

Home site is at URL: http://<server>/sites/top

Home Site Top Link Bar:

Home (auto-generated and points to "/sites/top/default.aspx" and can't be changed)

Sub1 (manually-created and points to "/sites/top/sub1/default.aspx" and can be changed -- don't include http://<server>)

Sub2 (manually-created and points to "/sites/top/sub2/default.aspx" and can be changed -- don't include http://<server>)

Make a site template of the Home site, and for each of the subsites (Sub1 and Sub2). Use the SPWeb.Add() method to create the new Home and subsites instances, but don't place new tabs for them along the Home site's top link bar (they're already part of the Home site).

After doing this, the tabs are active when they are supposed to be. I don't know what this means about relative vs. absolute paths in SharePoint. I haven't tried to use the templates on a completely different server yet, to make sure they don't do something crazy If you have the ability, that would be worth doing...

As I said, I'm not sure this method can work for all sites, as it does depend on a rigid site hierarchy, and as you mentioned, doesn't clean up after itself if a subsite is removed, but if those aren't issues, then it might be satisfactory.

Thanks!

Todd





Re: SharePoint - General Question and Answers and Discussion Save a Site Template including sub-sites

TPowellBAH

OK, here's the latest...

I'm able to move away from the manually created tabs in the Home page, and instead use the programmatic methods, as before, and still see the tabs act as "active" when they should. Here's the code:

Code Snippet

<some function wrapper>

SPWeb newHomeWeb;

try
{
// create new Home website
newHomeWeb = topLevelSiteWeb.Webs.Add("HomeA", "Home A",
"this is the site's description",
Convert.ToUInt32(1033), "Home.stp",
false, false);

// don't inherit the parent's top link bar
newHomeWeb.Navigation.UseShared = false;
}
catch
{
// error logic
return false;
}


// get reference to the new Home's webs collection,
// so we can Add to it
SPWebCollection subSites = newHomeWeb.Webs;


// create Sub1 subsite
if (!CreateSubWeb(newHomeWeb, subSites, "sub1", "Sub 1", "",
"Sub1.stp", false, false, true, true))
{
// error logic
return false;
}

// create Sub2 subsite
if (!CreateSubWeb(newHomeWeb, subSites, "sub2", "Sub 2", "",
"Sub2.stp", false, false, true, true))
{
// error logic
return false;
}

return true;
</some function wrapper>


private bool CreateSubWeb(SPWeb HomeSite, SPWebCollection colSubSites,
string sWebPath, string sWebTitle,
string sWebDescription, string sTemplateName,
bool bUseUniquePermissions, bool bConvertExisting,
bool bUseShared, bool bAddToTopLinkBar)
{
try
{
SPWeb newWeb = colSubSites.Add(sWebPath, sWebTitle, sWebDescription,
Convert.ToUInt32(1033), sTemplateName,
bUseUniquePermissions, bConvertExisting);

newWeb.Navigation.UseShared = bUseShared;

if (bAddToTopLinkBar)
{
//add new subsite to the Top Link Bar (tabs) of the parent site
SPNavigationNodeCollection topNavLinks = HomeSite.Navigation.TopNavigationBar;
SPNavigationNode newTopLink = new SPNavigationNode(sWebTitle,
newWeb.ServerRelativeUrl + "/default.aspx",
false);

topNavLinks.AddAsLast(newTopLink);
HomeSite.Update();
newTopLink = null;
}

return true;
}
catch
{
return false;
}
}

By using the .ServerRelativeUrl property, we are replicating in the SPNavigationNode constructor exactly the same type of string that SharePoint uses when the site is created through the UI. AND, it looks like it even gets cleaned up properly if the subsite is then deleted.

Still don't know about moving to an entirely different server/site collection...

- Todd