Home > SharePoint Development > Associated Groups for a SharePoint site

Associated Groups for a SharePoint site

When setting the group associations for a SharePoint site using the object model, don’t use the obvious method of using the AssociatedMemberGroup and similar properties:

web.AssociatedMemberGroup = group;

Instead use the property bag of the SPWeb object to set each association:

web.Properties["vti_associatemembergroup"] = group.ID.ToString();

and ensure that the property bag is updated after setting these values:

web.Properties.Update();

Enter the weirdness

Why not use the SPWeb.AssociatedMemberGroup property? Simply because it appears not to work.

In a recent project, setting each of the AssociatedMemberGroup, AssociatedOwnerGroup and AssociatedVisitorGroup properties resulted in only the Owner and Visitor Groups actually retaining their values. Debugging the code showed each property being correctly set – and the read-only property AssociatedGroups also being updated – only for the associations to be lost once the owning SPWeb object went out of scope.

A quick search on this topic revealed that several blog posts contained code that set the equivalent property bag values rather than using the public properties of the SPWeb class. Alas none of the posters actually explained why they’d taken this course of action.

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: