Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9127621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:19:25+00:00 2026-06-17T07:19:25+00:00

Good day! I have an event handler that, when you add a document to

  • 0

Good day! I have an event handler that, when you add a document to the library redirects the user to a web form with the parameters of the document. In the web form, it displays the current user in the form of Checkboxlist. The user selects the appropriate group, and he presses the Save button. Following are assigned permissions to the document according to the selected group. The problem is that the resolution of the document is not assigned according to selected groups. Here’s the handler code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Web;
using System.IO;
namespace SharePointProject3.EventReceiver2
{
    /// <summary>
    /// События элемента списка
    /// </summary>
    public class EventReceiver2 : SPItemEventReceiver
    {
    private HttpContext _context;
    public EventReceiver2()
    {
        _context = HttpContext.Current;
    }
    public override void ItemAdding(SPItemEventProperties properties)
    {
        //Временно отключаем срабатывание обработчика
        EventFiringEnabled = false;
        //Получаем файл из HttpContext
            HttpPostedFile file = _context.Request.Files[0];
            Stream fileStream = file.InputStream;
            byte[] fileByte = new byte[file.ContentLength];
            fileStream.Read(fileByte, 0, file.ContentLength);
            //Загружаем файл в библиотеку документов
            SPFile fileUploded = properties.Web.Files.Add(properties.AfterUrl, fileByte);
            //Включаем обработчик обратно
            EventFiringEnabled = true;
            //Отменяем добавление файла, которое делал пользователь
            properties.Cancel = true;
            properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
            //Деламе редирект
            properties.RedirectUrl = properties.Web.Url + "/test_perm/default.aspx?ID=" + fileUploded.UniqueId;
        }
    }

    }

And here’s the code of the Web Part:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using Microsoft.SharePoint.Utilities;
namespace CustomGroupAssignment.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite("http://kviten:83/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPUser currentUser = web.CurrentUser;
                    SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
                    SPGroupCollection webGroups = currentUser.Groups;
                    CheckBoxList1.DataSource = webGroups;
                    CheckBoxList1.DataValueField = "ID";
                    CheckBoxList1.DataTextField = "Name";
                    CheckBoxList1.DataBind();
                    foreach (ListItem li in CheckBoxList1.Items)
                    {
                        li.Selected = true;
                    }
                    try
                    {
                        string itemID = Page.Request.Params["ID"];
                        SPDocumentLibrary doclib = (SPDocumentLibrary)web.GetList(SPUrlUtility.CombineUrl(web.Url, "/DocLib2/Forms/AllItems.aspx"));
                        SPListItem item = doclib.GetItemByUniqueId(new Guid(itemID));
                     }
                   catch (Exception ex)
                    {
                        //Выводим ошибку
                    }

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite("http://kviten:83/"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPUser currentUser = web.CurrentUser;
                    SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
                      try
                    {
                        string itemID = Page.Request.Params["ID"];
                        SPDocumentLibrary doclib = (SPDocumentLibrary)web.GetList(SPUrlUtility.CombineUrl(web.Url, "/Test_Doc_Lib/"));
                        SPListItem item = doclib.GetItemByUniqueId(new Guid(itemID));
                        //Break the role inheritance from List and remove any RoleAssignments
                        //item.BreakRoleInheritance(false);
                        //while (item.RoleAssignments.Count > 0)
                        //{
                        //    item.RoleAssignments.Remove(0);
                        //}
                        if (!item.HasUniqueRoleAssignments)
                        {
                            item.ResetRoleInheritance();
                            item.Update();
                            item.BreakRoleInheritance(false);
                            item.Update();
                        }
                            foreach (ListItem li in CheckBoxList1.Items)
                        {
                            if (li.Selected) //Response.Write("- " + li.Text + "<br/>");
                            {
                                // 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 = item.RoleAssignments.GetAssignmentByPrincipal(principalGroup);
                                item.RoleAssignments.Add(roleAssignment);
                                item.Update();
                            }
                        }
                   }

                   catch (Exception ex)
                  {
                        //Выводим ошибку
                   }
                    Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                    Context.Response.Flush();
                    Context.Response.End();
                }
            }
        }
            protected void btnCancel_Click(object sender, EventArgs e)
        {
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
            Context.Response.Flush();
            Context.Response.End();
         }
    }
}

