Friday, November 2, 2007

Modify Members Web Part in code

To set a Members Webpart to "Show people in group" and fill in the group it has to show, use following code:

SPWeb web = GetWeb();

SPWebPartCollection webPartCollection = web.GetWebPartCollection(web.Navigation.Home.Url, Microsoft.SharePoint.WebPartPages.Storage.Shared);
                    webPartCollection.Web.AllowUnsafeUpdates = true;
                    foreach (WebPart webPart in webPartCollection) {
                        if (webPart is MembersWebPart) {
                            MembersWebPart membersWebPart = (MembersWebPart)webPart;
                            membersWebPart.DisplayType = MembersWebPartDisplayType.GroupMembership;
                            membersWebPart.MembershipGroupId = newWeb.Groups["groupName"].ID;
                            webPartCollection.SaveChanges(membersWebPart.StorageKey);
                        }
                    }
                    webPartCollection.Web.AllowUnsafeUpdates = false;

1 comment:

electronic signatures said...

I stumbled upon this topic via Google. Very interesting view on subject. Thanks for sharing.