Add RoleAssignment to listitem.

Please visit this : Samsung Service Galaxy S

This is continuing the previous post. After you create the list item impersonating the current logged in user in the sharepoint site you might want to give that user only the right to read it.
After oListItem.Update(); you must run the code to do this with elevated Privileges to assign the role.

SPUser user = oWebsiteRoot.SiteUsers.GetByID(web.CurrentUser.ID);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(SPControl.GetContextSite(this.Context).ID);
SPWeb w = site.OpenWeb();
Guid g = oListItem.UniqueId;
oListItem = w.Lists["Announcements"].GetItemByUniqueId(g);

oListItem is the listitem created in the previous post

I tried to set the RoleInheritence of the list item to false: oListItem.BreakRoleInheritance(false);
but i had some problems with it. I got the error:
“Updates are currently disallowed on GET requests.”

oListItem.BreakRoleInheritance(false) sets the spweb.AllowUnsafeUpdates to false; so after this you must set it true manually again even though we did that in the code before this (in the previous post) :

oListItem.BreakRoleInheritance(false);
w.AllowUnsafeUpdates = true;
w.Update();

Now to set the roles:

SPRoleDefinitionCollection webroledefinitions = w.RoleDefinitions;

SPRoleAssignment roleassignment = new SPRoleAssignment(user);

roleassignment.RoleDefinitionBindings.Add(webroledefinitions["Read"]);
oListItem.RoleAssignments.Add(roleassignment);
oListItem.Update();

});

And now every user sees the the items created by themselves.

Commerce Server 2009
By Ing. Msc. Dan Gheorghe

Advertisements