Ottenere la lista dei permessi di una list e relativi items

Tra le tante cose in quest’ultimo periodo di attività, ho avuto la necessità di creare un XML da dare in pasto a degli utenti che gestiscono la parte di controlli di accesso ad un’area delicata di un progetto in Sharepoint 2010.

Grazie a LinqToXML il compito può esser facilmente svolto Smile

Il codice non è completo, ma bastano queste poche righe per capire come poter continuare ed ottenere tutti i dati necessari per completarlo:

 

        public class ListInformation
        {
            public List<UserPermission> UserPermissions { get; set; }
            public List<ItemPermission> ItemPermissions { get; set; }
        }

        public class UserPermission
        {
            public string Username { get; set; }
            public List<string> Permissions { get; set; }
        }

        public class ItemPermission
        {
            public string Title { get; set; }
            public List<string> Permissions { get; set; }
        }

        private void GenerateXml(SPWeb spWeb, string spListName)
        {
            SPList myList = spWeb.Lists[spListName];
            var roleAssignments = myList.RoleAssignments;

            var myListInformation = new ListInformation();

            var lstPermissions = new List<UserPermission>();

            foreach (SPRoleAssignment roleAssignment in roleAssignments)
            {
                var roles = (from SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings select roleDefinition.Name).ToList();

                lstPermissions.Add(new UserPermission
                                    {
                                        Username = roleAssignment.Member.Name,
                                        Permissions = roles
                                    });
            }

            myListInformation.UserPermissions = lstPermissions;

            var permissionXml =
                new XElement("List", new XElement("Permissions",
                            from userPermission in myListInformation.UserPermissions
                            select new XElement("User", new XAttribute("Username", userPermission.Username),
                                                new XElement("Permissions",
                                                            from perm in userPermission.Permissions
                                                            select new XElement("Permission", perm)))));

            var items = myList.Items;

            var itemPermissions = new List<ItemPermission>();

            foreach (SPListItem item in items)
            {
                var roles = (from SPRoleAssignment roleDefinition in item.RoleAssignments
                             select roleDefinition.RoleDefinitionBindings.).ToList();

                itemPermissions.Add(new ItemPermission()
                                        {
                                            Title = item.Title,
                                            Permissions = roles
                                        });
            }

            permissionXml.Add(new XElement("ItemsCollection",
                from item in itemPermissions
                select new XElement("Item", new XAttribute("Title", item.Title),
                   new XElement("Permissions",
                               from perm in item.Permissions
                               select new XElement("Permission", perm)))));

            permissionXml.Save(@"C:\temp\AWEmpPOs.xml");

buona lettura

Rebitting Tags: | |