private void ProvisionStructuralNavigationNodes(Web web, TokenParser parser, Enums.NavigationType navigationType, Model.NavigationNodeCollection nodes, PnPMonitoredScope scope, string parentNodeTitle = null) { foreach (var node in nodes) { var navNode = web.AddNavigationNode( parser.ParseString(node.Title), new Uri(parser.ParseString(node.Url), UriKind.RelativeOrAbsolute), parser.ParseString(parentNodeTitle), navigationType, node.IsExternal); ProvisionStructuralNavigationNodes( web, parser, navigationType, node.NavigationNodes, scope, parser.ParseString(node.Title)); #if !SP2013 if (node.Title.ContainsResourceToken()) { navNode.LocalizeNavigationNode(web, node.Title, parser, scope); } #endif } }
private void ProvisionStructuralNavigationNodes(Web web, TokenParser parser, Enums.NavigationType navigationType, Model.NavigationNodeCollection nodes, String parentNodeTitle = null) { foreach (var node in nodes) { web.AddNavigationNode( node.Title, new Uri(parser.ParseString(node.Url), UriKind.RelativeOrAbsolute), parentNodeTitle, navigationType, node.IsExternal); ProvisionStructuralNavigationNodes( web, parser, navigationType, node.NavigationNodes, node.Title); } }
/// <inheritdoc/> public void CreateList(string listName, string description, List <string> fieldsAsXml) { // Need to modify to set privilege. using (var context = _authenticationManager.GetContext(_sharepointUrl)) { Web orcaSite = context.Web; ListCreationInformation listCreationInfo = new ListCreationInformation(); listCreationInfo.Title = listName; listCreationInfo.TemplateType = (int)ListTemplateType.GenericList; listCreationInfo.Description = description; List catalogList = orcaSite.Lists.Add(listCreationInfo); foreach (var fieldXml in fieldsAsXml) { catalogList.Fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.DefaultValue); } // hide the default title field Field titleField = orcaSite.Lists.GetByTitle(listName).Fields.GetByTitle("Title"); titleField.Hidden = true; titleField.Required = false; titleField.Update(); var defaultListView = catalogList.DefaultView; context.Load(defaultListView); context.Load(defaultListView.ViewFields); context.ExecuteQuery(); defaultListView.ViewFields.Remove("LinkTitle"); defaultListView.Update(); // add a navigation link to the newly created list orcaSite.AddNavigationNode( listName, new Uri($"{_sharepointUrl}/Lists/{listName}"), string.Empty, PnP.Framework.Enums.NavigationType.QuickLaunch); context.ExecuteQuery(); } }
/// <summary> /// Processes the navigation nodes for a web. /// </summary> /// <param name="clientContext"></param> /// <param name="web"></param> /// <param name="navigationNodes">Navigation nodes to add.</param> private static void ProcessWebNavigationNodes(ClientContext clientContext, Web web, List <PnP.Entities.NavigationNodeEntity> navigationNodes) { clientContext.Load(web, s => s.ServerRelativeUrl, s => s.Navigation, s => s.Webs); clientContext.ExecuteQueryRetry(); Console.WriteLine("Adding navigation nodes to {0}", web.ServerRelativeUrl); var typesToDelete = navigationNodes.Select(i => i.NavigationType).Distinct(); foreach (var type in typesToDelete) { web.DeleteAllNavigationNodes(type); } foreach (var node in navigationNodes) { web.AddNavigationNode(node.Title, node.Uri, node.ParentTitle, node.NavigationType, node.IsExternal); } if (web.Webs.Any()) { ProcessWebNavigationNodes(clientContext, web.Webs, navigationNodes); } }
private void ProvisionStructuralNavigationNodes(Web web, TokenParser parser, Enums.NavigationType navigationType, Model.NavigationNodeCollection nodes, PnPMonitoredScope scope, string parentNodeTitle = null, string l1ParentNodeTitle = null) { foreach (var node in nodes) { try { string fileUrl = ReplaceFileUniqueToken(web, parser.ParseString(node.Url)); var navNode = web.AddNavigationNode( parser.ParseString(node.Title), new Uri(fileUrl, UriKind.RelativeOrAbsolute), parser.ParseString(parentNodeTitle), navigationType, node.IsExternal, l1ParentNodeTitle: l1ParentNodeTitle ); #if !SP2013 if (node.Title.ContainsResourceToken()) { navNode.LocalizeNavigationNode(web, node.Title, parser, scope); } #endif } catch (ServerException ex) { // If the SharePoint link doesn't exist, provision it as external link // when we provision as external link, the server side URL validation won't kick-in // This handles the "no such file or url found" error WriteMessage(String.Format(CoreResources.Provisioning_ObjectHandlers_Navigation_Link_Provisioning_Failed_Retry, node.Title), ProvisioningMessageType.Warning); if (ex.ServerErrorCode == -2130247147) { try { string fileUrl = ReplaceFileUniqueToken(web, parser.ParseString(node.Url)); var navNode = web.AddNavigationNode( parser.ParseString(node.Title), new Uri(fileUrl, UriKind.RelativeOrAbsolute), parser.ParseString(parentNodeTitle), navigationType, true, l1ParentNodeTitle: l1ParentNodeTitle ); } catch (Exception innerEx) { WriteMessage(String.Format(CoreResources.Provisioning_ObjectHandlers_Navigation_Link_Provisioning_Failed, innerEx.Message), ProvisioningMessageType.Warning); } } else { WriteMessage(String.Format(CoreResources.Provisioning_ObjectHandlers_Navigation_Link_Provisioning_Failed, ex.Message), ProvisioningMessageType.Warning); } } ProvisionStructuralNavigationNodes( web, parser, navigationType, node.NavigationNodes, scope, parser.ParseString(node.Title), parentNodeTitle ); } }
private void ProvisionStructuralNavigationNodes(Web web, TokenParser parser, Enums.NavigationType navigationType, Model.NavigationNodeCollection nodes, string parentNodeTitle = null) { foreach (var node in nodes) { web.AddNavigationNode( parser.ParseString(node.Title), new Uri(parser.ParseString(node.Url), UriKind.RelativeOrAbsolute), parser.ParseString(parentNodeTitle), navigationType, node.IsExternal); ProvisionStructuralNavigationNodes( web, parser, navigationType, node.NavigationNodes, parser.ParseString(node.Title)); } }
private static void UpdateQuickLaunch(ClientContext ctx, Web web, SiteType siteType) { ctx.Load(web, w => w.ParentWeb); //Site Collection (parent) if (siteType == SiteType.SiteCollection) { web.DeleteAllNavigationNodes(OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); //Quick launch web.AddNavigationNode(web.Title, new Uri(web.Url), "", OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); web.AddNavigationNode("Projects", new Uri(web.Url), "", OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); AddListToQuickLaunch(ctx, web, "Shared Documents", web.Title); AddListToQuickLaunch(ctx, web, "Contacts", web.Title); web.AddNavigationNode("Notebook", new Uri(GetSiteNotebookUrl(ctx, web)), web.Title, OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); web.AddNavigationNode("Site Contents", new Uri(GetSiteContentsUrl(ctx, web)), web.Title, OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); } //Subsite (child) else { //Navigation settings NavigationExtensions.UpdateNavigationInheritance(web, true); web.DeleteAllNavigationNodes(OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); //Quick launch web.AddNavigationNode(web.Title, new Uri(web.Url), "", OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); AddListToQuickLaunch(ctx, web, "Shared Documents", web.Title); AddListToQuickLaunch(ctx, web, "RAID Log", web.Title); AddListToQuickLaunch(ctx, web, "Calendar", web.Title); AddListToQuickLaunch(ctx, web, "Contacts", web.Title); web.AddNavigationNode("Notebook", new Uri(GetSiteNotebookUrl(ctx, web)), web.Title, OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); web.AddNavigationNode("Site Contents", new Uri(GetSiteContentsUrl(ctx, web)), web.Title, OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); } }
private static void AddListToQuickLaunch(ClientContext ctx, Web web, string listTitle, string parentNodeTitle = "") { if (web.ListExists(listTitle)) { List list = web.GetListByTitle(listTitle); ctx.Load(list, s => s.DefaultViewUrl, s => s.Title); ctx.ExecuteQuery(); web.AddNavigationNode(listTitle, new Uri(list.DefaultViewUrl, UriKind.Relative), parentNodeTitle, OfficeDevPnP.Core.Enums.NavigationType.QuickLaunch); } }
/// <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. } }
private static void applyTemplate(SiteInformation siteInformation, ExecutionContext functionContext, ClientCredentials credentials, TraceWriter log) { try { using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteInformation.SiteUrl, credentials.ClientID, credentials.ClientSecret)) { Web web = ctx.Web; ctx.Load(web, w => w.Title, w => w.Navigation.QuickLaunch); ctx.ExecuteQueryRetry(); string groupID = GetSiteGroupID(ctx); // UpdateSubscriptionItemProperties(credentials, siteInformation, web.Title ,log); var rootSiteUrl = ConfigurationManager.AppSettings["RootSiteUrl"]; log.Info($"Successfully connected to site: {web.Title}"); var navigationcolls = web.Navigation.QuickLaunch; foreach (var nav in navigationcolls) { if (nav.Title == "Key Dates & Deliverables") { string url = nav.Url.Split('?')[0] + "?groupId=" + groupID + "&planId=" + siteInformation.PlanId; nav.Url = url; nav.Update(); ctx.ExecuteQueryRetry(); } } string currentDirectory = functionContext.FunctionDirectory; DirectoryInfo dInfo = new DirectoryInfo(currentDirectory); var schemaDir = dInfo.Parent.FullName + "\\Templates"; XMLTemplateProvider sitesProvider = new XMLFileSystemTemplateProvider(schemaDir, ""); log.Info($"About to get template with with filename '{PNP_TEMPLATE_FILE}'"); ProvisioningTemplate template = sitesProvider.GetTemplate(PNP_TEMPLATE_FILE); var getHomeClientPage = template.ClientSidePages.Find(i => i.Title == "Home"); if (getHomeClientPage != null) { UpdateControlsDataDynamic(siteInformation, getHomeClientPage); } log.Info($"Successfully found template with ID '{template.Id}'"); ProvisioningTemplateApplyingInformation ptai = new ProvisioningTemplateApplyingInformation { ProgressDelegate = (message, progress, total) => { log.Info(string.Format("{0:00}/{1:00} - {2}", progress, total, message)); } }; // Associate file connector for assets.. FileSystemConnector connector = new FileSystemConnector(Path.Combine(currentDirectory, "Files"), ""); template.Connector = connector; web.ApplyProvisioningTemplate(template, ptai); if (siteInformation.IsTopNavigation) { // Add top navigation bar. web.AddNavigationNode("SharePoint Main Menu", new Uri(rootSiteUrl + "/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); web.AddNavigationNode("Document Centre", new Uri(rootSiteUrl + "/Document%20Centre/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); web.AddNavigationNode("Project Centre", new Uri(rootSiteUrl + "/Project%20Centre/SitePages/Home.aspx"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); web.AddNavigationNode("WHS Centre", new Uri(rootSiteUrl + "/WHS%20Centre/"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); web.AddNavigationNode("Training Centre", new Uri(rootSiteUrl + "/Training%20Centre"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); web.AddNavigationNode("Proposal Hub", new Uri(rootSiteUrl + "/Proposal%20Hub"), "", OfficeDevPnP.Core.Enums.NavigationType.TopNavigationBar); } UpdateListTitle(ctx, web, "01. Project Management"); } } catch (Exception e) { log.Error("Error when applying PnP template!", e); throw; } }