Help with error creating SharePoint list (probably due to privilege issues)
- by Swami
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;
}