帮助解决创建 SharePoint 列表时出现的错误(可能是由于权限问题)

发布于 2024-09-04 23:15:30 字数 2026 浏览 7 评论 0原文

我在尝试激活 Web 部件时遇到错误。它在一种设置中可以正常激活,但在另一种设置中会失败。两者的管理员。似乎它失败了,因为它无法创建列表。错误是:消息:值不能为空。堆栈跟踪:位于 Microsoft.Sharepoint.SPRoleAssignment..ctor 在 ClientRequestHandler.CreateList(...

private static void CreateLists()
{            
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.RootWeb)
                {
                    string listName = LIST_NAME;
                    bool listExist = ContainList(web, listName);

                    if (!listExist)
                    {
                        AddFieldDelegate _delegate = new AddFieldDelegate(AddAttachmentFields);
                        SPList list = CreateList(web, listName, _delegate);
                        RegisterList(web, list, KEY);

                    }                            
                }
            }
        });
    }
    catch (Exception ex)
    {
        throw new Exception(String.Format("Message: {0} Stack Trace: {1}", ex.Message, ex.StackTrace.ToString())); 
    }

}   private static SPList CreateList(SPWeb web, string listName, AddFieldDelegate _delegate)
{
    web.AllowUnsafeUpdates = true;

    SPListTemplateType genericList = new SPListTemplateType();
    genericList = SPListTemplateType.GenericList;

    Guid listGuid = web.Lists.Add(listName, "List", genericList);

    SPList list = web.Lists[listGuid];
    list.Hidden = true;

    SPView view = _delegate(list); 

    view.Update();

    //Remove permissions from the list
    list.BreakRoleInheritance(false);

    //Make site owners the list administrators 
    SPPrincipal principal = web.AssociatedOwnerGroup as SPPrincipal;
    SPRoleAssignment assignment = new SPRoleAssignment(principal);
    assignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));
    list.RoleAssignments.Add(assignment);

    //update list changes
    list.Update();
    return list;
}

I'm getting an error when trying to activate a webpart. It activates fine in one setup , but fails in a different one. Administrator in both. Seems like it fails because it's not able to create the list. The error is: Message: Value cannot be null. Stack Trace: at Microsoft.Sharepoint.SPRoleAssignment..ctor
at ClientRequestHandler.CreateList(...

private static void CreateLists()
{            
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.RootWeb)
                {
                    string listName = LIST_NAME;
                    bool listExist = ContainList(web, listName);

                    if (!listExist)
                    {
                        AddFieldDelegate _delegate = new AddFieldDelegate(AddAttachmentFields);
                        SPList list = CreateList(web, listName, _delegate);
                        RegisterList(web, list, KEY);

                    }                            
                }
            }
        });
    }
    catch (Exception ex)
    {
        throw new Exception(String.Format("Message: {0} Stack Trace: {1}", ex.Message, ex.StackTrace.ToString())); 
    }

}   private static SPList CreateList(SPWeb web, string listName, AddFieldDelegate _delegate)
{
    web.AllowUnsafeUpdates = true;

    SPListTemplateType genericList = new SPListTemplateType();
    genericList = SPListTemplateType.GenericList;

    Guid listGuid = web.Lists.Add(listName, "List", genericList);

    SPList list = web.Lists[listGuid];
    list.Hidden = true;

    SPView view = _delegate(list); 

    view.Update();

    //Remove permissions from the list
    list.BreakRoleInheritance(false);

    //Make site owners the list administrators 
    SPPrincipal principal = web.AssociatedOwnerGroup as SPPrincipal;
    SPRoleAssignment assignment = new SPRoleAssignment(principal);
    assignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));
    list.RoleAssignments.Add(assignment);

    //update list changes
    list.Update();
    return list;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-09-11 23:15:30

确保相关网站确实有关联的所有者组(/_layouts/groups.aspx -> 设置 -> 设置组)

Make sure that the web in question actually has an associated owner group (/_layouts/groups.aspx -> Settings -> Set Up Groups)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文