none
Object reference error when try to break role inheritance for Tasks Lits in a sub site RRS feed

  • Question

  • hi,

    i am trying to read the Tasks list in my sub site programmatically and trying to break the role  inheritance and apply contribute permissions to a set of  users.

    but when i

    private bool ApplyContriToTasklistforSubSite(string strBIFNodePathhURL, IList<SPUser> spuserCollecApprovers)
            {

              bool  isPermiApplied =false;
              try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite subNodeSite = new SPSite(strBIFNodePathhURL))
                        {
                            using (SPWeb subNodeWeb = subNodeSite.OpenWeb())
                            {
                                subNodeWeb.AllowUnsafeUpdates = true;

                                SPList splistTasksList = subNodeWeb.Lists.TryGetList("Tasks");

                                if (!splistTasksList.HasUniqueRoleAssignments)
                                splistTasksList.BreakRoleInheritance(true);

                                foreach(SPUser singleeUser in spuserCollecApprovers)
                                {
                                    //SPGroup _singleWebCurentSelectedGroup = subNodeSiteforCTypeWeb.SiteGroups[currSelectedGroupNameforCType];
                                    SPRoleAssignment _singleWebRoleAssignment = new SPRoleAssignment(singleeUser);
                                    SPRoleDefinition _singleTaskRoleDefinition = subNodeWeb.RoleDefinitions["Contribute"];
                                    _singleWebRoleAssignment.RoleDefinitionBindings.Add(_singleTaskRoleDefinition);
                                    splistTasksList.RoleAssignments.Add(_singleWebRoleAssignment);
                                }
                                splistTasksList.Update();
                             subNodeWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    });
                }
                catch(SPException spekl) { }

                return isPermiApplied;
            }

    Any idea what may be the reason for this.

    Friday, July 25, 2014 3:27 PM

Answers

  • Are you sure, your tasks list title is "Task"?

    in different default languages, list title may be differ

    when using tryGetList, you most control null Values:

    SPList splistTasksList = subNodeWeb.Lists.TryGetList("Tasks");
                if (splistTasksList != null)
                {
                    if (!splistTasksList.HasUniqueRoleAssignments)
                        splistTasksList.BreakRoleInheritance(true);
    
                    foreach (SPUser singleeUser in spuserCollecApprovers)
                    {
                        //SPGroup _singleWebCurentSelectedGroup = subNodeSiteforCTypeWeb.SiteGroups[currSelectedGroupNameforCType];
                        SPRoleAssignment _singleWebRoleAssignment = new SPRoleAssignment(singleeUser);
                        SPRoleDefinition _singleTaskRoleDefinition = subNodeWeb.RoleDefinitions["Contribute"];
                        _singleWebRoleAssignment.RoleDefinitionBindings.Add(_singleTaskRoleDefinition);
                        splistTasksList.RoleAssignments.Add(_singleWebRoleAssignment);
                    }
                    splistTasksList.Update();
                }
                


    yaşamak bir eylemdir

    • Marked as answer by SaMolPP Saturday, July 26, 2014 10:54 AM
    Friday, July 25, 2014 11:27 PM

All replies

  • Are you able to add custom logging to this code and identity the exact line that fails?

    These postings are provided "AS IS" with no warranties, and confers no rights.

    Friday, July 25, 2014 3:34 PM
  • Are you sure, your tasks list title is "Task"?

    in different default languages, list title may be differ

    when using tryGetList, you most control null Values:

    SPList splistTasksList = subNodeWeb.Lists.TryGetList("Tasks");
                if (splistTasksList != null)
                {
                    if (!splistTasksList.HasUniqueRoleAssignments)
                        splistTasksList.BreakRoleInheritance(true);
    
                    foreach (SPUser singleeUser in spuserCollecApprovers)
                    {
                        //SPGroup _singleWebCurentSelectedGroup = subNodeSiteforCTypeWeb.SiteGroups[currSelectedGroupNameforCType];
                        SPRoleAssignment _singleWebRoleAssignment = new SPRoleAssignment(singleeUser);
                        SPRoleDefinition _singleTaskRoleDefinition = subNodeWeb.RoleDefinitions["Contribute"];
                        _singleWebRoleAssignment.RoleDefinitionBindings.Add(_singleTaskRoleDefinition);
                        splistTasksList.RoleAssignments.Add(_singleWebRoleAssignment);
                    }
                    splistTasksList.Update();
                }
                


    yaşamak bir eylemdir

    • Marked as answer by SaMolPP Saturday, July 26, 2014 10:54 AM
    Friday, July 25, 2014 11:27 PM
  • Thanks Ilqar Alizade!

    The tasks list was not present in the sub site through which i am iterating.I am  iteraing through each and every subsite in my site collection and reading the  tasks list and applying the permission. Yes, i didnt check for null values after calling trygetlist().

    I thought,  tasks list would be created automatically when a  subsite was created  from team site template.

    After the null check as you suggested, it works fine, it enters into the else { } , many of the sub sites in  my site collection. Now I have to create the tasks list programmatically when a sub site was   created and assign permissions for a special set of users.

    Thanks again!



    • Edited by SaMolPP Saturday, July 26, 2014 11:02 AM
    Saturday, July 26, 2014 9:59 AM