简体   繁体   中英

Help with error creating SharePoint list (probably due to privilege issues)

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

确保所讨论的网站实际上具有关联的所有者组(/_layouts/groups.aspx->设置->设置组)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM