0

良い一日!ドキュメントをライブラリに追加すると、ユーザーをドキュメントのパラメーターを使用して Web フォームにリダイレクトするイベント ハンドラーがあります。Web フォームでは、現在のユーザーを Checkboxlist の形式で表示します。ユーザーは適切なグループを選択し、[保存] ボタンを押します。選択したグループに応じて、ドキュメントに次の権限が割り当てられます。問題は、選択したグループに従ってドキュメントの解像度が割り当てられないことです。ハンドラー コードは次のとおりです。

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

    }

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

ドキュメントにアクセス許可を割り当てられない理由がわかりません! 助けてください!/ / Response.Write ("-" + li.Text + "<br/>");コメントアウトしたものを使用すると、チェックボックスが選択されておらず、表示されていないことがわかります。item.ResetRoleInheritance(); 実行され、グループを持たない現在のユーザーのみに権限が割り当てられます。その理由は何でしょうか?

4

1 に答える 1

0

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)
        {
            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();
        }
    }
}
于 2013-01-10T09:43:59.627 に答える