Monday, August 04, 2008

Control your Sharepoint Permissions in SDK

One big problem in sharepoint is permission, allways permission, today have contribute, tomorow have read.
This is not the best policy, allways changing...., have to make rules! Talk to you client, sit 2,3,4,6 hours, 2 day... trying to understand the business, what can they see, get and put Data, Forms to be made, who are the final users, Designer... "My Kingdom" for a good designer, SharePoint could do nothing but if you have a good designer people will have apetite to prove sharepoint :).
Define all, only there you can start managing "if is a customer pre-requisite" permission.


Hope you like, and "CODE WILL THE LAST THING YOU TOUCH, ALLWAYS", not because is hard "not...", but can compromise times of develomp, and business, use other tools like Sharepoint Designer and Third tool exist in "Codeplex" and other Sites to help Configuring your site.


Here you have a litle code to add, remove group and change permissions:
Managing Permission in a ListItem
//add user
SPListItem i = fileI.Item;
i.Web.AllowUnsafeUpdates = true;
i.BreakRoleInheritance(true);
i.Web.AllowUnsafeUpdates = true;
SPRoleDefinition rd = i.Web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment ra = new SPRoleAssignment(SPUser user);
ra.RoleDefinitionBindings.Add(rd);
i.RoleAssignments.Add(ra);
i.Update();


//Change Permission Groups To read
SPListItem itemH = file.Item;
SPRoleDefinition rd = itemH.Web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment ra;
foreach (SPGroup Group in cweb.Groups)
{
ra = new SPRoleAssignment(Group);
ra.RoleDefinitionBindings.Add(rd);
itemH.RoleAssignments.Add(ra);
}

//Remove a Group
SPGroup group = cweb.Groups["Group Name"];
itemH.RoleAssignments.Remove(group);
Post a Comment