Exemplo n.º 1
0
        /// <inheritdoc/>
        public void CreateList(string listName, string description, List <string> fieldsAsXml)
        {
            // Need to modify to set privilege.
            using (var context = GetSharepointSiteContext())
            {
                Web orcaSite = context.Web;

                ListCreationInformation listCreationInfo = new ListCreationInformation();
                listCreationInfo.Title             = listName;
                listCreationInfo.TemplateType      = (int)ListTemplateType.GenericList;
                listCreationInfo.Description       = description;
                listCreationInfo.QuickLaunchOption = QuickLaunchOptions.On;
                // The new list is displayed on the Quick Launch of the site.
                List catalogList = orcaSite.Lists.Add(listCreationInfo);
                foreach (var fieldXml in fieldsAsXml)
                {
                    catalogList.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue);
                }

                // Hide the default title field (column).
                // When created the list, do not set the title as 'required'.
                Field title = orcaSite.Lists.GetByTitle(listName).Fields.GetByTitle("Title");
                title.Hidden   = true;
                title.Required = false;
                title.Update();

                var defaultListView = catalogList.DefaultView;
                context.Load(defaultListView);
                context.Load(defaultListView.ViewFields);
                context.Load(catalogList, listInfo => listInfo.DefaultViewUrl);
                context.ExecuteQuery();
                defaultListView.ViewFields.Remove("LinkTitle");
                defaultListView.Update();
                // add a navigation link to the newly created list
                string baseSharepointUrl = _sharepointUrl.Substring(0, _sharepointUrl.IndexOf("/sites"));
                orcaSite.AddNavigationNode(
                    listName,
                    new Uri($"{baseSharepointUrl}{catalogList.DefaultViewUrl}"),
                    string.Empty,
                    PnP.Framework.Enums.NavigationType.QuickLaunch);

                // Change permissions.
                Microsoft.SharePoint.Client.List targetList = orcaSite.Lists.GetByTitle(listName);
                context.Load(
                    targetList,
                    listInfo1 => listInfo1.HasUniqueRoleAssignments,
                    listInfo2 => listInfo2.RoleAssignments.Include(roles => roles.Member));
                context.ExecuteQuery();

                context.Load(orcaSite.AssociatedOwnerGroup, group => group.Id);
                context.ExecuteQuery();

                var siteOwnerGroupId = orcaSite.AssociatedOwnerGroup.Id;

                if (!targetList.HasUniqueRoleAssignments)
                {
                    // Target list is inheriting role assignments, break that.
                    targetList.BreakRoleInheritance(true, false);
                }
                // Delete all exist permissions except admin group.
                foreach (var assignment in targetList.RoleAssignments)
                {
                    // Delete the role assignment if it doesn't correspond to the site owners group.
                    if (assignment.Member.PrincipalType == PrincipalType.SharePointGroup && assignment.Member.Id != siteOwnerGroupId)
                    {
                        targetList.RoleAssignments.GetByPrincipal(assignment.Member).DeleteObject();
                    }
                }
                targetList.Update();
                context.ExecuteQuery();

                Console.WriteLine("List has been created and permissions on it has been modified.");
                // Currently only site admin can view this list.
            }
        }