I can not understand why I cannot assign permissions to the document! Help please!
If you use the / / Response.Write ("-" + li.Text + "<br/>"); which commented out, we can see that the checkboxes are not selected and not displayed. item.ResetRoleInheritance(); executed and permission assigned to only the current user with no groups. In what could be the reason?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:19:27+00:00Added an answer on June 17, 2026 at 7:19 am

    The right solution for a web part:

    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Text;
    using Microsoft.SharePoint.Utilities;
    namespace CustomGroupAssignment.VisualWebPart1
    {
        public partial class VisualWebPart1UserControl : UserControl
        {
               protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) { 
                using (SPSite site = new SPSite("http://kviten:83/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPUser currentUser = web.CurrentUser;
                        SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
                        SPGroupCollection webGroups = currentUser.Groups;
                        CheckBoxList1.DataSource = webGroups;
                        CheckBoxList1.DataValueField = "ID";
                        CheckBoxList1.DataTextField = "Name";
                        CheckBoxList1.DataBind();
                        foreach (ListItem li in CheckBoxList1.Items)
                        {
                            li.Selected = true;
                        }
                        try
                        {
                            string itemID = Page.Request.Params["ID"];
                            SPDocumentLibrary doclib = (SPDocumentLibrary)web.GetList(SPUrlUtility.CombineUrl(web.Url, "/DocLib2/Forms/AllItems.aspx"));
                            SPListItem item = doclib.GetItemByUniqueId(new Guid(itemID));
                        }
                        catch (Exception ex)
                        {
                            //Выводим ошибку
                        }
                    }
                    }
                }
            }
    
               protected void Button1_Click(object sender, EventArgs e)
               {
                   using (SPSite site = new SPSite("http://kviten:83/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPUser currentUser = web.CurrentUser;
                        SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Test_Doc_Lib"];
                            string itemID = Page.Request.Params["ID"];
                            SPDocumentLibrary doclib = (SPDocumentLibrary)web.GetList(SPUrlUtility.CombineUrl(web.Url, "/Test_Doc_Lib/"));
                            SPListItem item = doclib.GetItemByUniqueId(new Guid(itemID));
                   foreach (ListItem li in CheckBoxList1.Items)
                   {
                       if (li.Selected) //Response.Write("- " + li.Text + "<br/>");
                       {
                                    if (!item.HasUniqueRoleAssignments)
                                    {
                                        item.ResetRoleInheritance();
                                        item.Update();
                                    }
                                    item.BreakRoleInheritance(false);
                                    item.Update();
                                    SPGroup group = web.Groups.GetByID(Convert.ToInt32(li.Value));
                                    SPPrincipal principalGroup = (SPPrincipal)group;
                                    //SPRoleAssignment roleassignment_group = new SPRoleAssignment(principalGroup);
                                    SPRoleAssignment roleAssignment = item.Web.RoleAssignments.GetAssignmentByPrincipal(principalGroup);
                                    item.RoleAssignments.Add(roleAssignment);
                                    item.Update();
                                   // var roleAssignment = new SPRoleAssignment(principalGroup);
                                  //  item.RoleAssignments.Add(roleAssignment);
                                   // item.Update();
                                    }
                            }
                   Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                   Context.Response.Flush();
                   Context.Response.End();
                       }
                   }
               }
    
            protected void btnCancel_Click(object sender, EventArgs e)
            {
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day I have a custom TextBox that has a IndicatorTextBox.ui.xml file as well
Good day, I have a class that implements the LoaderCallbacks, and hence have the
Good Day, I have a simple working routine in Perl that swaps two words:
Good Day, I have a Hibernate mapping that looks something like this: public class
Good day, I have created an object that will manage data access. My app
Good day all, I have an html file as follows, which displays a Google
Good day, I have a ASP.net MVC app that needs to upload files to
Good day, everyone! I have here a program that sorts 50,000 words from a
Good day to all. I have a problem with the following code: textBox.addEventListener(KeyboardEvent.KEY_DOWN,handler); function
Good day all, I have a preset situation that can't be changed, that revolves

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.