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

时间:2010-06-10 16:48:11

标签: c# .net sharepoint sharepoint-2007 elevated-privileges

我在尝试激活webpart时遇到错误。它在一个设置中激活,但在另一个设置中失败。两者都是管理员。似乎它失败了,因为它无法创建列表。错误是:消息:值不能为空。堆栈跟踪:在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;
}

1 个答案:

答案 0 :(得分:2)

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