Visual studio 2010 无法为VisualWebPart SharePoint 2010基础上的文档设置权限

Visual studio 2010 无法为VisualWebPart SharePoint 2010基础上的文档设置权限,visual-studio-2010,sharepoint-2010,permissions,web-parts,Visual Studio 2010,Sharepoint 2010,Permissions,Web Parts,你好!我有一个事件处理程序,当您向库中添加文档时,它会使用文档的参数将用户重定向到web表单。在web表单中,它以复选框列表的形式显示当前用户。用户选择适当的组,然后按下保存按钮。以下是根据所选组为文档分配的权限。问题是文档的分辨率没有根据选定的组进行分配。以下是处理程序代码: using System; using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilit

你好!我有一个事件处理程序,当您向库中添加文档时,它会使用文档的参数将用户重定向到web表单。在web表单中,它以复选框列表的形式显示当前用户。用户选择适当的组,然后按下保存按钮。以下是根据所选组为文档分配的权限。问题是文档的分辨率没有根据选定的组进行分配。以下是处理程序代码:

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;
        }
    }

    }
使用系统;
使用System.Security.Permissions;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.Utilities;
使用Microsoft.SharePoint.Workflow;
使用System.Web;
使用System.IO;
命名空间SharePointProject3.EventReceiver 2
{
/// 
/// События элемента списка
/// 
公共类事件接收者2:SPItemEventReceiver
{
私有HttpContext\u上下文;
公共事件接收人2()
{
_context=HttpContext.Current;
}
公共覆盖无效项添加(SPItemEventProperties属性)
{
//Временно отключаем срабатывание обработчика
EventFiringEnabled=false;
//ПббббзHttpContext
HttpPostedFile文件=_context.Request.Files[0];
Stream fileStream=file.InputStream;
byte[]fileByte=新字节[file.ContentLength];
读取(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;
}
}
}
以下是Web部件的代码:

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();
         }
    }
}
使用系统;
使用系统组件模型;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.WebControl;
使用系统文本;
使用Microsoft.SharePoint.Utilities;
命名空间CustomGroupAssignment.VisualWebPart1
{
公共部分类VisualWebPart1UserControl:UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
使用(SPSite站点=新SPSite(“http://kviten:83/"))
{
使用(SPWeb=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(在CheckBoxList1.Items中列出项目li)
{
li.Selected=true;
}
尝试
{
string itemID=Page.Request.Params[“ID”];
SPDocumentLibrary doclib=(SPDocumentLibrary)web.GetList(spulutility.combineur(web.Url,“/DocLib2/Forms/AllItems.aspx”);
SPListItem=doclib.GetItemByUniqueId(新Guid(itemID));
}
捕获(例外情况除外)
{
//Выводим ошибку
}
}
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
使用(SPSite站点=新SPSite(“http://kviten:83/"))
{
使用(SPWeb=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(spulutility.combineur(web.Url,“/Test\u Doc\u Lib/”);
SPListItem=doclib.GetItemByUniqueId(新Guid(itemID));
//从列表中断开角色继承并删除所有角色分配
//项目.BreakRoleInheritance(假);
//而(item.roleasignments.Count>0)
//{
//项目。角色分配。删除(0);
//}
如果(!item.HasUniqueRoleAssignments)
{
item.ResetRoleInheritance();
item.Update();
项目.BreakRoleInheritance(假);
item.Update();
}
foreach(在CheckBoxList1.Items中列出项目li)
{
if(li.Selected)//Response.Write(“-”+li.Text+”
); { //向特定组授予权限 SPGroup group=web.Groups.GetByID(Convert.ToInt32(li.Value)); p主-主组=组; SPRoleAssignment roleassignment_group=新的SPRoleAssignment(group);
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();
        }
    }
}