I've created an event handler that fires when a new item is created in
a list, and automatically creates a new subsite based on the
parameters to the list.

This works almost fine, but I have two issues that I hope someone can
help me with.

1) Permissions
Since my subsite does not inherit permissions I need to create the
Owner, Member and Visitors groups and add the appropriate
permissionlevels. I'm having problem adding the creator to the Owner
group.

My code:
// Add current user to owner group
SPUser user = parentWeb.Users.GetByID(properties.CurrentUserId);
newWeb.AssociatedOwnerGroup.AddUser(user);
newWeb.AssociatedOwnerGroup.Update();

The code runs, and I get no error, but the user is not added to the
group.

I hope some of you might have a tip for me?

2. Navigation
I would like the subsites to be added to the parent site's top link
bar that same way that they are when they are created using the ui
wizard and you choose to display this site in the top navigation bar
of the parent area.

I'm able to create a new node in the parent TopNavigation bar, but if
I do this the link is visible to everyone and not just the users that
have access to the subsite.

My code:
// Display settings
newWeb.Navigation.UseShared = true;
// Add site to top navigation if parent does not inherit navigation
if (!newProject.ParentWeb.Navigation.UseShared)
{
SPNavigationNode node = new SPNavigationNode(newProject.Title,
newProject.ServerRelativeUrl);
SPNavigationNode addedNode =
newProject.ParentWeb.Navigation.TopNavigationBar.AddAsLast(node);
addedNode.IsVisible = true;
addedNode.Update();
}
newProject.Update();

Any help is much appreciated.

Regards
Elin Kolloen

Re: Programmatically add a wss subsite using the object model - by Mike

Mike
Mon Aug 06 10:09:49 CDT 2007

The best place to post programming questions is to the newsgroup
microsoft.public.sharepoint.development_and_programming which
specialises in them.

That's were the programming experts are and also where there are
probably existing threads which will be useful for you.

It covers all SharePoint products so please say exactly what you are
running when posting there.

Mike Walsh
WSS FAQ http://www.wssfaq.com
no questions by e-mail please



Elin wrote:
> I've created an event handler that fires when a new item is created in
> a list, and automatically creates a new subsite based on the
> parameters to the list.
>
> This works almost fine, but I have two issues that I hope someone can
> help me with.
>
> 1) Permissions
> Since my subsite does not inherit permissions I need to create the
> Owner, Member and Visitors groups and add the appropriate
> permissionlevels. I'm having problem adding the creator to the Owner
> group.
>
> My code:
> // Add current user to owner group
> SPUser user = parentWeb.Users.GetByID(properties.CurrentUserId);
> newWeb.AssociatedOwnerGroup.AddUser(user);
> newWeb.AssociatedOwnerGroup.Update();
>
> The code runs, and I get no error, but the user is not added to the
> group.
>
> I hope some of you might have a tip for me?
>
> 2. Navigation
> I would like the subsites to be added to the parent site's top link
> bar that same way that they are when they are created using the ui
> wizard and you choose to display this site in the top navigation bar
> of the parent area.
>
> I'm able to create a new node in the parent TopNavigation bar, but if
> I do this the link is visible to everyone and not just the users that
> have access to the subsite.
>
> My code:
> // Display settings
> newWeb.Navigation.UseShared = true;
> // Add site to top navigation if parent does not inherit navigation
> if (!newProject.ParentWeb.Navigation.UseShared)
> {
> SPNavigationNode node = new SPNavigationNode(newProject.Title,
> newProject.ServerRelativeUrl);
> SPNavigationNode addedNode =
> newProject.ParentWeb.Navigation.TopNavigationBar.AddAsLast(node);
> addedNode.IsVisible = true;
> addedNode.Update();
> }
> newProject.Update();
>
> Any help is much appreciated.
>
> Regards
> Elin Kolloen
>