How to programmatically grant 'Read' permission to a Group/user for a particular list?.
I have created a group in Share Point manually.
Created a list too.
Now i want to add 'Read' permission to the particular list for a particular group/user.
The webpart works fine.
But does not update the permission.
Kindly help.
I am pasting the code below…
protected void Button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
{
Label4.Text = "Please Enter Some Values";
//Label4.ForeColor = System.Drawing.Color.Red;
}
else
{
SPWeb web = SPContext.Current.Web;
SPGroup group = web.SiteGroups[TextBox2.Text];
SPWebApplication webApp = web.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
web.AllowUnsafeUpdates = true;
SPRoleDefinition rDefination = web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment rAssignment = new SPRoleAssignment(group);
rAssignment.RoleDefinitionBindings.Add(rDefination);
SPList list = web.Lists[TextBox1.Text];
list.BreakRoleInheritance(true);
//SPItem item = list.
//item.RoleAssignments.Add(rAssignment);
list.Update();
Label4.Text = "Permission is successfully on item";
//Label4.ForeColor = System.Drawing.Color.Green;
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
web.RoleAssignments.Add(rAssignment);
web.Update();
web.AllowUnsafeUpdates = false;
webApp.FormDigestSettings.Enabled = true;
}
}
Best Answer
try to use this the code below. runwithelevatedprivileges will allow the simple user to update the list.