views:

133

answers:

1

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;
}
+2  A: 

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

zincorp
This is most likely the source of the problem. According to the error, the constructor for the `SPRoleAssignment` is null, which means that the `SPPrincipal` principal is null, which means `web.AssociatedOwnerGroup` is apparently null for that setup.
ccomet
So, is there anyway to work around this if they don't have an owner's group? Or is that a prerequisite for creating lists?
Prabhu
@Swami No, in a standard situation an owner group is not necessary to create a list. Site Groups are only necessary for dealing with permissions, and a list can be created without needing to assign permissions. The reason it is blocking the creation of the list in this example is because the code never reaches the `list.Update()` statement that finalizes the creation.
ccomet
Thanks @ccomet. I actually didn't write this code so I'm wondering why that section, the one that's failing (//Make site owners the list administrators) is there in the first place. I wonder if it's because the list needs to be modified.
Prabhu
@Swami In terms of what it does, that code basically clears all permissions on that list, and associates a single entity Full Control. My guess is that this is some custom list that the web part uses to perform some data handling, and the purpose is to actually restrict normal users from being able to modify or even see that list. Why that is necessary is determined by how the web part operates.
ccomet
@ccomet, so assuming that that section of code is there for good reason, the only way this is going to work is if there is an associated owner group on the client's SP instance, right?
Prabhu
@Swami Correct, if you can confirm the target site to have an Associated Owner Group, then that error should not happen anymore.
ccomet
@ccomet So, I asked my client about the Owners group, and their response was "on the sites there are owners groups and they have Full Control on the sites."I plan to do screensharing and see it myself, but I was wondering what your thoughts were...do you think maybe they are looking in the wrong place?
Prabhu
@Swami Doubt they are looking in the wrong place... I think that for some reason something got decoupled somewhere. However, some food for thought... SPWeb.AssociatedOwnerGroup is *not* read-only. So the easiest fix would just be to programmatically assign an SPGroup to that (specifically, whichever group is the client's owner group). That's a good thing to give a shot at.
ccomet