Sharing the experience search

Search sharing-the-experience.blogspot.com

Thursday, March 24, 2011

splistitem.RoleAssignments comparison (SpRoleAssigementComparer)

   I have got a task to implement assignment population over the splistitem in case the current role assignments are not the same as I want to populate. To find out if the collections of SPRoleAssigment are the same , I need to implement custom IEqualityComparer<SPRoleAssignment>
  
 Here is the function to populate assignments if there are new:
  private void ApplyRoleAssigments(SPListItem item)
        {

            var newAssignments = new List<SPRoleAssignment>();

            var current = from rol in item.RoleAssignments.Cast<SPRoleAssignment>()
                          select rol;
            List<SPRoleAssignment> currentAssigments = current.ToList();


            IEnumerable<SPRoleAssignment> differenceNewAssigments = newAssignments.Except(currentAssigments,
                                                                                          new SpRoleAssigementComparer());
            IEnumerable<SPRoleAssignment> differenceCurrentAssigments = currentAssigments.Except(newAssignments,
                                                                                          new SpRoleAssigementComparer());

            if (differenceNewAssigments.Count() > 0 || differenceCurrentAssigments.Count() > 0)
            {
                SPSecurity.RunWithElevatedPrivileges(() =>
                          SharePointHelper.InvokeInSPListItemContext(operationContext, item.ParentList.RootFolder.ToString(), item.ID, (site, web, list, item_) =>
                          {
                              SharePointHelper.DisableEventFiring();
                              SPGroupHelper.ResetRoleAssignments(item_);
                              item_.RoleAssignments.Add(newAssignments);
                              item_.SystemUpdate();
                              SharePointHelper.EnableEventFiring();
                          }));
            }


        }

Here is a custom IEqualityComparer<SPRoleAssignment>

public class SpRoleAssigementComparer : IEqualityComparer<SPRoleAssignment>
    {

        public bool Equals(SPRoleAssignment x, SPRoleAssignment y)
        {
            //Check whether the compared objects reference the same data.
            if (ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
                return false;

            if (x.Member == null || y.Member == null)
            {
                return false;
            }

            if (x.Member.Name == y.Member.Name && x.RoleDefinitionBindings.Xml == y.RoleDefinitionBindings.Xml)
                return true;
            return false;
        }



        public int GetHashCode(SPRoleAssignment obj)
        {

            return obj.ToString().ToLower().GetHashCode();

        }

    }

No comments:

Post a Comment