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: | |

posted @ martedì 5 aprile 2011 16:00

Print

Comments on this entry:

# re: Ottenere la lista dei permessi di una list e relativi items

Left by rattan furniture at 20/02/2013 05:02
Gravatar

# re: Ottenere la lista dei permessi di una list e relativi items

Left by rattan furniture at 20/02/2013 05:07
Gravatar

# re: Ottenere la lista dei permessi di una list e relativi items

Left by outdoor furniture at 20/02/2013 05:10
Gravatar

# tankless water heater

Left by tankless water heater at 29/05/2013 16:42
Gravatar

# re: Ottenere la lista dei permessi di una list e relativi items

Left by 花岗岩 at 20/01/2014 05:58
Gravatar

# CISCO

Left by summer at 30/07/2014 11:02
Gravatar
my company main Cisco product sales,
The strength of the company and There is sufficient stock,
the price is pretty competitive,
I hope that we will have the opportunity to do business with you,
we can give you best price,
We sincerely hope to establish friendship with you.

# Your UPS Oline

Left by NETCCA at 19/12/2015 06:28
Gravatar

# Manager

Left by Tony at 18/01/2016 07:19
Gravatar
Comments have been closed on this topic.