Here is my Event Receiver to redirected to my visual web part:
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
properties.Cancel = true;
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = "/SitePages/test_aspx.aspx";
}
Hi this is my visual web part:
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Web.Url);
SPWeb web = site.OpenWeb();
SPUser currentUser = web.CurrentUser;
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
// CheckBoxList1 = new CheckBoxList();
SPGroupCollection webGroups = currentUser.Groups;
CheckBoxList1.DataSource = webGroups;
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Web.Url);
SPWeb web = site.OpenWeb();
SPUser currentUser = web.CurrentUser;
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
// SPListItem thisItem = properties.ListItem;
// SPListItem employeeItem = docLib.GetItemById(ID);
// Query the second list for the item
SPQuery myQuery = new SPQuery();
myQuery.Query = "<Where><Eq><FieldRef Name = \"ID\"/>" +
"<Value Type = \"Text\">";
// Get a collection of the second list items selected by the query and pick the first (0) value as it should only return one!
SPListItemCollection secondlistItemCol = docLib.GetItems(myQuery);
SPListItem secondItem = secondlistItemCol[0];
SPGroupCollection webGroups = currentUser.Groups;
CheckBoxList1.DataSource = webGroups;
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataBind();
//Break the role inheritance from List and remove any RoleAssignments
secondItem.BreakRoleInheritance(false);
while (secondItem.RoleAssignments.Count > 0)
{
secondItem.RoleAssignments.Remove(0);
}
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected)
{
// Give permissions to a specific group
SPGroup group = web.Groups.GetByID(Convert.ToInt32(li.Value));
SPPrincipal principalGroup = group;
SPRoleAssignment roleassignment_group = new SPRoleAssignment(group);
SPRoleAssignment roleAssignment = docLib.RoleAssignments.GetAssignmentByPrincipal(principalGroup);
secondItem.RoleAssignments.Add(roleAssignment);
}
}
}
}
In a visual web part i'm getting list of group of current user. The user selects the necessary groups and presses a button save. After that assigns the rights to documents according to selected groups. How do I get the selected groups to assign permissions to a document? And how do I assign the rights to the just created document? Please help me. If there are examples show please.