Thursday, September 27, 2007

Add a new SPGroup programmatically to SharePoint

Recently I have developed a web application to interact with SharePoint. Its objective was to hide the SharePoint UI to the final users in order to they can administer the site collection without any knowledge of SharePoint. Users have rights to manage users and groups but we wanted that they did not navigate through the administration pages of the site. For that reason the web application uses the API to do all the operations. At the moment the most complex operation has been the "add new group" due his poor documentation. This is the code that I have used to accomplish this operation:

//Add the group to the SPWeb web
web.SiteGroups.Add(groupName, owner, defaultuser, description);

//Associate de group to the SPWeb. Now it will display in the quick launch bar

//Assignment of the roles.
SPRoleAssignment assignment = new SPRoleAssignment(web.SiteGroups[groupName]); SPRoleDefinition roleApp = web.RoleDefinitions["Aprobar"]; assignment.RoleDefinitionBindings.Add(roleApp);
SPRoleDefinition roleCol = web.RoleDefinitions["Colaborar"]; assignment.RoleDefinitionBindings.Add(roleCol);


Meera said...

I just wanted to make sure if it is possible to add an SPGroup which was created in a web to another web.I tried this manually and it works.Can u suggest a way to do this programmatically.How to retreive all the SPGroups existing in a site collection???


Anonymous said...

i wanna know if i have a list of courses beside it a check box i wanna know when a user check a course (each course has 2 groups)
so when he check a course his name is added to the group
so how can i add the user to an existing group programmatically???

thanx in advance

TestBest said...

You can do it programmatically aswell.
just export as xml using XmlTextWriter from sharepoint site and then once all required groups, users and permisson are exported then you can import on site you required by reading that xml using XmlTextReader.


Anonymous said...

die Ideale Antwort viagra ohne rezept erfahrungen levitra bayer [url=http//]viagra bestellen billig[/url]

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

buy levitra said...

You have good skills in web developing.

Term papers Help said...

Your blog is really helps for my search and i really like it.. Thanks a lot..:)
Term Papers Writing Services

Dennison Uy said...

Is it possible to add a SharePoint group dynamically without using programming (i.e. using templates)?

Àlex said...

Hi Dennison,
CAML does not allow to define groups. Then you have to execute some code to do it in an event handler or in the provisioning class of your site.

emi_me said...

hey check this new website It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

Online Visitors